title: Getting a UDT from a LongPtr in VBA
body: <p>I want to get a UDT that is kept as a pointer in the user data associated with a subclassed window.
  The UDT is declared as:</p>
  <pre><code>Type ID
   nPID As long
   hDefProc As LongPtr
  End Type
  
  Declare PtrSafe Function GetWindowLongPtr Lib &quot;user32&quot; Alias &quot;GetWindowLongA&quot; (ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
  </code></pre>
  <p><code>GetWindowLongPtr()</code> returns a LongPtr, and I can't seem to dereference that</p>
  <p>Inside my subclassed WindowProc I'm trying to access it like this</p>
  <pre><code>Dim pID As ID
  pID = GetWindowLongPtr(hWnd, GWLP_USERDATA) '&lt;- Err: Type mismatch
  </code></pre>
  <p>Is there a way to dereference this in vb6? If not, what are my alternatives to store data in a sub-classed windowclass?</p>
  <ul>
  <li><strong>EDIT :</strong></li>
  </ul>
  <p>I did a poor job at explaining. The problem I'm having boils down to this. I can't allocate an UDT on the heap and dereference it after: This is the full code:</p>
  <pre><code>Type IDT
      id As Long
      SomeOther As Long
  End Type
  
  Public Const HEAP_NO_SERIALIZE = &amp;H1
  Public Const HEAP_ZERO_MEMORY = &amp;H8
  
  Declare PtrSafe Sub CopyMemory Lib &quot;kernel32&quot; Alias &quot;RtlMoveMemory&quot; (Destination As Any, Source As Any, ByVal Length As LongPtr)
  Declare PtrSafe Function HeapAlloc Lib &quot;kernel32&quot; Alias &quot;HeapAlloc&quot; (ByVal hHeap As LongPtr, ByVal dwFlags As Long, ByVal dwBytes As LongPtr) As LongPtr
  Declare PtrSafe Function GetProcessHeap Lib &quot;kernel32&quot; Alias &quot;GetProcessHeap&quot; () As LongPtr
  
  Sub main()
     Dim SourceIDT As IDT
     Dim HeapIDT As LongPtr
     Dim TestIDT As IDT
  
     SourceIDT.id = 1234
     Debug.Print &quot;SourceIDT.id = &quot; &amp; SourceIDT.id ' returns 1234
  
     HeapIDT = HeapAlloc(GetProcessHeap(), HEAP_NO_SERIALIZE or HEAP_ZERO_MEMORY, LenB(SourceIDT))
     CopyMemory ByVal HeapIDT, VarPtr(SourceIDT), LenB(SourceIDT)
  
     CopyMemory VarPtr(TestIDT), ByVal HeapIDT, LenB(SourceIDT)
     Debug.Print &quot;TestIDT.id = &quot; &amp; TestIDT.id ' expected 1234, but getting 0
     ' to do: free
  End Sub
  </code></pre>
  <p>&quot;Dereferencing&quot; it like that always yields garbage or zero. What am I doing wrong? It's a VBA6 macro in Excel x86 if that matters.</p>