title: Correct way to marshal SIZE_T*?
body: <p>I have the following C++ function definition, which I am trying to call through PInvoke from managed code:</p>
  
  <pre><code>bool FooBar(SIZE_T* arg1);
  </code></pre>
  
  <p>My managed declaration looked as follows:</p>
  
  <pre><code>[DllImport("mydll", SetLastError=true, CharSet=CharSet.Unicode)]
  private static extern bool FooBar(ref uint arg1);
  </code></pre>
  
  <p>Some of you may notice the same bug I eventually did. This is not 64bit portable. SIZE_T is of variable size (32-64 bit) as is the pointer to it. On the managed size the pointer correctly translates to 64bit, but the uint does not, and you can end up with trash in the upper bits of arg1. This was an especially persistent error since the trash was often just zeros :(</p>
  
  <p>The only solution I have gotten to work is the following managed declaration:</p>
  
  <pre><code>[DllImport("mydll", SetLastError=true, CharSet=CharSet.Unicode)]
  private static extern bool FooBar(ref IntPtr arg1);
  </code></pre>
  
  <p>This works of course because IntPtr can vary its size correctly. In my code I just treat the IntPtr as an integer, and it works, though it looks like an ugly hack. It seems to me there should be some way to specify this properly, perhaps using UnmanagedType.SysUInt, but I have been unable to come up with any other working solution.</p>