473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Help cast Mask GetPrivateProfileString from Kernel32

84
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 3765
nukefusion
221 Expert 100+
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 Expert 100+
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
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
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 Expert 100+
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 Expert 100+
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
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...
2
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...
1
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
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...
5
by: L | last post by:
static extern uint GetPrivateProfileString( string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName); Interoping...
9
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...
1
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...
5
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
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...

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.