Archived

This topic is now archived and is closed to further replies.

Ice-T

Why does CreateDevice() fail?

Recommended Posts

For some reason, CreateDevice() always fails for me in Direct3D 9. I know I installed DirectX 9 properly so why is not working? Direct3D_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, VertexProcessing, &Present_Parameters, &D3D_Device)

Share this post


Link to post
Share on other sites
With the code you posted, we need to know this : hWnd, Present_Parameters, and VertexProcessing to know whats up, and to make sure you initialized your Device to NULL.


Thanks!

-Rhone

Share this post


Link to post
Share on other sites
Sorry about that. Here's the code. There's no reason why it shouldn't work. It was a sample with the DirectX9 SDK.


#include <d3d9.h>

LPDIRECT3D9 g_pD3D = NULL; LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;

HRESULT InitD3D( HWND hWnd )
{

if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;


if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}



return S_OK;
}



VOID Cleanup()
{
if( g_pd3dDevice != NULL)
g_pd3dDevice->Release();

if( g_pD3D != NULL)
g_pD3D->Release();
}




VOID Render()
{
if( NULL == g_pd3dDevice )
return;


g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );


if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{



g_pd3dDevice->EndScene();
}


g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}





LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;

case WM_PAINT:
Render();
ValidateRect( hWnd, NULL );
return 0;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}




INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{

WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"D3D Tutorial", NULL };
RegisterClassEx( &wc );


HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), NULL, wc.hInstance, NULL );


if( SUCCEEDED( InitD3D( hWnd ) ) )
{
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );


MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}

UnregisterClass( "D3D Tutorial", wc.hInstance );
return 0;
}


[edited by - Ice-T on August 14, 2003 12:03:28 AM]

Share this post


Link to post
Share on other sites
Grab the exact error code with an HRESULT variable and run it through DXGetErrorString9 or DXGetErrorDescription9 and pop those in a message box. That oughta clear things up.

Share this post


Link to post
Share on other sites
quote:
Original post by RhoneRanger
did you create your window before you tried to pass it to the CreateDevce?


Yah. And the error description I got was D3DERR_INVALIDCALL and "Invalid Call". What should I do since it doesn't recognize the call? Does it mean DirectX 9 is improperly installed? It compiles and links fine.

Just wondering, did the code work for you guys?



[edited by - Ice-T on August 15, 2003 1:20:57 AM]

Share this post


Link to post
Share on other sites
If you have the debug version of DirectX installed it will tell you what''s wrong. If you don''t have it installed, go do it.

Then go to the control panel and click the DirectX icon. Go to the Direct3D tab and crank the error level. Now when you run your DirectX apps all sorts of interesting information will be dumped into the debug window.


Stay Casual,

Ken
Drunken Hyena

Share this post


Link to post
Share on other sites
quote:
Original post by DrunkenHyena
If you have the debug version of DirectX installed it will tell you what's wrong. If you don't have it installed, go do it.

Then go to the control panel and click the DirectX icon. Go to the Direct3D tab and crank the error level. Now when you run your DirectX apps all sorts of interesting information will be dumped into the debug window.



No luck with that either. So does anybody have any suggestions?


[edited by - Ice-T on August 15, 2003 2:38:58 AM]

Share this post


Link to post
Share on other sites
HRESULT InitD3D( HWND hWnd );

Try HRESULT InitD3D( HWND *hWnd );

Sometimes, the hWnd can be the problem, that depends ofcause on your application.

Share this post


Link to post
Share on other sites
okay, a few things::


Where do you register your win class?

ALso, dont try to init D3D until you Show Window.


RegisterClass (& windowClass);

ShowWindow;
Updatewindow;

NOW init d3d.

Share this post


Link to post
Share on other sites
First off, you should make your WindowClass Global.

char * WndClass="WindowClass";


Then pass WndClass to your create window, and also to the RegisterClass function.


Then do the rest:

ShowWindow;
UpdateWindow;

NOW createD3D;

Share this post


Link to post
Share on other sites
At some computer this happened to me.
After some investigation someone has disabled the 3D accelaration and this fails to create the device.

Share this post


Link to post
Share on other sites
quote:
Original post by Ice-T No luck with that either. So does anybody have any suggestions?


No luck getting the debug stuff to work? Or no luck with the output? The Debug output is incredibly useful and it should at the very least give you a hint about why it can''t create a device.

Share this post


Link to post
Share on other sites
I tried all the suggestions like creating and registering before creating the deviece and I keep getting the same error. When I ran D3DSpy, I got the same message, that CreateDevice returned "Invalid Call". That is the Debug Output part right? Worst part about this whole thing is that it''s one of the samples with the SDK so it makes no sense for it not to work. I even re-installed DirectX 9 but CreateDevice() fails. I even tried another sample from another site and that failed too. Is it possible I didnt install some part of DirectX improperly? What''s odd is that this is the only call that fails. I tried some samples in the SDK like lighting and water for Direct3D adn they all work fine, its whenever an app uses CreateDevice() that it fails.

Share this post


Link to post
Share on other sites
Ugh...

I am almost positive you have to set the hDeviceWindow in your present parameters if you want to use windowed mode. Make your HWND global and try setting it to that.

Share this post


Link to post
Share on other sites
Thanks for all the help guys but I figured it out. My weird PC was on 24bit mode instead of 32 bit because that was the highest bit-depth I had and when I changed it to 16 bit, it worked.

Share this post


Link to post
Share on other sites
quote:
Original post by Raloth
Ugh...

I am almost positive you have to set the hDeviceWindow in your present parameters if you want to use windowed mode. Make your HWND global and try setting it to that.


IIRC, if you leave the HWND as NULL, then Direct3D selects the foreground window as the render target.

Share this post


Link to post
Share on other sites
Actually it works like this:

If you don't specify a focus window (the argument to CreateDevice), you won't be able to go full-screen. Otherwise not a problem.

If you don't specify a device window (in D3DPRESENT_PARAMETERS), the focus window will be used for the device window.

If you don't specify either, device creation will fail. In other words, the device window is required. If you don't specify it explicitly, you must provide a focus window which will be used implicitly. One way or another the runtime requires a window in this role.

I would advise against using the desktop window in either role, especially as the focus window.

EDIT: To clarify, this is a bad idea because the focus window is subclassed when you go full-screen. You don't want to be doing this to the desktop window.

[edited by - Donavon Keithley on August 15, 2003 10:54:04 PM]

[edited by - Donavon Keithley on August 15, 2003 10:57:35 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Ice-T
I tried all the suggestions like creating and registering before creating the deviece and I keep getting the same error. When I ran D3DSpy, I got the same message, that CreateDevice returned "Invalid Call". That is the Debug Output part right?


No. If you use VisualC++ when you run the programmer in the debugger there is a debug window that receives input. Lots of helpful error and warning messages are written there.

For example, I set my video mode to 24-bit and then ran a windowed mode sample. It gave this error:
Direct3D9: (ERROR) :Invalid value for BackBufferFormat. ValidatePresentParameters fails.

If you don''t use VC++ (or want to run without having the IDE loaded) you can download DebugView from www.sysinternals.com

Stay Casual,

Ken
Drunken Hyena

Share this post


Link to post
Share on other sites