Or, he could just use a definition of GetPrivateProvideSection which
would marshal the string correctly. A declaration that uses bytes in the
parameter doesn't do any good.
The declaration should be:
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
[In, Out] char[] lpReturnedString,
uint nSize,
string lpFileName);
The use of the char array is to prevent the marshaling layer from not
sending back all of the characters in the string after finding the first
null character.
It makes it a lot easier than allocating the memory and whatnot (which
should have been placed in a try/finally block, in the case of an
exception).
--
- Nicholas Paldino [.NET/C# MVP]
-
(E-Mail Removed)
"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:b34c99c8-a2fa-476b-ac0c-(E-Mail Removed)...
> On Nov 16, 12:34 pm, "Marc" <i...@ingcarlease.nl> wrote:
>> I have a string and every second char is a \0. Can I somehow convert it
>> to a
>> normal string. Or may-be in the underlying code I am doing something
>> wrong,
>> choose the wrong C# type?
>
> I don't know the details of GetPrivateProfileSectionW, but you may
> well be able to get away with creating a byte[] instead of using
> AllocCoTaskMem to return an IntPtr. You can then pass that byte[] into
> GetPrivateProfileSectionW and use Encoding.Unicode afterwards.
>
> Otherwise, use Marshal.Copy to copy the data into a byte[], and then
> again you can use Encoding.Unicode to turn the data into a string.
>
> If these don't work, it's worth asking on the .interop newsgroup.
>
> Jon