如何使用extern" C" DLL函数以字符**在C#应用程序的参数? [英] How to use extern "C" dll function taking char** as an argument in C# application?
问题描述
我有一个功能DLL:
extern "C"
int
doJob(char** buffer);
及其与C ++使用情况是这样的:
Its usage with C++ looks like this:
char* buf;
int status = doJob(&buf);
我应该有什么的定义在C#中这个功能呢?
我怎样才能使用此功能在C#?
What definition should I have for this function in C#? How can I use this function in C#?
推荐答案
一个可能的模式是:
[DllImport("containsdojob.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 doJob(out IntPtr buffer);
[DllImport("containsdojob.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void freeMemory(IntPtr buffer);
和
IntPtr buffer = IntPtr.Zero;
string str = null;
try
{
doJob(out buffer);
if (buffer != IntPtr.Zero)
{
str = Marshal.PtrToStringAnsi(buffer);
}
}
finally
{
if (buffer != IntPtr.Zero)
{
freeMemory(buffer);
}
}
请注意,你需要一个 freeMemory
方法以释放 doJob
分配的内存。
Note that you'll need a freeMemory
method to free the memory allocated by doJob
.
有其他可能的模式,例如基于 BSTR
和 SysAllocString
更易于实现C#-side (但较难实现的C面)
There are other possible patterns, for example based on BSTR
and SysAllocString
that are easier to implement C#-side (but more difficult to implement C-side)
模式,使用BSTR:
C-端:
char *str = "Foo"; // your string
int len = strlen(str);
int wslen = MultiByteToWideChar(CP_ACP, 0, str, len, 0, 0);
BSTR bstr = SysAllocStringLen(NULL, wslen);
MultiByteToWideChar(CP_ACP, 0, str, len, bstr, wslen);
// bstr is the returned string
C# - 侧:
C#-side:
[DllImport("containsdojob.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 doJob([MarshalAs(UnmanagedType.BStr)] out string buffer);
string str;
doJob(out str);
该内存由CLR自动处理(释放)。
The memory is automatically handled (freed) by the CLR.
如果您使用Visual C ++,你甚至可以
If you are using Visual C++ you can even
char *str = "Foo"; // your string
_bstr_t bstrt(str);
BSTR bstr = bstrt.Detach();
// bstr is the returned string
或C端,你可以用它可以释放C# - 侧两个分配器之一:的 LocalAlloc 或<一个href=\"https://msdn.microsoft.com/en-us/library/windows/desktop/ms692727%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396\"相对=nofollow> CoTaskMemAlloc :
Or C-side you could use one of the two allocators that can be freed C#-side: LocalAlloc or CoTaskMemAlloc:
char *str = "Foo"; // your string
char *buf = (char*)LocalAlloc(LMEM_FIXED, strlen(str) + 1);
// or char *buf = (char*)CoTaskMemAlloc(strlen(str) + 1);
strcpy(buf, str);
// buf is the returned string
然后,可以使用第一个例子,但不是叫
Then you use the first example, but instead of calling
freeMemory(buffer);
您拨打:
Marshal.FreeHGlobal(buffer); // for LocalAlloc
或
Marshal.FreeCoTaskMem(buffer); // for CoTaskMemAlloc
这篇关于如何使用extern&QUOT; C&QUOT; DLL函数以字符**在C#应用程序的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!