ranadheer mac

hi all,

i have a StringBuilder object with unknown number of strings each string followed by a null character. i am getting the only first string by whatever the parsing technique i used because of null character.

any suggetions

thanks and regards

ranadheer.



Re: Visual C# General How to Enumerate StringBuilder object with null character as delimiter between strings

Geert Verhoeven

Hi,

Can you please post the code that you are using

Greetz,

Geert

Geert Verhoeven
Consultant @ Ausy Belgium

My Personal Blog






Re: Visual C# General How to Enumerate StringBuilder object with null character as delimiter between strings

Mattias Sjogren

Is the string returned from a call to native code




Re: Visual C# General How to Enumerate StringBuilder object with null character as delimiter between strings

ranadheer mac

thanks for your reply.

the code i used is as follows

**************///code for importing GetPrivateProfileSection() method///**************************

[DllImport("kernel32")]

private static extern int GetPrivateProfileString(string section,

string key, string def, StringBuilder retVal,

int size, string filePath);

**************///code for reading .ini file //**************************

StringBuilder temp = new StringBuilder(1024);

int i = GetPrivateProfileString("exch2000-gate", null, "", temp,

1024, "C:\\randhir\\fpadmin\\FPUserList.ini");

string str = temp.ToString();

my aim is to enumerate key names of a specified section "exch2000-gate". in my above code temp is stringbuilder conating of all key value names. you may advise to use GetPrivateProfileSection() method to retrieve key names and their values. but by giving "null" as a second parameter i can enumerate key names to a specified buffer temp.

i reffered the following link for my code.

http://msdn2.microsoft.com/en-us/library/ms724353.aspx

thanks and regards

ranadheer.





Re: Visual C# General How to Enumerate StringBuilder object with null character as delimiter between strings

ranadheer mac

and the code is to be executed when the window form is loaded.

i used the code in the following method

private void Form1_Load(object sender, EventArgs e)





Re: Visual C# General How to Enumerate StringBuilder object with null character as delimiter between strings

Mattias Sjogren

It's the CLR marshaler that cuts the string at the first null character. If you change the type from StringBuilder to char[] you should get the entire buffer back.






Re: Visual C# General How to Enumerate StringBuilder object with null character as delimiter between strings

ranadheer mac

thanks.

i changed StringBuilder to Byte[]. its working

regards

ranu





Re: Visual C# General How to Enumerate StringBuilder object with null character as delimiter between strings

Tolga Ercan

All,

I have a similar situation.  In my case it is a return value from a function that I am trying to run.  The returned string is supposed to be a set of null-delimited string values finally finished off with an additional null.  i.e. Value1<null>Value2<null>Value3<null><null>  Unfortunately, using Byte[] or char[] does not work for me.

The call is to an application API (DataStage 7.5.1a to be exact so if you know there is a .NET API wrapper for DataStage API, that would be even better :D) and the API documentation is not correct so I do not know the exact type that they used for the return value.  They copied and pasted another method's structure and forgot to update it.  Evil copy-paste!  In another online post I found someone saying the return is a char* but I'm not 100% sure.  Anyway, so I tried StringBuilder and string, both of which return only the first value.  If I use Byte[] or char[], I get an exception stating:  "Cannot marshal 'return value': Invalid managed/unmanaged type combination."

Any help would be greatly appreciated!

T.





Re: Visual C# General How to Enumerate StringBuilder object with null character as delimiter between strings

Tolga Ercan

Whoa!  After even more searching with ubiquitious search terms, I finally discovered the solution!  The solution is to use IntPtr as the return value and then get the pointed strings one at a time and incrementing the pointer until we cannot find anymore strings.  The details are at this link.  Hopefully this way someone else would have to search a few hours less! :D

http://www.dotnet247.com/247reference/msgs/12/63214.aspx