This question already has an answer here:
这个问题在这里已有答案:
- How do I handle null or optional dll struct parameters in C# 1 answer
如何在C#1答案中处理null或可选的dll struct参数
I have a few p/invoked functions (but I'm rewriting my code at the moment so I'm tidying up) and I want to know how to use/pass a nullable type as one of the parameters. working with int types isn't a problem but given the following:
我有几个p /被调用的函数(但我现在正在重写我的代码所以我正在整理)我想知道如何使用/传递一个可空类型作为参数之一。使用int类型不是问题,但给出以下内容:
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, int? enumerator, IntPtr hwndParent, uint Flags);
I'd like to be able to pass the Guid
parameter as a nullable type. As it stands at the moment I can call it as:
我希望能够将Guid参数作为可空类型传递。就目前而言,我可以将其称为:
SetupDiGetClassDevs(ref tGuid, null, IntPtr.Zero, (uint)SetupDiFlags.DIGCF_PRESENT );
but I need the first parameter to also be passable as null
.
但我需要第一个参数也可以作为null传递。
1 个解决方案
#1
It's not possible to pass a Nullable type into a PInvoke'd function without some ... interesting byte manipulation in native code that is almost certainly not what you want.
如果没有一些......本机代码中有趣的字节操作几乎肯定不是你想要的,那么就不可能将Nullable类型传递给PInvoke'd函数。
If you need the ability to pass a struct value as NULL to native code declare an overload of your PInvoke declaration which takes an IntPtr in the place of the struct and pass IntPtr.Zero
如果你需要能够将结构值作为NULL传递给本机代码,则声明PInvoke声明的重载,该声明将IntPtr替换为结构并传递IntPtr.Zero
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, ref int enumerator, IntPtr hwndParent, uint Flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr enumerator, IntPtr hwndParent, uint Flags);
Note: I added a ref class to the first signature. If the native signature can take NULL, it is likely a pointer type. Hence you must pass value types by reference.
注意:我在第一个签名中添加了一个ref类。如果本机签名可以为NULL,则可能是指针类型。因此,您必须通过引用传递值类型。
Now you can make calls like the following
现在您可以拨打以下电话
if (enumerator.HasValue) {
SetupDiGetClassDevs(someGuid, ref enumerator.Value, hwnd, flags);
} else {
SetupDiGetClassDevs(someGuid, IntPtr.Zero, hwnd, flags);
}
#1
It's not possible to pass a Nullable type into a PInvoke'd function without some ... interesting byte manipulation in native code that is almost certainly not what you want.
如果没有一些......本机代码中有趣的字节操作几乎肯定不是你想要的,那么就不可能将Nullable类型传递给PInvoke'd函数。
If you need the ability to pass a struct value as NULL to native code declare an overload of your PInvoke declaration which takes an IntPtr in the place of the struct and pass IntPtr.Zero
如果你需要能够将结构值作为NULL传递给本机代码,则声明PInvoke声明的重载,该声明将IntPtr替换为结构并传递IntPtr.Zero
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, ref int enumerator, IntPtr hwndParent, uint Flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr enumerator, IntPtr hwndParent, uint Flags);
Note: I added a ref class to the first signature. If the native signature can take NULL, it is likely a pointer type. Hence you must pass value types by reference.
注意:我在第一个签名中添加了一个ref类。如果本机签名可以为NULL,则可能是指针类型。因此,您必须通过引用传递值类型。
Now you can make calls like the following
现在您可以拨打以下电话
if (enumerator.HasValue) {
SetupDiGetClassDevs(someGuid, ref enumerator.Value, hwnd, flags);
} else {
SetupDiGetClassDevs(someGuid, IntPtr.Zero, hwnd, flags);
}