我正在使用来自第 3 方供应商的 C API DLL。我遇到的问题是,我似乎找不到用于编组以下 C 代码的好模板:
API_Open( void ** handle );
API_Close( void * handle );
Run Code Online (Sandbox Code Playgroud)
调用被简化了,但句柄是一个 void *,它(在 C 中)API_Open作为 &handle传入调用,然后API_Close作为句柄传入。
我尝试在 C# 中做同样的事情,但无法弄清楚如何正确处理 Marshal。我的 C# 版本(最新尝试)是:
[DllImport("External.dll",EntryPoint="API_Open")]
public static extern int API_Open( out IntPtr handle );
[DllImport("External.dll",EntryPoint="API_Close")]
public static extern int API_Close( IntPtr handle );
public static int Wrapper_API_Open( ref Int32 handle )
{
int rc = SUCCESS;
// Get a 32bit region to act as our void**
IntPtr voidptrptr = Marshal.AllocHGlobal(sizeof(Int32)); …Run Code Online (Sandbox Code Playgroud)