Platform SDK: DirectX

Accessing Direct3D

[C++]

When a Direct3D application written in C++ starts up, it must obtain a pointer to an IDirect3D7 interface to access the latest Direct3D functionality. Use the following steps to obtain a pointer to the IDirect3D7 interface.

To obtain a pointer to the IDirect3D7 interface

  1. Call DirectDrawCreateEx to create a DirectDraw device. Applications must use the DirectDrawCreateEx function to create a DirectDraw object capable of supporting IDirect3D7; a DirectDraw object created by the legacy DirectDrawCreate function does not support the IDirect3D7 interface.
  2. Call the IUnknown::QueryInterface method to get a pointer to an IDirect3D7 interface.

The following illustration shows these steps and relates this process to the one required for legacy applications.

Note  As shown in the preceding figure, the DirectDraw component is comprised of two COM objects. The newest object—called DirectDraw7 and created by calling the DirectDrawCreateEx function—is the only object that exposes the IDirect3D7 interface. The DirectDraw7 object does not expose legacy Direct3D interfaces. Applications that require previous iterations of the Direct3D must use the DirectDrawCreate method to create a DirectDraw object, and then query for a legacy interface.

The following code fragment demonstrates how to create a DirectDraw7 object and query for an IDirect3D7 interface.

LPDIRECTDRAW7   lpDD;               // IDirectDraw7 Interface
LPDIRECT3D7     lpD3D;              // IDirect3D7 Interface
HRESULT hr;
 
// Get an IDirectDraw7 interface.
// Use the current display driver.
hr = DirectDrawCreateEx (NULL, (void **)&lpDD, IID_IDirectDraw7, NULL); 
if (FAILED (hr))
{
    // Code to handle an error goes here.
}
 
// Get D3D interface
hr = lpDD->QueryInterface (IID_IDirect3D7, (void **)&lpD3D);
if (FAILED (hr))
{
    // Code to handle the error goes here.
}
[Visual Basic]

When a Direct3D application written in Visual Basic starts up, it must obtain a reference to the Direct3D7 class to access Direct3D functionality. Use the following steps to obtain such a reference.

To obtain a reference to the Direct3D7 class

  1. Call DirectX7.DirectDrawCreate to create a DirectDraw7 class object.
  2. Call the DirectDraw7.GetDirect3D method to retrieve the DirectDraw object's reference to the Direct3D7 class.

The following illustration shows these steps.

The following code fragment demonstrates how to retrieve the Direct3D7 class from a DirectDraw7 class object.

' Retrieve a reference to the Direct3D7 class.
'
' For this example, dx is a valid reference to the DirectX7 class.
On Local Error Resume Next
Dim ddraw As DirectDraw7
Dim d3d As Direct3D7
 
' Create a default DirectDraw7 object.
Set ddraw = dx.DirectDrawCreate("")
If (Err.Number <> DD_OK) Then
    ' Handle error.
End If
 
' Retrieve the Direct3D7 reference from the DirectDraw7 object.
Set d3d = ddraw.GetDirect3D
If (Err.Number <> DD_OK) Then
    ' Handle error.
End If