【发布时间】:2012-10-30 02:23:59
【问题描述】:
我有一个 C++ DLL (SimpleDLL.dll),它有一个公开的函数 (DllFunctionPoibnterGetName),它有一个函数指针 (getNameFP) .函数指针将 char * 作为参数 (*char * name*)。
// C++
DllExport void DllFunctionPoibnterGetName( void (*getNameFP) (char * name, unsigned short * length ) ) {
char name[1024];
unsigned short length = 0 ;
getNameFP( name, &length );
printf( "length=[%d] name=[%s]\n", length, name );
}
我有一个想要使用这个 C++ DLL 的 C# 应用程序。
// C#
public unsafe delegate void GetName( System.Char* name, System.UInt16* length);
unsafe class Program
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void delegateGetName(System.Char* name, System.UInt16* length);
[DllImport("SimpleDLL.dll", CharSet = CharSet.Ansi )]
public static extern void DllFunctionPoibnterGetName([MarshalAs(UnmanagedType.FunctionPtr)] delegateGetName getName);
static void Main(string[] args)
{
DllFunctionPoibnterGetName(GetName);
}
static void GetName(System.Char* name, System.UInt16* length)
{
// name = "one two three";
*length = 10;
}
}
目前我可以毫无问题地设置长度,但我似乎找不到正确设置名称的方法。
我的问题是
- 如何正确地将 char * 名称设置为一个值。
【问题讨论】: