Thread overview
DirectX, COM and Access Violation
Feb 05, 2007
Henrik Eneroth
Feb 05, 2007
Don Clugston
Feb 05, 2007
Lionello Lunesu
February 05, 2007
Hello there,

I've been trying to get Direct3D to work with D (or vice versa if you will) and keep running into some problems.
I checked around for translations of the DX headers and I found that existing efforts can vary a lot in implementation. So I tried to translate them myself and reduce dependencies to the standard library only. However, in the world of COM, nothing is what it seems... :)

At the present moment I've translated d3d9, d3d9types and d3d9caps, the minimum to get DX running it seems. I get some errors however.

Whenever I to call a DX function, I get an error saying "Access Violation". someone suggested that CoInitialize has to be called first, but that does not solve it. I looked around ( codesearch.google.com, lang:d ) and found a few different implementations. One of them differed from mine in a significant way.


This is what mine looks like (truncated to be readable):


Main file:
//-------------------//

IDirect3D9 * d3d;    // the pointer to our Direct3D interface IDirect3DDevice9 * d3ddev;    // the pointer to the device class


bit initD3D(HWND hWnd)
{
    D3DDISPLAYMODE d3ddm;

    d3d = Direct3DCreate9(D3D_SDK_VERSION);
    assert(d3d);

    d3d.GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm); // Access Violation

    D3DPRESENT_PARAMETERS d3dpp;

    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT.D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFORMAT.D3DFMT_UNKNOWN;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFORMAT.D3DFMT_D16;


    d3d.CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev ); // Access violation if GetAdapterDisplayMode is commented out

    return true;
}

D3D9.d file:
//-------------------//
extern (Windows)
{
    export IDirect3D9 * Direct3DCreate9(UINT SDKVersion);

    interface IDirect3D9 : public IUnknown
    {

        /* ... */

        HRESULT CreateDevice(   UINT Adapter,D3DDEVTYPE DeviceType, HWND hFocusWindow,DWORD BehaviorFlags,D3DPRESENT_PARAMETERS* pPresentationParameters,IDirect3DDevice9** ppReturnedDeviceInterface    );
    }

    /* ... */
}



This is essentially the other implementation:


Main file:
//-------------------//

IDirect3D9 d3d;    // Notice that this is not a pointer anymore IDirect3DDevice9 d3ddev;    // Neither is this


bit initD3D(HWND hWnd)
{
    D3DDISPLAYMODE d3ddm;

    d3d = Direct3DCreate9(D3D_SDK_VERSION);
    assert(d3d);

    d3d.GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm); // No Access Violation anymore

    D3DPRESENT_PARAMETERS d3dpp;

    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT.D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFORMAT.D3DFMT_UNKNOWN;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFORMAT.D3DFMT_D16;


    d3d.CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev ); // Neither here

    return true;
}

D3D9.d file:
//-------------------//
extern (Windows)
{
    export IDirect3D9 Direct3DCreate9(UINT SDKVersion);

    interface IDirect3D9 : public IUnknown
    {

        /* ... */

        HRESULT CreateDevice(   UINT Adapter,D3DDEVTYPE DeviceType, HWND hFocusWindow,DWORD BehaviorFlags,D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DDevice9* ppReturnedDeviceInterface    ); // Notice that ppReturnedDeviceInterface is a simple pointer here
    }

    /* ... */
}



Now the interesting thing is that if I try the other implementation, I do not get an access violation. However, I also get a blank window with nothing in it. D3D fails to even try to initialize it seems. I tried using the reference device instead of HAL, but the access violations remain even then suggesting there is some holy COM law I am continously violating or something.
I'm lost as to how to troubleshoot this. Any ideas?

By the way, I include these libs:
pragma (lib, "devrun\\x86\\d3d9d.lib"); // implib'd from d3d9d.dll
pragma (lib, "\\dm\\lib\\gdi32.lib");
pragma (lib, "\\dm\\lib\\uuid.lib");;
February 05, 2007
Henrik Eneroth wrote:
> Hello there,
> 
> I've been trying to get Direct3D to work with D (or vice versa if you will) and keep running into some problems.

[snip]
> By the way, I include these libs:
> pragma (lib, "devrun\\x86\\d3d9d.lib"); // implib'd from d3d9d.dll
> pragma (lib, "\\dm\\lib\\gdi32.lib");
> pragma (lib, "\\dm\\lib\\uuid.lib");;

FYI: This _could_ be a problem. The libs that come with DMD are *ancient*. You should run coffimplib on the latest Microsoft libs to create up-to-date ones.

February 05, 2007
> IDirect3D9 * d3d;    // the pointer to our Direct3D interface IDirect3DDevice9 * d3ddev;    // the pointer to the device class

Try without the "*", interfaces are always pointers (like classes).

L.


February 06, 2007
"Lionello Lunesu" <lionello@lunesu.remove.com> wrote in message news:eq7tnr$1rkn$1@digitaldaemon.com...
>> IDirect3D9 * d3d;    // the pointer to our Direct3D interface IDirect3DDevice9 * d3ddev;    // the pointer to the device class
>
> Try without the "*", interfaces are always pointers (like classes).

That's right.  Additionally, you might want to check out the JEDI DirectX libs at http://www.clootie.ru/.  They're the ones I've always used and they work pretty well.

In fact, I'll attach my (not necessarily complete) D3D9/D3DX9 D header file. It includes a fair amount of the D3D interfaces and D3DX interfaces/functions.  And I'll put the .def file that I use with the Clootie libs as well.  Keep in mind that this header is for the April 2006 DX9 SDK so it's a little out of date.. but you can get those libs from Clootie at http://www.clootie.ru/delphi/DX92/d3dx9_29_dll.zip .



February 06, 2007
"Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:eq8knd$30ah$1@digitaldaemon.com...

> Clootie at http://www.clootie.ru/delphi/DX92/d3dx9_29_dll.zip .

Oops, that link is only for the d3dx9_29.dll file.  I guess you can't get the old SDK libs anymore.  Well, you can give the October 2006 ones a shot..!