Thread: Die you dirty DLL!

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Die you dirty DLL!

    Ok so I'm trying to load a function from a DLL using GetProcAddress. I've got this DLL:

    Code:
    extern "C" __declspec(dllexport) HWND WINAPI WWNewWindow(WNDPROC lpfnWndProc, LPSTR lpszClassName,
                                                      LPSTR lpszWindowName, int x, int y,
                                                      int width, int height, DWORD dwStyle)
    {
        WNDCLASSEX wc;
        HWND hWnd;
    
        wc.cbClsExtra = 0;
        wc.cbSize = sizeof(wc);
        wc.cbWndExtra = 0;
        wc.hbrBackground = static_cast<HBRUSH> (GetStockObject(GRAY_BRUSH));
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
        wc.hInstance = NULL;
        wc.lpfnWndProc = lpfnWndProc;
        wc.lpszClassName = lpszClassName;
        wc.lpszMenuName = NULL;
        wc.style = CS_VREDRAW | CS_HREDRAW;
    
        if (! RegisterClassEx(&wc)) return NULL;
        hWnd = CreateWindow(NULL, lpszWindowName, dwStyle, x, y, width, height, NULL, NULL, NULL, NULL);
    
        return hWnd;
    }
    With this trying to load it:

    Code:
    typedef HWND (WINAPI *lpfnWWNewWindow)(WNDPROC lpfnWndProc, LPSTR lpszClassName, LPSTR lpszWindowName,
                                           int x, int y, int width, int height, DWORD dwStyle);
    
    lpfnWWNewWindow WWNewWindow;
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    
    HRESULT LoadWindowWrapper()
    {
        HMODULE hmWindowWrapperDLL;
    
        hmWindowWrapperDLL = LoadLibrary("WindowWrapper.dll");
        if (! hmWindowWrapperDLL) return E_FAIL;
    
        WWNewWindow = (lpfnWWNewWindow) GetProcAddress(hmWindowWrapperDLL, "WWNewWindow");
        if (! WWNewWindow) return E_FAIL;
        return S_OK;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        HWND hWnd;
    
        if (SUCCEEDED(LoadWindowWrapper()))
        {
            hWnd = WWNewWindow(WndProc, "D3DApp", "Hello Ace!", 0, 0, 300, 300, WS_OVERLAPPEDWINDOW);
            if (! hWnd)
            {
                MessageBox(0, "Error creating window", "Error", MB_OK | MB_ICONERROR);
                return 1;
            }
        }
        else
        {
            MessageBox(0, "Error loading DLL", "Error", MB_OK | MB_ICONERROR);
            return 1;
        }
    
        return 0;
    }
    But it doesn't retrieve the function address. I've stepped though it and get an Access Violation *something* at address 0x00000000 when it gets to the call in WinMain (if I remove the error checking in LoadWindowWrapper, obviously).

    I can't see what's wrong with this. I hate this platform sometimes.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You need to use a .def file to export the functions from your DLL. A sample .def file will look like this and needs to be added to the project:
    Code:
    LIBRARY    "MyDllName"
    EXPORTS
        WWNewWindow
    See 'Why can't I GetProcAddress a function I dllexport'ed?'.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Ah I thought that's what __declspec(dllexport) did. Thanks.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. Newbie class question
    By vastgoten in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2008, 01:43 AM
  3. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  4. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM