Hey All

I am trying to create a DLL to handle some directx for me. But it seems to be acting up. If you take a look at the picture below you will see that the LPDIRECT3DDEVICE9 d3ddev; pointer is not being created? While at the same time, the D3DPRESENT_PARAMETERS d3dpp; is.

http://www.exsolved.com/media/d3d.jpg

The directx header is being included at the top of the file like so:

#include <d3d9.h>
#pragma comment (lib, "d3d9.lib")

Everything else directx wise works except for

LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;

Is there a reason LPDIRECT3DDEVICE9 d3ddev; & LPDIRECT3D9 d3d; are not being created?

Thanks a ton.

You're just creating a pointer variable without actually setting it to anything, so what you're getting is 'worse' than a null pointer, it's a pointer to some random location. The message in the screenshot is caused by the debugger trying to treat whatever's at that random location as a direct3d device, which it almost certainly isn't.

You have to call a function (and I don't remember which one) to actually create and return a direct3d device, then store a pointer to it in a LPDIR...CE9 variable.

Are you following a tutorial or trying to assume/guess how to do things? I would suggest looking at some working source code that uses the API before trying to use it yourself.

Oh, and the reason that a D3DPRESENT_PARAMETERS object 'just works' is that the D3DPRESENT_PARAMETERS is a value (structure) type not a pointer type.

Learn this distinction ASAP.

No, I believe I am setting it to something, but during that setup its failing. Here's the exact function taken straight out the DLL source code:

extern "C" e2api void initDirectX(HWND &handle, int windowed, int width, int height)
	{
		d3d = Direct3DCreate9(D3D_SDK_VERSION);

		D3DPRESENT_PARAMETERS d3dpp;

		LPDIRECT3D9 d3d;
		LPDIRECT3DDEVICE9 d3ddev;

		ZeroMemory(&d3dpp, sizeof(d3dpp));
		d3dpp.Windowed = windowed;
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		d3dpp.hDeviceWindow = handle;

		d3d->CreateDevice(D3DADAPTER_DEFAULT,
						  D3DDEVTYPE_HAL,
						  handle,
						  D3DCREATE_SOFTWARE_VERTEXPROCESSING,
						  &d3dpp,
						  &d3ddev);

		d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
		d3ddev->BeginScene();		
		d3ddev->EndScene();
		d3ddev->Present(NULL, NULL, NULL, NULL);
	}

However, it keeps crashing everytime d3ddev is called... usually crashing at the line:

d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);

So I added a breakpoint just above that line, and when hovering over d3ddev, it says "iUnknown {...}".

Any ideas mate?

Thanks

Does this have something to do with the fact that I am calling DirectX from within a DLL? I have never worked with DLL's before so I am assuming things. Other than that, are you allowed to create/use variables within a DLL?

Ah ok. There shouldn't be any problem with doing what you're doing from a DLL. I have only ever written DX applications that are staticly linked and DLLs that don't do anything DX, so I don't know for sure if there are any specific issues with DX calls in DLLs, but I would think that it's unlikely to be a problem.

What is the return value from CreateDevice?

I'm not sure how I check that directly, but I added a breakpoint just below the call, and checked d3d as that's holding CreateDevice's return value (I believe), and it's saying the same thing as d3ddev.. "__vfptr = CXX0030: Error: expression cannot be evaluated". So I think the problem is the CreateDevice function itself. Either I am calling it incorrectly, or one or more of its arguments are not quite there.

The function call CreateDevice returns a HRESULT (error code), so do:

HRESULT result = CreateDevice (...);

the possible values are defined as D3D_OK, D3DERR_DEVICELOST, D3DERR_INVALIDCALL, D3DERR_NOTAVAILABLE, D3DERR_OUTOFVIDEOMEMORY ( see http://msdn.microsoft.com/en-us/library/bb174313(VS.85).aspx).

So do something like:

switch ( result ) {
  case D3DERR_DEVICELOST: std::cerr << "device lost"; break;
  ... etc ...
}

Just to find out what (if anything) DX thinks is going wrong.

Just FYI, the __vfptr = CXX0030 thing is because the debugger thinks that d3ddev is pointing to an actual instance of a D3D device, if there's some error in the call to CreateDevice, and for whatever reason the device isn't created, then the pointer doesn't get assigned a value, and it'll have an 'undefined' value (since you don't initialize the pointer to anything). Basically, if you declare & initialize d3ddev like:

LPDIRECT3DDEVICE9 d3ddev = NULL;

Then, should the CreateDevice call fail to initialize that variable, the debugger will give a less confusing message, i.e. it will report d3ddev as still being NULL (and it shouldn't try to dereference it and complain about a bad virtual function table, which is what that __vfptr is all about)

Thank you for your help mate. But none of this is going the right way, so I am going to take a completely different approach to it. I've got directx working wonderfully countless times using it straight in a win32 app. This DLL stuff is killing me. So, I'll just stick to using it straight in win32.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.