Colleagues,
Preamble. My question is more about best practices. I know one workaround. This is the first time I have to deal with interop in C#, at the same time I’ve written a fair amount of code in C and C++.
I need to invoke 2 times a function exposed by an unmanaged DLL. The function takes a pointer to a struct as an argument. 1st time I need to pass a null
pointer. 2nd time I need to pass a pointer to an instance of the struct.
The function has 6 parameters, so below are simplified declarations that work for the purposes of this question. (I can post the specifics if anyone is interested.) Here’s the 1st variant of the declaration:
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool foo(ref NEEDED_STRUCT_TYPE sDataStruct);
I like it, because it's strongly typed for the NEEDED_STRUCT_TYPE
. But to be able to pass a null pointer, I had to change the declaration to
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool foo(IntPtr sDataStruct);
Now I can pass IntPtr.Zero
for null
pointer, but the parameter is no longer strongly typed. In the given situation, is there a way to have both: strongly typed parameter and the ability to pass null pointer?
Any suggestion, insight or reference is really appreciated!
Cheers,
- Nick