Hi Guys,
I need some urgent help with this as I am becoming clueless now.
I have 2 DllImport as below from Kernel32 - [DllImport("kernel32")]
-
private static extern int GetPrivateProfileString(string section, int key, string defaultValue, [MarshalAs(UnmanagedType.LPArray)] byte[] result, int size, string fileName);
-
-
[DllImport("kernel32")]
-
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******* - private static int GetPrivateProfileString(string section, int key, string defaultValue, [MarshalAs(UnmanagedType.LPArray)] byte[] result, int size, string fileName)
-
{
-
StringBuilder res = new StringBuilder(Encoding.ASCII.GetString(result));
-
int ret = IniFile.GetPrivateProfileString(section, key.ToString(), defaultValue, res, size, fileName);
-
-
result = Encoding.ASCII.GetBytes(res.ToString());
-
-
return ret;
-
}
-
****this works fine************ - private static int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder result, int size, string fileName)
-
{
-
return IniFile.GetPrivateProfileString(section, key, defaultValue, result, size, fileName);
-
}
Please help, how do i make the not working bit working here. Any help will be appreciated as this is very very urgent!
6 3765
Hi There,
Should you not be passing a char array instead of a byte array? Like this: -
[DllImport("kernel32.dll")]
-
static extern uint GetPrivateProfileString(
-
string lpAppName,
-
string lpKeyName,
-
string lpDefault,
-
[In, Out] char[] lpReturnedString,
-
uint nSize,
-
string lpFileName);
-
Also, please try to use code tags when posting code samples, as it makes it easier for us to read.
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: - 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 : -
private static void DoSomething(int x)
-
{
-
x = 5;
-
}
-
-
private static void Main()
-
{
-
int x = 4;
-
DoSomething(x);
-
Console.WriteLine(x); // x is 4
-
}
-
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.
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
hi vekipeki
I tried the following :- -
private static int GetPrivateProfileString(int section, string key, string defaultValue,[MarshalAs(UnmanagedType.LPArray)] byte[] result, int size, string fileName)
-
{
-
StringBuilder res = new StringBuilder(Encoding.ASCII.GetString(result));
-
int ret = IniFile.GetPrivateProfileString(section.ToString(), key, defaultValue, res, size, fileName);
-
-
result = Encoding.ASCII.GetBytes(res.ToString());
-
-
return ret;
-
}
-
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:(
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.
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: - 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:
2. Or, if your method signature must stay the same, use Array.Copy to actually copy the elements into your array: -
byte[] data = Encoding.ASCII.GetBytes("000");
-
Array.Copy(data, result, data.Length);
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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...
|
by: L |
last post by:
static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
uint nSize,
string lpFileName);
Interoping...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |