AAAAhhhh,i'm going nervous!

Please help,what's wrong with my code ? it give me an unhandled exception when i try to run it.


HRESULT: 0x00000005 (5)
Name: ERROR_ACCESS_DENIED
Description: Access is denied.
Severity code: Success
Facility Code: FACILITY_NULL (0)
Error Code: 0x0005 (5)


Here's my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

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

const int width=1000;
const int height=1000;

void initd3d(HWND);
void clean();

IDirect3D9* pd3d;
IDirect3DDevice9* pdev;

LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nCmdShow )
{
WNDCLASSEX wc;
memset(&wc,0,sizeof wc);
wc.cbSize=sizeof (WNDCLASSEX);
wc.hbrBackground=(HBRUSH)COLOR_WINDOW;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hInstance=hInstance;
wc.lpfnWndProc=WinProc;
wc.style=CS_HREDRAW|CS_VREDRAW;
wc.lpszClassName=L"a";
RegisterClassEx(&wc);
HWND mywin=CreateWindowEx(NULL,L"a",L"asd",WS_OVERLAPPEDWINDOW,0,0,width,height,NULL,NULL,hInstance,NULL);
ShowWindow(mywin,nCmdShow);
initd3d(mywin);
MSG msg;
ZeroMemory(&msg,sizeof msg);
for(;;)

{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
	TranslateMessage(&msg);
	DispatchMessage(&msg);
}
if(msg.message==WM_QUIT)
break;


}



clean();
return msg.wParam;
}

LRESULT CALLBACK WinProc(HWND _hw,UINT _ui,WPARAM _wp,LPARAM _lp)
{
switch(_ui)
{
case WM_DESTROY :
	{
		PostQuitMessage(0);
		return 0;
	}
}

return DefWindowProc(_hw,_ui,_wp,_lp);
}

void initd3d(HWND b)
{
pd3d=Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS pp;
memset(&pp,0,sizeof pp);
pp.BackBufferFormat=D3DFMT_X8B8G8R8;
pp.BackBufferHeight=height;
pp.BackBufferWidth=width;
pp.EnableAutoDepthStencil=true;
pp.AutoDepthStencilFormat=D3DFMT_D16;
pp.Windowed=true;
pp.hDeviceWindow=b;
pp.SwapEffect=D3DSWAPEFFECT_DISCARD;
pd3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,b,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&pp,&pdev);

pdev->SetRenderState(D3DRS_ZENABLE,true);

}

void clean()
{
if(pd3d)
pd3d->Release();
if(pdev)
pdev->Release();


}

P.S. It gives me an exception at 84 th line
Is pdev null?
No,but is that important?
Yes. If is, it'll give you that crash.
I've tried with NULL,but nothing changed :(
Any suggestions???
I'm pretty sure kbw is urging you to make sure that pdev is not null, which to me, it looks like it is. pdev is a pointer to a IDirect3DDevice9 object, but you never set that pointer to anything.

Make sure that pdev points to something, preferably something valid. I wouldn't be surprised if Direct3D has a function that creates these objects and returns a pointer for you, like the IDirect3D type does.

-Albatross
Last edited on
closed account (3pj6b7Xj)
You care creating the device and object the wrong way. don't access the COM interface directly. use

LPDIRECT3D9 pd3d;
LPDIRECT3DEVICE9 pd3dDevice;

You should get rid of the __in , that really isn't needed....you might as well have replaced WINAPI with __stdcall if you wanted to get that detailed.

Here is some code from my MyDirect3D implementation, it might help to see where you are going wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// MYDIRECT3D.cpp ; 1/31/2011
// Rafael P. Santana (C) 2011
// MyDirect3D Framework.

#include "MYDIRECT3D.h"

MYDIRECT3D::MYDIRECT3D()
// The constructor sets the Direct3D object & device to NULL.
{
    pd3d=NULL;              // Direct3D object.
    pd3dDevice=NULL;        // Direct3D device.
}

MYDIRECT3D::~MYDIRECT3D()
// The destructor releases the Direct3D device & object.
{
    // If NULL, do not release.
    if(pd3dDevice)pd3dDevice->Release();    // Direct3D device.
    if(pd3d)pd3d->Release();                // Direct3D object.

    // Other stuff to release?
}

