title: VC++ Compiler Can't Catch HANDLE and PHANDLE Type Mismatch
body: <p>I spent a whole day looking for a bug caused by wrongly passing Windows PHANDLE type to a function expecting HANDLE!!!  I was expecting the VC++ 2010 compiler to catch such a simple and obvious type mismatch error.  However, it didn't.  It just didn't.  </p>
  
  <p>On a closer look, HANDLE is defined as void pointer in winnt.h and so PHANDLE is just void pointer pointer.  Since anything can be passed to void * or void ** so PHANDLE and HANDLE mismatch can't be warned by the compiler.</p>
  
  <p>Is there anyway to avoid such a problem.  I don't know how many other Windows types are typedef'ed as void *.  Are there any strategies to avoid such errors?</p>
  
  <p>For example, the following compiled without any error in VC++ 2010, although the function is called by wrong pointer types. And, it is not obvious the pitfall is there unless one knows HANDLE (or any other Windows types) is a void pointer:</p>
  
  <pre><code>void f1 (HANDLE h) {
    printf("%x",h);
  }
  
  int _tmain(int argc, _TCHAR* argv[])
  {
    PHANDLE ph=NULL;
    int c=0;
  
    f1(ph);
    f1(&amp;c);
    return 0;
  }
  </code></pre>