How do I handle null or optional DLL struct parameters
Asked Answered
R

1

21

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?

Robinetta answered 27/12, 2017 at 19:58 Comment(0)
R
22

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.

Robinetta answered 27/12, 2017 at 19:58 Comment(1)
You can also (read: my personal favorite is) declare the P/Invoke signature using unsafe pointers. static unsafe extern int DLLFunction(TheStruct* struct, ...);. This has several advantages. You can use an actual value type instead of a reference type (relevant if stack alloc performance matters), can pass null, don't need another overload, no marshaling (actually enforces it to be blittable even, which in turn increases performance again) and it's type-safe (unlike IntPtr). The obvious downside is that you have to use unsafe (though to be fair using IntPtr is not any safer).Towne

© 2022 - 2024 — McMap. All rights reserved.