title: Interop question about passing a struct by ref, null pointer and IntPtr
body: <p>Colleagues,</p>
  
  <p>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++.</p>
  
  <p>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 <code>null</code> pointer.  2nd time I need to pass a pointer to an instance of the struct.</p>
  
  <p>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:</p>
  
  <pre><code>[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
     static extern bool foo(ref NEEDED_STRUCT_TYPE sDataStruct);</code></pre>
  
  <p>I like it, because it's strongly typed for the <code>NEEDED_STRUCT_TYPE</code>.  But to be able to pass a null pointer, I had to change the declaration to</p>
  
  <pre><code>[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
     static extern bool foo(IntPtr sDataStruct);</code></pre>
  
  <p>Now I can pass <code>IntPtr.Zero</code> for <code>null</code> 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?</p>
  
  <p>Any suggestion, insight or reference is really appreciated!</p>
  
  <p>Cheers, <br>
  - Nick</p>