How do I handle null or optional dll struct parameters in C#

南楼画角 提交于 2020-01-08 14:36:15

问题


How do I deal with optional struct arguments in dll methods called from C# using pinvoke? For example, the lpSecurityAttributes parameter here should be passed null when absent.

The correct way of passing struct's seems to be using ref, but it cannot have optional parameters, or take null in general.

What ways are there to achieve this?


回答1:


You have a few options

1) Use a class instead of a struct

I think this method is the easiest. Simply declare the struct as a class:

[StructLayout(LayoutKind.Sequential)]
public class CStruct
{
    //member-list
}

and then declare your method:

[DllImport("mydll.dll", OptionName = optionValue, ...)]
static extern int DLLFunction(CStruct cStruct, ...);

If your optional parameter happens to be the last one, you can instead use CStruct cStruct = null as the parameter. This allows you to exclude it instead of passing null explicitly. You can also write a wrapper method that uses this and ensures the optional parameters come last.

2) Use IntPtr and IntPtr.Zero

Use a struct:

[StructLayout(LayoutKind.Sequential)]
public struct CStruct
{
    //member-list
}

and declare your method as:

[DllImport("mydll.dll", OptionName = optionValue, ...)]
static extern int DLLFunction(IntPtr cStruct, ...);

In the non-null case, marshal the struct to a pointer and call the method:

IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CStruct)));
try{
    Marshal.StructureToPtr(myCStruct, ptr, false);
    DLLFunction(ptr, ...);
} finally {
    Marshal.FreeHGlobal(ptr);
}

In the null case, call the method with IntPtr.Zero:

DLLFunction(IntPtr.Zero, ...);

Again, you can make this parameter optional if this happens to be the last in the list (or you use a wrapper to make it so). Do this by using IntPtr cStruct = default(IntPtr) as the parameter. (As default(IntPtr) creates a IntPtr.Zero.)

3) Overload your method to avoid marshaling

Use a struct as in 2).

Simply declare one option for the non-null case:

[DllImport("mydll.dll", OptionName = optionValue, ...)]
static extern int DLLFunction(ref cStruct, ...);

and another for the null case:

[DllImport("mydll.dll", OptionName = optionValue, ...)]
static extern int DLLFunction(IntPtr cStruct, ...);

The first method will automatically get called when passing a struct, and the second when passing IntPtr.Zero. If declaring the IntPtr version with an optional parameter (as shown at the bottom of 2) above), it will automatically call it when you exclude the cStruct parameter.

4) Raw pointers using unsafe

Use a struct as in 2) and declare your method (note the unsafe keyword):

[DllImport("mydll.dll", OptionName = optionValue, ...)]
static unsafe extern int DLLFunction(CStruct* cStruct, ...);

In the non-null case, you pass &myCStruct, and simply null in the null case. As in 1), if this optional parameter is last, you can declare the parameter as CStruct* cStruct = null to automatically pass null when cStruct is excluded.

Thanks to @dialer for suggesting this method.



来源:https://stackoverflow.com/questions/47997942/how-do-i-handle-null-or-optional-dll-struct-parameters-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!