[英]void ** handles with P/Invoke
我正在使用來自第三方供應商的C API DLL。 我的問題是,我似乎找不到用於編組以下C代碼的好的模板:
API_Open( void ** handle );
API_Close( void * handle );
調用得到了簡化,但句柄是一個空*,(在C中)作為&handle傳遞給API_Open
調用,然后作為句柄傳遞給API_Close
。
我試圖在C#中執行相同的操作,但無法弄清楚如何正確處理元帥。 我的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返回代碼始終為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);
}
在這些時候,這些包裝器方法看起來毫無意義!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.