DLL Access Violation?

This is a discussion on DLL Access Violation? within the C++ Programming forums, part of the General Programming Boards category; I made a dll to just create a window so I don't have to do it everytime and I'm getting ...

  1. #1
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215

    Arrow DLL Access Violation?

    I made a dll to just create a window so I don't have to do it everytime and I'm getting an access violation on a bunch of stuff. The first to go was LoadCursor(), now its GetStockObject(). Here's some code:

    Code:
    bool ArkCore::ArkCreateWindow(HINSTANCE hInstance, char* Title)
    {
    	hInst = hInstance;
    
    	WNDCLASSEX wcex;
    
    	/////Fill in the Window class structure - Describes how the window will look
    	wcex.cbSize = sizeof( WNDCLASSEX ); //size of the structure
    	wcex.style = CS_HREDRAW | CS_VREDRAW; //the class style
    	wcex.lpfnWndProc = (WNDPROC)MsgProc; //Window procedure callback
    	wcex.cbClsExtra = 0; //Extra class bytes
    	wcex.cbWndExtra = 0; //Extra window bytes
    	wcex.hInstance = hInstance; //Handle to the app instance
    	wcex.hIcon = NULL; // icon
    
    	wcex.hCursor = LoadCursor(NULL, IDC_ARROW); <--//ACCESS VIOLATION
    
    	wcex.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); //<----ACCESS VIOLATION
    I was told from someone that LoadCursor wasn't pointing to a valid instance handle and that's what caused it, but, I don't want to use a resource, I want the predefined IDC_ARROW. Is there anyway to do this?

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    that code looks fine to me, are you sure it's not somewhere else? I've used that same code for those two calls before.

  3. #3
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Quote Originally Posted by rockytriton
    that code looks fine to me, are you sure it's not somewhere else? I've used that same code for those two calls before.
    Was that code in a DLL? I've used the same code on my windows apps and it compiled but as soon as I put them in a dll they blew up.

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    oh forgot it was a DLL, hmm, maybe try calling:
    Code:
    AFX_MANAGE_STATE(AfxGetStaticModuleState())
    at the start of the function

  5. #5
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    I'm not using Afx, I tried using GetInstanceModule("ArkCore.dll"), GetInstanceModule(NULL) which is supposed to give me the instance of the .exe and both failed. I'm assuming the Afx function does the same thing.

    Something really really strange is going on, now its giving me an access violation on hIcon = NULL =\ I'm stuck to believe that I can't create a window from outside of the program. I think I remember something from a while back that actually puts the code in the program. I think it was inline. I'm going to play around with it. If you have any suggestions please post.

    Thanks!

  6. #6
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    sounds like your debugger is messed up, are you testing in debug mode or release mode?

  7. #7
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    I built in both

    The debugger is .NET 2003 btw
    Last edited by durban; 10-04-2005 at 02:17 PM.

  8. #8
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    I have seen this behavior before when calling a class function before the pointer has been initialized, do you think this may be a problem? How are you instantiating this class and calling the function?

  9. #9
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Thank you rocky for your undying support, here is some snippets from the calling code and dll.

    test.cpp (calling code)
    ----------------------------------------
    Code:
    #include "ArkCore.h"
    
    ArkCore *AC;
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
    {
    	if( !AC->ArkCreateWindow(hInstance, "test"))
    		return 0;
    	if( !AC->InitD3D(true, 640, 800))
    		return 0;
    	return AC->StartLoop();
    }

    ArkCore.dll - ArkCore.h
    -----------------------------------------
    Code:
    #ifndef INC_ARKCORE_H
    #define INC_ARKCORE_H
    #include <d3d9.h>
    #include <d3dx9.h>
    #include <windows.h>
    
    class __declspec(dllexport) ArkCore
    {
    protected:
    	HWND wndHandle;
    	HINSTANCE hInst;
    	
    	LPDIRECT3D9 d3d;
    	LPDIRECT3DDEVICE9 device;
    
    public:
    	HWND *GetHWND() { return &wndHandle; }
    	HINSTANCE *GetHINST() { return &hInst; }
    
    	bool ArkCreateWindow(HINSTANCE hInstance, char* Title); // Creates the window
    	bool InitD3D( bool Windowed, int BackBufferHeight, int BackBufferWidth ); // Initializes DirectX
    	int StartLoop(); // Controls the message loop
    	void Render();
    	void ShutDown();
    
    	static LRESULT CALLBACK MsgProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
    	{
    		//Check any available messages from the queue
    		switch(message)
    		{
    		case WM_CLOSE:
    			PostQuitMessage(WM_QUIT);
    			break;
    		case WM_DESTROY:
    			PostQuitMessage(WM_QUIT);
    			break;
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    		}
    
    		return 0;
    	}
    };
    #endif

    ArkCore.dll - ArkCore.cpp (ArkCreateWindow)
    -----------------------------------------------
    Code:
    bool ArkCore::ArkCreateWindow(HINSTANCE hInstance, char* Title)
    {
    	hInst = hInstance;
    
    	WNDCLASSEX wcex;
    
    	/////Fill in the Window class structure - Describes how the window will look
    	wcex.cbSize = sizeof( WNDCLASSEX ); //size of the structure
    	wcex.style = CS_HREDRAW | CS_VREDRAW; //the class style
    	wcex.lpfnWndProc = (WNDPROC)MsgProc; //Window procedure callback
    	wcex.cbClsExtra = 0; //Extra class bytes
    	wcex.cbWndExtra = 0; //Extra window bytes
    	wcex.hInstance = hInstance; //Handle to the app instance
    	wcex.hIcon = NULL; // icon
    	wcex.hCursor = LoadCursor(NULL, IDC_ARROW); //cursor
    	wcex.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); //bgcolor
    	wcex.lpszMenuName = NULL; //resource name for the menu
    	wcex.lpszClassName = "Arkhana"; //class name being created
    	wcex.hIconSm = NULL; //Small icon handle
    	RegisterClassEx( &wcex );
    
    	//Create the window
    	wndHandle = CreateWindow( "Arkhana", // Class name to use
    		Title, // Title bar text
    		WS_OVERLAPPEDWINDOW, //The window style
    		0, // The starting x coord
    		0, // The starting y coord
    		800, // The pixel height
    		600, // The pixel width
    		NULL, // The parent window, null for desktop
    		NULL, // The menu for the app - null for none
    		hInstance, // The handle to the app instance
    		NULL); // No values passed to the window
    
    	if( !wndHandle )
    		return false;
    
    	ShowWindow( wndHandle, SW_SHOW );
    	UpdateWindow( wndHandle );
    	return true;
    }

  10. #10
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    yes, exactly what I thought, you need to do:

    Code:
    AC = new ArkCore();
    at the beginning of your WinMain function. You need to instantiate the object before calling any non-static methods on it.

  11. #11
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    OMG ........ing right rocky. It works like a charm now. Thanks for the help bro!

  12. #12
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    no prob, I've had this type of problem before, you forget to create a new instance and you get all kinds of random weird problems, very hard to debug.

  13. #13
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    I will definitely remember this error and know how to counter it now. Working with DirectX for so long made me forget all about creating new instances since the API does it for you . This forum is bringing back some old C++ memories too, I think I'll stick around for awhile and help some people out lol..too bad the game programming forum is kind of dead I could be of more use there but oh well. Anyways, Thanks again for the support rocky, your solution has been professional and very helpful. The best of luck to ya mate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  2. FtpFileFind access violation with MS VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 07:02 PM
  3. Access VB DLL from C++ ?
    By torbjoen in forum Windows Programming
    Replies: 3
    Last Post: 12-05-2002, 06:14 AM
  4. DLL & free() Heap Access Violation
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 08-13-2002, 10:45 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21