C#代码到C世界函数的调用
问题描述:
我有点怀疑,
我正在从C#托管世界到C托管代码库进行函数调用.
C#代码
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代码
bool GetFormattedString(字符* str,整数*大小){
..
..
strcpy(str,result);
* size = strlen(str);
return(* size> 0); //0或1的某些结果
}
我不知道或不想设置StringBuilder的大小,哪种类型的字符串最适合通过引用传递.(性能明智)
任何想法或经验都将不胜感激.
谢谢,
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,
答
hummm不能将StringBuilder用作指针,请参考MSDN中的StringBuilder类定义,但是您可以将字符串生成器的内容转换为使用ToString()方法的String对象,简单地将Len设置为String对象,将赋予u您要查找的大小.
祝你好运
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