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\0 T\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" 5 9035
On Nov 16, 12:34 pm, "Marc" <i...@ingcarlease.nlwrote:
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
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]
- mv*@spam.guard.caspershouse.com
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:b3**********************************@41g2000h sh.googlegroups.com...
On Nov 16, 12:34 pm, "Marc" <i...@ingcarlease.nlwrote:
>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
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
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(
I am using GetPrivateProfileSECTION not GetPrivateProfileSTRING
But I guess the defintion should be:
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
static extern uint GetPrivateProfileSection(
string lpAppName,
[In, Out] char[] lpReturnedString,
uint nSize,
string lpFileName);
And this I am using in my program:
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern uint GetPrivateProfileSectionW(string lpAppName, IntPtr
lpReturnedString, uint nSize, string lpFileName);
public static bool GetPrivateProfileSectionAsCS(string appName, string
fileName, out string section)
{
So there is a difference, but do not ask me now what is correct.
And there is also this little problem. The section is not one null
terminated string but several. This is in MSDN
"The format of the returned keys and values is one or more null-terminated
strings, followed by a final null character. Each string has the following
form: key=string"
I've already used GetPrivateProfileSTRING, that one was easy. Ok, I will
look into it and thanks for suggestions.
Marc,
Regardless, if you use the character array, then you can parse the
character array, looking for the null characters, and splitting the strings
apart that way.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Marc" <in**@ingcarlease.nlwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
> 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(
I am using GetPrivateProfileSECTION not GetPrivateProfileSTRING
But I guess the defintion should be:
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
static extern uint GetPrivateProfileSection(
string lpAppName,
[In, Out] char[] lpReturnedString,
uint nSize,
string lpFileName);
And this I am using in my program:
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern uint GetPrivateProfileSectionW(string lpAppName, IntPtr
lpReturnedString, uint nSize, string lpFileName);
public static bool GetPrivateProfileSectionAsCS(string appName, string
fileName, out string section)
{
So there is a difference, but do not ask me now what is correct.
And there is also this little problem. The section is not one null
terminated string but several. This is in MSDN
"The format of the returned keys and values is one or more null-terminated
strings, followed by a final null character. Each string has the following
form: key=string"
I've already used GetPrivateProfileSTRING, that one was easy. Ok, I will
look into it and thanks for suggestions.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
Marc,
Regardless, if you use the character array, then you can parse the
character array, looking for the null characters, and splitting the
strings apart that way.
Hey thanks, this works:
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern uint GetPrivateProfileSection(string lpAppName, [In, Out]
char[] lpReturnedString, uint nSize, string lpFileName);
//
public static bool GetPrivateProfileSectionAsCommaText(string appName,
string fileName, out string section)
{
section = "";
uint MAX_BUFFER = 327670;
char[] sectionchar = new char[MAX_BUFFER];
if (!System.IO.File.Exists(fileName))
return false;
uint bytesReturned = GetPrivateProfileSection(appName, sectionchar,
MAX_BUFFER, fileName);
if ((bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0))
return false;
for (int i = 0; i < bytesReturned; i++)
{
if (sectionchar[i] != (char)0)
section += sectionchar[i];
else
{
if (i != bytesReturned - 1) section += ',';
}
}
return true;
}
>
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kai Bohli |
last post by:
Hi all !
I need to translate a string to Ascii and return a string again. The code below dosen't work for
Ascii (Superset) codes above 127. Any help are greatly appreciated.
protected...
|
by: Ricardo Quintanilla |
last post by:
i have a code that sends data to a socket listening over as400 platform,
the socket responds to me as a "byte array".
then i need to convert the "byte array" into a string.
the problem is that...
|
by: MilanB |
last post by:
Hello
How to convert char to int?
Thanks
|
by: Allan Ebdrup |
last post by:
How do I easily convert a int to a string?
Kind Regards,
Allan Ebdrup
|
by: Mika M |
last post by:
Hi!
I've made little code to convert string into hex string...
Public ReadOnly Property ToHexString(ByVal text As String) As String
Get
Dim arrBytes As Integer() = CharsToBytes(text)
Dim sb...
| |
by: Dirk Hagemann |
last post by:
Hi!
When I receive data from Microsoft Active Directory it is an
"ad_object" and has the type unicode. When I try to convert it to a
string I get this error:
UnicodeEncodeError: 'ascii' codec...
|
by: meendar |
last post by:
Hi,
I am having a character pointer which contains ascii values. i just
want to convert all these ascii values to respective characters and
again store it in another character pointer.
...
|
by: est |
last post by:
From python manual
str( )
Return a string containing a nicely printable representation of an
object. For strings, this returns the string itself. The difference
with repr(object) is that...
|
by: aatif |
last post by:
I want to convert a string of hex characters (2 hex chars = 1 byte), to ASCII. Hex chars include zeros (0x00) as well, which I want to include in ASCII string.
hex string: 5000005355....
ASCII:...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |