473,785 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help cast Mask GetPrivateProfi leString from Kernel32

84 New Member
Hi Guys,
I need some urgent help with this as I am becoming clueless now.

I have 2 DllImport as below from Kernel32

Expand|Select|Wrap|Line Numbers
  1. [DllImport("kernel32")]
  2. private static extern int GetPrivateProfileString(string section, int key, string defaultValue, [MarshalAs(UnmanagedType.LPArray)] byte[] result, int size, string fileName);
  3.  
  4. [DllImport("kernel32")]
  5. private static extern int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder result, int size, string fileName);
Now, I have made my DLL called CustomDLL.DLL which maps the second one properly. However, I am trying to use this and convert one above aswell and this is where i am not sure what to do.

***this doesnt work*******
Expand|Select|Wrap|Line Numbers
  1. private static int GetPrivateProfileString(string section, int key, string defaultValue, [MarshalAs(UnmanagedType.LPArray)] byte[] result, int size, string fileName)
  2.         {
  3.             StringBuilder res = new StringBuilder(Encoding.ASCII.GetString(result));
  4.             int ret = IniFile.GetPrivateProfileString(section, key.ToString(), defaultValue, res, size, fileName);
  5.  
  6.             result = Encoding.ASCII.GetBytes(res.ToString());
  7.  
  8.             return ret;
  9.         }
  10.  
****this works fine*********** *
Expand|Select|Wrap|Line Numbers
  1. private static int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder result, int size, string fileName)
  2.         {
  3.             return IniFile.GetPrivateProfileString(section, key, defaultValue, result, size, fileName);
  4.         }


Please help, how do i make the not working bit working here. Any help will be appreciated as this is very very urgent!
Jan 16 '09 #1
6 3812
nukefusion
221 Recognized Expert New Member
Hi There,
Should you not be passing a char array instead of a byte array? Like this:

Expand|Select|Wrap|Line Numbers
  1. [DllImport("kernel32.dll")]
  2. static extern uint GetPrivateProfileString(
  3.    string lpAppName, 
  4.    string lpKeyName,
  5.    string lpDefault, 
  6.    [In, Out] char[] lpReturnedString, 
  7.    uint nSize,
  8.    string lpFileName);
  9.  
Also, please try to use code tags when posting code samples, as it makes it easier for us to read.
Jan 16 '09 #2
vekipeki
229 Recognized Expert New Member
If I understood well, you want to create a .Net assembly (CustomDLL.DLL) which wraps Kernel32 imports?

1. If CustomDLL.DLL is going to be called from managed code only, you shouldn't add the MarshalAs() attribute. If it is not being marshalled, this only makes it confusing later.

2. When you say "it doesn't work", what does that actually mean? One thing that surely does not work is changing your "result" parameter's reference in line:

Expand|Select|Wrap|Line Numbers
  1. result = Encoding.ASCII.GetBytes(res.ToString());
Since "result" is passed by value, you cannot expect it to change outside your method. Here is what I'm talking about:

Expand|Select|Wrap|Line Numbers
  1. private static void DoSomething(int x)
  2. {
  3.     x = 5;
  4. }
  5.  
  6. private static void Main()
  7. {
  8.       int x = 4;
  9.       DoSomething(x);
  10.       Console.WriteLine(x); // x is 4
  11. }
  12.  
But the actual remark is: if you are wrapping DllImports, you don't need to stick to the original function signature in your managed code; you are free to pass any parameters and return any object from your method (returning int in managed code for error handling is usually avoided), so it's best to create a method that is simple to use from managed code, and then hide the messy DllImport stuff inside.
Jan 16 '09 #3
alag20
84 New Member
Hi nuke,
Thanks for you help.

The way the application stands, I am being forced to used byte array.

I cant go changing the who app so i need to make the switch at this level, i.e. map my dll there so that it can take that byte array. instead of StringBuilder etc.

Please help
Jan 16 '09 #4
alag20
84 New Member
hi vekipeki

I tried the following :-

