title: The right type for handles in C interfaces
body: <p>I'm creating a C api that hides some functionality in a DLL file.</p>
  
  <p>Since everything is C++ on the inside most of the functions works against handles which maps directly to this pointers on the inside of the API.</p>
  
  <p>To get a some degree of type safety for those handles I define them like this:</p>
  
  <pre><code>typedef struct MyType1* MyType1Handle;
  typedef struct MyType2* MyType2Handle;
  </code></pre>
  
  <p>I don't actually define MyType1 or MyType2 at any place since I only use them as pointers and do a type cast on the inside of the api to the actual pointer type. </p>
  
  <p>My problem is that when using my library in a clr project in visual studio I get this <code>warning: unresolved typeref token (token) for 'type'; image may not run.</code></p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/h8027ys9(VS.80).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/h8027ys9(VS.80).aspx</a></p>
  
  <p>It's no big deal since it works, but it looks unprofessional.</p>
  
  <p>I don't like using void*:</p>
  
  <pre><code>typedef void* MyType1Handle;
  typedef void* MyType2Handle;
  </code></pre>
  
  <p>This makes it possible to call a function wanting a MyType1Handle with a MyType2Handle since they are actually the same type.</p>
  
  <p>Another approach I don't want to use is something like this</p>
  
  <pre><code>typedef int MyType1Handle;
  typedef int MyType2Handle;
  </code></pre>
  
  <p>This would work fine as long as int's and pointers have the same size, but that's not always the case and it seems like there is no foolproof way of getting a platform specific pointer sized integer. It has the same type safety problems as the void* as well.</p>
  
  <p>Another approach I tried was to do it like this:</p>
  
  <pre><code>struct MyType1{};
  typedef struct MyType1* MyType1Handle;
  </code></pre>
  
  <p>This didn't work in C since empty structs is invalid C code. I could of course extend my struct with a dummy member, but it seems like there should be a better way of doing this.</p>
  
  <p>So my question boils down to:</p>
  
  <p>How do you generally specify this kind of types in the most compatible way?</p>