void ** 处理 P/Invoke

void ** handles with P/Invoke

我正在使用来自第 3 方供应商的 C API DLL。我遇到的问题是我似乎无法找到一个好的模板来编组以下 C 代码:

API_Open( void ** handle );
API_Close( void * handle );

调用已简化,但句柄是一个 void *,它(在 C 中)作为 &handle 传递给 API_Open 调用,然后作为句柄传递给 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));

    // Call our function.  
    rc = API_Open(out voidptrptr);

    // In theory, the value that voidptrptr points to should be the 
    // RAM address of our handle.
    handle = Marshal.ReadInt32( Marshal.ReadIntPtr(voidptrptr) );

    return rc;
}

public static int Wrapper_API_Close(ref Int32 handle)
{
    int rc = SUCCESS;

    // Get a 32bit region to act as our void *
    IntPtr voidptr = Marshal.AllocHGlobal(sizeof(Int32));

    // Write the handle into it.
    Marshal.WriteInt32(voidptr,handle);

    // Call our function.
    rc = API_Close(voidptr);

    return rc;
}


public void SomeRandomDrivingFunction()
{
    .
    .
    .
    Int32 handle;
    Wrapper_API_Open( ref handle );
    .
    .
    .
    Wrapper_API_Close( ref handle );
    .
    .
    .
}

当我调用 API_Close 时,API return 代码总是 INVALID_DEVICE_OBJECT。有什么想法吗?我认为这会非常简单,但我无法理解函数调用的 void** 和 void* 部分。

谢谢

你似乎把这个问题复杂化了。我不知道为什么要为句柄引入 Int32,因为它们确实需要指针大小。你应该使用 IntPtr

API_Open接受返回句柄的变量地址。调用者分配该变量并将其传递给被调用者,后者填充该变量。 C 函数可能如下所示:

int API_Open(void **handle)
{
    *handle = InternalCreateHandle();
    return CODE_SUCCESS;
}

你可以在 C 中这样称呼它:

void *handle;
int retval = API_Open(&handle);
if (retval != CODE_SUCCESS)
    // handle error
// go one and use handle

翻译成 C#,void* 映射到 IntPtr,双指针的使用只是绕过 C 只支持按值传递这一事实的一种手段。在 C# 中,您将使用按引用传递。

对于 API_Close 它更简单,因为我们按值传递句柄:

int API_Close(void *handle)
{
    InternalCloseHandle(handle);
    return CODE_SUCCESS;
}

调用代码很简单:

int retval = API_Close(handle);
if (retval != CODE_SUCCESS)
    // handle error

因此,C# 包装函数应该是:

public static int Wrapper_API_Open(out IntPtr handle)
{
    return API_Open(out handle);
}

public static int Wrapper_API_Close(IntPtr handle)
{
    return API_Close(handle);
}

此时这些包装方法看起来确实有些毫无意义!