M
Marc
Hello dear,
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?
This is my function I am trying to get right:
public static bool GetPrivateProfileSectionAsCS(string appName, string
fileName, out string section)
{
section = null;
if (!System.IO.File.Exists(fileName))
return false;
uint MAX_BUFFER = 32767;
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER);
uint bytesReturned = GetPrivateProfileSectionW(appName, pReturnedString,
MAX_BUFFER, fileName);
if ((bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0))
return false;
System.Text.StringBuilder returnedString = new
System.Text.StringBuilder((int)bytesReturned);
//bytesReturned -1 to remove trailing \0
for (int i = 0; i < bytesReturned - 1; i++)
returnedString.Append((char)Marshal.ReadByte(new
IntPtr((uint)pReturnedString + (uint)i)));
Marshal.FreeCoTaskMem(pReturnedString);
section = returnedString.ToString();
section.Replace("\0", ""); // this does not work
return true;
}
And this is how my string section looks like:
"N\0U\0M\0B\0E\0R\0=\0f\0t\0A\0u\0t\0o\0I\0n\0c\0\0\0S\0t\0a\0t\0u\0s\0N\0r\0=\0I\0D\0A\0\0\0P\0a\0t\0N\0a\0m\0e\0=\0N\0o\0t\0U\0s\0e\0d\0\0\0D\0a\0t\0e\0=\0T\0x\0_\0D\0t\0T\0m\0<\0r\0e\0g\0e\0x\0>\0(\00\0[\01\0-\09\0]\0|\0[\01\02\0]\0[\00\0-\09\0]\0|\03\0[\00\01\0]\0)\0-\0(\00\0[\01\0-\09\0]\0|\01\0[\00\01\02\0]\0)\0-\0(\01\09\0|\02\00\0)\0[\00\0-\09\0]\0[\00\0-\09\0]\0<\0/\0r\0e\0g\0e\0x\0>\0\0\0T\0i\0m\0e\0=\0T\0x\0_\0D\0t\0T\0m\0<\0r\0e\0g\0e\0x\0>\0[\00\0-\09\0]\0[\00\0-\09\0]\0:\0[\00\0-\09\0]\0[\00\0-\09\0]\0:\0[\00\0-\09\0]\0[\00\0-\09\0]\0<\0/\0r\0e\0g\0e\0x\0>\0\0\0V\0e\0l\0d\0n\0a\0a\0m\0=\0F\0i\0e\0l\0d\0_\0L\0a\0b\0e\0l\0+\0F\0i\0e\0l\0d\0_\0N\0a\0m\0e\0\0\0C\0l\0i\0n\0i\0c\0=\0T\0s\0t\0l\0\0\0B\0e\0a\0m\0N\0r\0=\0N\0o\0t\0U\0s\0e\0d\0\0\0S\0e\0g\0m\0N\0r\0=\0N\0o\0t\0U\0s\0e\0d\0\0\0T\0e\0r\0m\0i\0n\0a\0t\0e\0=\0N\0o\0t\0U\0s\0e\0d\0\0\0D\0i\0a\0p\0h\0P\0r\0e\0s\0c\0=\0N\0o"
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?
This is my function I am trying to get right:
public static bool GetPrivateProfileSectionAsCS(string appName, string
fileName, out string section)
{
section = null;
if (!System.IO.File.Exists(fileName))
return false;
uint MAX_BUFFER = 32767;
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER);
uint bytesReturned = GetPrivateProfileSectionW(appName, pReturnedString,
MAX_BUFFER, fileName);
if ((bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0))
return false;
System.Text.StringBuilder returnedString = new
System.Text.StringBuilder((int)bytesReturned);
//bytesReturned -1 to remove trailing \0
for (int i = 0; i < bytesReturned - 1; i++)
returnedString.Append((char)Marshal.ReadByte(new
IntPtr((uint)pReturnedString + (uint)i)));
Marshal.FreeCoTaskMem(pReturnedString);
section = returnedString.ToString();
section.Replace("\0", ""); // this does not work
return true;
}
And this is how my string section looks like:
"N\0U\0M\0B\0E\0R\0=\0f\0t\0A\0u\0t\0o\0I\0n\0c\0\0\0S\0t\0a\0t\0u\0s\0N\0r\0=\0I\0D\0A\0\0\0P\0a\0t\0N\0a\0m\0e\0=\0N\0o\0t\0U\0s\0e\0d\0\0\0D\0a\0t\0e\0=\0T\0x\0_\0D\0t\0T\0m\0<\0r\0e\0g\0e\0x\0>\0(\00\0[\01\0-\09\0]\0|\0[\01\02\0]\0[\00\0-\09\0]\0|\03\0[\00\01\0]\0)\0-\0(\00\0[\01\0-\09\0]\0|\01\0[\00\01\02\0]\0)\0-\0(\01\09\0|\02\00\0)\0[\00\0-\09\0]\0[\00\0-\09\0]\0<\0/\0r\0e\0g\0e\0x\0>\0\0\0T\0i\0m\0e\0=\0T\0x\0_\0D\0t\0T\0m\0<\0r\0e\0g\0e\0x\0>\0[\00\0-\09\0]\0[\00\0-\09\0]\0:\0[\00\0-\09\0]\0[\00\0-\09\0]\0:\0[\00\0-\09\0]\0[\00\0-\09\0]\0<\0/\0r\0e\0g\0e\0x\0>\0\0\0V\0e\0l\0d\0n\0a\0a\0m\0=\0F\0i\0e\0l\0d\0_\0L\0a\0b\0e\0l\0+\0F\0i\0e\0l\0d\0_\0N\0a\0m\0e\0\0\0C\0l\0i\0n\0i\0c\0=\0T\0s\0t\0l\0\0\0B\0e\0a\0m\0N\0r\0=\0N\0o\0t\0U\0s\0e\0d\0\0\0S\0e\0g\0m\0N\0r\0=\0N\0o\0t\0U\0s\0e\0d\0\0\0T\0e\0r\0m\0i\0n\0a\0t\0e\0=\0N\0o\0t\0U\0s\0e\0d\0\0\0D\0i\0a\0p\0h\0P\0r\0e\0s\0c\0=\0N\0o"