Expand|Select|Wrap|Line Numbers
  1. private static int GetPrivateProfileString(int section, string key, string defaultValue,[MarshalAs(UnmanagedType.LPArray)] byte[] result, int size, string fileName)
  2. {
  3.    StringBuilder res = new StringBuilder(Encoding.ASCII.GetString(result));
  4.    int ret = IniFile.GetPrivateProfileString(section.ToString(), key, defaultValue, res, size, fileName);
  5.  
  6.             result = Encoding.ASCII.GetBytes(res.ToString());
  7.  
  8.             return ret;
  9.         }
  10.  

Doesnt work either - i should have mentioned that i already tried that:(


As i mentioned, the CustomDll is in C++ which has been done for one reason only - that filename can be changed on the fly and this has been done by an external company and i have no access to its source code:(
Jan 16 '09 #5
nukefusion
221 Recognized Expert New Member
I'm not sure I understand entirely what it is you are trying to do. You simply cannot use a byte array in your call to the Kernel32 method because it's not available as a parameter in the signature exposed.
If needs be, you'll need to do some sort of type conversion within your custom dll, but you can't pass a byte array if the method doesn't accept it.
Jan 16 '09 #6
vekipeki
229 Recognized Expert New Member
I am also not sure I understand what's going on, but here is what I am talking about (that is at least one of the problems).

This will never work, because result will point to a different object, so after the method ends the original byte[] array will not be changed:
Expand|Select|Wrap|Line Numbers
  1. result = Encoding.ASCII.GetBytes(res.ToString());
If you want the result to be changed, you can either:

1. Add the ref keyword to pass it by reference:
Expand|Select|Wrap|Line Numbers
  1. ref byte[] result
2. Or, if your method signature must stay the same, use Array.Copy to actually copy the elements into your array:
Expand|Select|Wrap|Line Numbers
  1. byte[] data = Encoding.ASCII.GetBytes("000");
  2. Array.Copy(data, result, data.Length);
  3.  
Note that the last line will throw an exception if result is not large enough, so it must be preallocated to the right size. You cannot allocate it or resize it in this method, because it is not passed by reference.
Jan 16 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
19243
by: Mark Hayworth | last post by:
Randy: I too am having the problem that this other guy/girl had. I put in the proper API declarations and arguments for GetPrivateProfileString yet it doesn't return the correct stuff. It always returns the default value (padded with huge amount of spaces on the right) and the return value (number of characters read) is some crazy huge number like 51298398749084 Your demo doesn't work in .Net - it's for prior versions of VB, and so the...
2
2183
by: OZ | last post by:
Hi, I am new C++ and need a little help with a public domain program that is suppose to perform a byte swap. I am receiving the following error messages during the compile process with Microsoft C++ 2003. Here are the error messages and source code: (97) : error C2664: 'fgetpos' : cannot convert parameter 2 from 'void
1
3961
by: Germic | last post by:
Does C# has an equivalent of the 'C' GetPrivateProfileString? or is the only way to get similar features to read from an INI file is to do a DllImport on Kernel32.dll? Thanks
8
4348
by: Duncan Winn | last post by:
I am new to VC++7. I am using a method GetPrivateProfileString that requires an LPTSTR. I have defined this as a: char * data_name; I am then trying to convert this to an LPOLESTR and I have done this as follows: LPOLESTR dunk_data = (LPOLESTR)T2CW(data_name);
5
19690
by: L | last post by:
static extern uint GetPrivateProfileString( string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName); Interoping GetPrivateProfileString is giving me this Exception in
9
7135
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but when I tried the program on the Win98 system it will be running on, I get the following error: Cast from string "2076719" to type 'Long' is not valid I am not sure why I only get this error on the Win98 system or how to go about correcting...
1
3657
by: Domac | last post by:
I need to read some configuration data from .ini file located at Application.StartupPath location . Here is code snippet : Public Declare Unicode Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
5
7663
by: 海风 | last post by:
A question about GetPrivateProfileString a section in a .ini file , for example ip = 192.168.1.112 .... i want to get the ip value by using GetPrivateProfileString() function. the code is :
2
3755
by: Nhan | last post by:
Hi, I am trying to use the function GetPrivateProfileString as following: public static extern Int32 GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString, short nSize, string lpFileName); ....... String sDatabase = "";
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8978
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.