Click here to Skip to main content
14,580,625 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have small doubt,
I am having function call from C# managed world to C unmanaged code base..

C# Code
Boolean ChsarpFunction(StringBuilder sb) {
int size;

return GetFormattedString(sb,size);
}

[DllImport(MYDLL, CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false)]
[return: MarshalAs(UnmanagedType.U1)]
... Boolean GetFormattedString( [MarshalAs(UnmanagedType.LPStr)] StringBuilder value, int size);


C Code
bool GetFormattedString (char *str, int* size) {
..
..
strcpy(str,result);
*size = strlen(str);
return (*size > 0); // some result of 0 or 1
}

I don't know or want to set the size for the StringBuilder, What kind of string is optimal to pass by reference.(performance wise)
Any ideas or experience is much appreciated.

Thanks,
Posted
Updated 20-Jan-12 8:22am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

hummm the StringBuilder can't be tratead as pointer, refer to the StringBuilder Class definition in MSDN, but you can convert the string builder content to a String Object with the ToString() Method, a simply Len to the String Object will give u the size that u're finding.

Good Luck
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100