HRESULT MYDIRECT3D::CreateStage(HWND hWnd)
// Inititalizes Direct3D & calls d3dOnCreate(). Call only ONCE!
{
    // Create Direct3D object.
    pd3d=Direct3DCreate9(D3D_SDK_VERSION);

    // Gather window data.
    RECT Rect; GetClientRect(hWnd,&Rect);
    WindowData.hInstance=GetModuleHandle(NULL);
    WindowData.WindowHandle=hWnd;
    WindowData.ClientWidth=Rect.right;
    WindowData.ClientHeight=Rect.bottom;

    // Presentation parameters.
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp,sizeof(d3dpp));
    d3dpp.Windowed=TRUE;
    d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow=WindowData.WindowHandle;
    d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;
    d3dpp.BackBufferCount=1;
    d3dpp.BackBufferWidth=WindowData.ClientWidth;
    d3dpp.BackBufferHeight=WindowData.ClientHeight;

    // Create the Direct3D device.
    pd3d->CreateDevice(D3DADAPTER_DEFAULT,
                       D3DDEVTYPE_HAL,
                       WindowData.WindowHandle,
                       D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                       &d3dpp,
                       &pd3dDevice);

    d3dOnCreate(); // Initialize other Direct3D/Application data.

    return S_OK; // all OK.
}

HRESULT MYDIRECT3D::Render()
// Begins a rendering scene & then calls d3dOnRender(). When d3dOnRender()
// returns, the render scene ends & the back buffer is presented to the screen.
{
    // Clear the Direct3D back buffer.
    pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(80,80,80),1.0f,0);

    pd3dDevice->BeginScene(); // Begin rendering.
    d3dOnRender(); // Handles the drawing work.
    pd3dDevice->EndScene(); // End rendering.

    // Present the back buffer to display.
    pd3dDevice->Present(NULL,NULL,NULL,NULL);

    return S_OK; // all OK.
}

HRESULT MYDIRECT3D::InitViewport()
// Initializes the viewport & field of view matrices. This is critical if you
// are using untransformed vertices, otherwise, the scene comes up blank!
{
    // Setup viewport.
    D3DXVECTOR3 Eye(0,0,-10);
    D3DXVECTOR3 Target(0,0,0);
    D3DXVECTOR3 Up(0,1,0);
    D3DXMATRIXA16 Viewport;
    D3DXMatrixLookAtLH(&Viewport,&Eye,&Target,&Up);
    pd3dDevice->SetTransform(D3DTS_VIEW,&Viewport);

    // The viewport works the following way. The Eye is the position to look
    // from. The Eye will always look at the Target. Up is the orientation of the
    // Eye(i.e 0,-1,0) would flip the scene upside down, normally it is (0,1,0).

    // Setup field of view.
    D3DXMATRIX FieldOfView;
    D3DXMatrixPerspectiveFovLH
        (&FieldOfView, // Where to store data.
         D3DX_PI*0.25f, // Field of view, in the y direction, in radians.
         WindowData.ClientWidth/ // Client width.
         WindowData.ClientHeight, // Client Height.
         1,50); // near & far viewing ranges.
    pd3dDevice->SetTransform(D3DTS_PROJECTION,&FieldOfView);

    // WARNING!! Failure to setup the camera viewport & field of view will
    // result in a blank scene. It is a MUST to setup these matrices or
    // you will not see anything, save yourself tremendous frustration!

    return S_OK; // all OK.
}


here is where object and device are declared...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class MYDIRECT3D : _d3dOnCreate
// _d3dOnCreate is a struct that this class inherits from d3dOnCreate.h
// Use the struct to initialize your 3D scene variables. It is recommended
// you follow this rule to better organize your project's data.
{
    private:

    // Window data structure.
    struct _WindowData
    {
        HINSTANCE hInstance;
        HWND WindowHandle;
        UINT ClientWidth;
        UINT ClientHeight;
    }   WindowData;

    MSG Msg; // Stores window messages.

    public:

    // Constructor & Destructor
    MYDIRECT3D(); ~MYDIRECT3D();

    // Make pd3d & pd3dDevice public!
    LPDIRECT3D9 pd3d; LPDIRECT3DDEVICE9 pd3dDevice;

    // pd3d & pd3dDevice are COM objects with very strict encapsulation
    // rules, as defined by the COM interface. It is safe for these to
    // be public, you need them anyway to work with Direct3D!

    // Member functions.
    HRESULT CreateStage(HWND); // Initializes Direct3D.
    HRESULT d3dOnCreate(); // What to do when CreateStage() is called.
    HRESULT d3dOnRender(); // What to do when Render() is called.
    HRESULT Render(); // Renders a frame.
    HRESULT InitViewport(); // Sets up the viewport & field of view.

    // Store window messages in Msg.
    void GetMsg(MSG*Msg_){Msg=*Msg_;}
};


Hope you fix your problems, happy coding.
Last edited on
this error often happend when you used null pointor;
Topic archived. No new replies allowed.