title: Different D3D9 devices dont share the same VTable address
body: <p>Lately I've been messing around with VTables and virtual pointers,
  and I've found that every vptr from the same class type (<em>should</em>) point to the same VTable.(They point to the same address).</p>
  
  <p>And I've searched for information about how COM objects work, and I've found that they also share the same VTable.(Objects of the same class ofc ) </p>
  
  <p><a href="https://www.codeproject.com/articles/153096/intercepting-calls-to-com-interfaces]" rel="nofollow noreferrer">https://www.codeproject.com/articles/153096/intercepting-calls-to-com-interfaces]</a></p>
  
  <blockquote>
    <p>First of all, when you set a method hook, it will work not only for the current instance of the COM object. It’ll work for all objects of the same class (but not for all the classes implementing the interface hooked).</p>
  </blockquote>
  
  <p>The problem I have is as follows: I created in a test app  3 different d3d9 devices and I checked their pointer to the VTable but they are not the same!</p>
  
  <pre><code> if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
      return E_FAIL;
  if (NULL == (g_pD3D2 = Direct3DCreate9(D3D_SDK_VERSION)))
      return E_FAIL;
  D3DPRESENT_PARAMETERS d3dpp;
  ZeroMemory( &amp;d3dpp, sizeof( d3dpp ) );
  d3dpp.Windowed = TRUE;
  d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  D3DPRESENT_PARAMETERS d3dpp2;
  ZeroMemory(&amp;d3dpp2, sizeof(d3dpp2));
  d3dpp2.Windowed = TRUE;
  d3dpp2.SwapEffect = D3DSWAPEFFECT_DISCARD;
  d3dpp2.BackBufferFormat = D3DFMT_UNKNOWN;
     if( FAILED( g_pD3D-&gt;CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                    D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                    &amp;d3dpp, &amp;g_pd3dDevice ) ) )
  {
      return E_FAIL;
  }
  if (FAILED(g_pD3D-&gt;CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
      &amp;d3dpp2, &amp;g_pd3dDevic)))
  {
      return E_FAIL;
  }
  WNDCLASSEX wc = { sizeof(WNDCLASSEX),CS_CLASSDC,TempWProc,0L,0L,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,(LPCWSTR)("1"),NULL };
  RegisterClassEx(&amp;wc);
  HWND hWndd = CreateWindow((L"1"), NULL, WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), NULL, wc.hInstance, NULL);
  if (FAILED(g_pD3D2-&gt;CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWndd,
      D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_DISABLE_DRIVER_MANAGEMENT,
      &amp;d3dpp2, &amp;g_pd3dDevic3)))
  {
      return E_FAIL;
  }
  </code></pre>
  
  <p>(As a last resort I also created a different window and initialized a new D3D Interface just in case it would make the new d3d9 device point to the same VTable.)</p>
  
  <p>In the debugger I checked the values of the device's pointers and this is what they yield: </p>
  
  <pre><code>g_pd3dDevice(The first one) : __vfptr = 0x00b75d1c { 0x6a976f19 }
  g_pd3dDevic(The second one) : __vfptr = 0x0512a75c { 0x6a976f19 }
  g_pd3dDevic3(The third one) : __vfptr = 0x0524393c { 0x6a976f19 }
  </code></pre>
  
  <p>The 3 VTables are the same, but they are in different places. (The three pointers point to the same function)
  (And I also checked and the VTable is offseted by 0x2D9C from the object)
  So this is what I dont really understand:</p>
  
  <ul>
  <li>Do D3D9 devices <strong>always</strong> use the same VTable?</li>
  <li><p>Why don't they use the same VTable as COM objects do as pointed by the link I've given?</p></li>
  <li><p>Is it a problem related to having more than one device in the same HWND?</p></li>
  <li>Is it some compiler optimization- related thingie?</li>
  <li>Are they really VTables?</li>
  </ul>
  
  <p>P.S: Sorry for my poor English, and my way of explaining things :C</p>
  
  <p>Thanks a lot for reading this wall of text, seriously, thanks.</p>
  
  <p>Thanks in advance!!!
  (And sorry if it is a really dumby question)</p>