Hi there!

skinner

Newcomer
Joined
Nov 30, 2008
Messages
2
Hi there! At first, I would like to say that I don't speak as you british or american people do (I am from Spain!). Well, my name is Jose, and I am a new user! Glad to meet you.

I am posting because I am having some problems while trying to execute a Visual C++ .NET code.

It is a win32 application, and it works with Directx 9. The code is this:

Code:
#include <windows.h>
#include <d3d9.h>

//Store handles to the main window and application instance globally.
HINSTANCE hInst; // global handle to hold the application instance
HWND wndHandle; // global variable to hold the window handle
LPDIRECT3D9 pD3D; // the Direct3D object
LPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D device

bool initWindow( HINSTANCE hInstance );
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
bool initDirect3D(void);
void render(void);
void cleanUp(void);

//WinMain: Entry point for windows application
int WINAPI
WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, int showCmd)
{
// Initialize the window
if ( !initWindow( hInstance ) )
return false;
// called after creating the window
if ( !initDirect3D( ) )
return false;
// main message loop:
MSG msg;
ZeroMemory( &msg, sizeof( msg ) );
while( msg.message!=WM_QUIT )
{
// Check the message queue
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage ( &msg );
DispatchMessage ( &msg );
}
else
{
render( );
}
cleanUp();
return (int) msg.wParam;

}
}

/*********************************************************************
* initDirect3D
*********************************************************************/
bool initDirect3D(void)
{
pD3D = NULL;
pd3dDevice = NULL;
// Create the DirectX object
if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
{
return false;
}
// Fill the presentation parameters structure
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight = 480;
d3dpp.BackBufferWidth = 640;
d3dpp.hDeviceWindow = wndHandle;
// Create a default DirectX device
if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,
wndHandle,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&pd3dDevice ) ) )
{
return false;
}
return true;
}
/******************************************************************************
* bool initWindow( HINSTANCE hInstance )
* initWindow registers the window class for the application, creates the window
******************************************************************************/
bool initWindow( HINSTANCE hInstance )
{
WNDCLASSEX wcex;
// Fill in the WNDCLASSEX structure. This describes how the window
// will look to the system
wcex.cbSize = sizeof(WNDCLASSEX); // the size of the structure
wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
wcex.lpfnWndProc = (WNDPROC)WndProc; // the window procedure callback
wcex.cbClsExtra = 0; // extra bytes to allocate for this class
wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
wcex.hInstance = hInstance; // handle to the application instance
wcex.hIcon = 0; // icon to associate with the application
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);// the default cursor
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background color
wcex.lpszMenuName  = NULL; // the resource name for the menu
wcex.lpszClassName = "DirectXExample"; // the class name being created
wcex.hIconSm = 0; // the handle to the small icon
RegisterClassEx(&wcex);
// Create the window
wndHandle = CreateWindow(
"DirectXExample",
"DirectXExample",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, // the starting x coordinate
CW_USEDEFAULT, // the starting y coordinate
640, // the pixel width of the window
480, // the pixel height of the window
NULL, // the parent window; NULL for desktop
NULL, // the menu for the application; NULL for
// none
hInstance, // the handle to the application instance
NULL); // no values passed to the window
// Make sure that the window handle that is created is valid
if (!wndHandle)
return false;
// Display the window on the screen
ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);
return true;
}

/******************************************************************************
* LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
* LPARAM lParam)
* The window procedure
******************************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// Check any available messages from the queue
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
// Always return the message to the default window
// procedure for further processing
return DefWindowProc(hWnd, message, wParam, lParam);
}


/*********************************************************************
* render
*********************************************************************/
void render(void)
{
// Check to make sure you have a valid Direct3D device
if( NULL == pd3dDevice )
return;// Clear the back buffer to a blue color
pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB( 0,0,255 ), 1.0f, 0 );
// Present the back buffer contents to the display
pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

void cleanUp (void)
{
// Release the device and the Direct3D object
if( pd3dDevice != NULL )
pd3dDevice->Release( );
if( pD3D != NULL )
pD3D->Release( );
}

I get 0 errors and 0 warnings, and it executes correctly... ok, it doesn't.

It creates and shows the window, but suddenly (and without having time to see the window's content) it closes, and return to .NET.

I have included the Directx9 SDK Include folder to the VC++ .NET's Include list, and so did I with the LIBRARY folder. I also linked in the Drectx libraries to the project... but I don't see the blue window at all (the code is made to do this! To display a blue window!... But it closes very very fast).

I thought it was a compiler problem, so I installed VC++ 6.0... but it doesn't work.

What can I do?

I hope I have been conclude and precise. And sorry again for my English level!

See you!
 
Debugging

It sounds very likely that your initDirect3D function is failing, and your program exits before the message processing loop. Your first step should be to verify that this is the case - add some printf statements liberally at critical points - when working with things like DirectX it can be a lot easier to debug this way than stepping through.

For example, this is how I'd add debugging statements to initDirect3D:

Code:
bool initDirect3D(void)
{
    HRESULT hResult = D3D_OK;

    printf("Entered function initDirect3D\n");
    pD3D = NULL;
    pd3dDevice = NULL;

    // Create the DirectX object
    pD3D = Direct3DCreate9( D3D_SDK_VERSION );
    if (pD3D == NULL) {
        printf("Direct3DCreate9 failed. pD3D == NULL\n");
        return false;
    }
    printf("IDirect3D9 was created successfully\n");

    // Fill the presentation parameters structure
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof( d3dpp ) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    d3dpp.BackBufferCount = 1;
    d3dpp.BackBufferHeight = 480;
    d3dpp.BackBufferWidth = 640;
    d3dpp.hDeviceWindow = wndHandle;

    // Create a default DirectX device
    hResult = pD3D->CreateDevice( D3DADAPTER_DEFAULT,
            D3DDEVTYPE_REF,
            wndHandle,
            D3DCREATE_SOFTWARE_VERTEXPROCESSING,
            &d3dpp,
            &pd3dDevice );

    if (hResult != D3D_OK) {
        printf("pD3D->CreateDevice failed. hResult = %i\n", hResult);

        switch (hResult) {
        case D3DERR_INVALIDCALL:
            printf("hResult = D3DERR_INVALIDCALL\n"); break;
        case D3DERR_DEVICELOST:
            printf("hResult = D3DERR_DEVICELOST\n"); break;
        case D3DERR_NOTAVAILABLE:
            printf("hResult = D3DERR_NOTAVAILABLE\n"); break;
        case D3DERR_OUTOFVIDEOMEMORY:
            printf("hResult = D3DERR_OUTOFVIDEOMEMORY\n"); break;
        default:
            printf("Unknown hResult\n");
        }

        return false;
    }

    printf("Device created successfully\n");
    return true;
}

Add similar changes to the rest of your code, and debugging these sorts of problems will become much easier - just by looking at the console output of your program, you will be able to locate the area of code which is the source of the problem, and then take steps to correct it.

I could well be wrong about the problem being in the initDirect3D function, but without some sort of debugging facility, it is impossible to tell!

Good luck :cool:
 
Hey Mr Paul, I will have a try tomorrow! Thank you very much! We are both in contact:o

Thank you again,

Skinner
 
Back
Top