use*_*059 5 c# null interop parameter-passing value-type
我正在使用C#来调用DLL函数.
[DllImport("MyDLL.dll", SetLastError = true)]
public static extern uint GetValue(
pHandle handle,
ref somestruct a,
ref somestruct b);
Run Code Online (Sandbox Code Playgroud)
如何传递null参数3 的参考?
当我尝试时,我收到编译时错误:
无法转换
<null>为ref somestruct.
我也试过了IntPtr.Zero.
您有两种选择:
创建somestruct一个类,并将函数签名更改为:
[DllImport("MyDLL.dll", SetLastError = true)]
public static extern uint GetValue(
pHandle handle, somestruct a, somestruct b);
Run Code Online (Sandbox Code Playgroud)
通常,这不能改变任何东西,除了你可以传递一个null作为价值a和b.
为函数添加另一个重载,如下所示:
[DllImport("MyDLL.dll", SetLastError = true)]
public static extern uint GetValue(
pHandle handle, IntPtr a, IntPtr b);
Run Code Online (Sandbox Code Playgroud)
现在你可以调用函数IntPtr.Zero,除了一个ref类型的对象somestruct:
GetValue(myHandle, ref myStruct1, ref myStruct2);
GetValue(myHandle, IntPtr.Zero, IntPtr.Zero);
Run Code Online (Sandbox Code Playgroud)