CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2009
    Posts
    22

    [RESOLVED] Creating a window in a DLL

    So I'm using the code below to create a window from a DLL. I've tried this code in an application, and it worked, but it does not seem to work from a DLL. I get "1" on the return so I know the program is properly loading the DLL, but the window is never created. I've also tested the "double windowt", and was able to change the size of that window so I know I'm passing the argument correctly.

    DLL double startup(double windowt,double x1,double y1,double x2,double y2)
    {HWND window=(HWND)(DWORD)windowt;
    browse=window;
    WNDCLASS wc;
    //create window
    //HINSTANCE hInstance=GetModuleHandle(NULL);
    HINSTANCE hInstance = (HINSTANCE)GetWindowLong(window, GWL_HINSTANCE);
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = NULL;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "eRun";
    RegisterClass( &wc );
    HWND hWnd = CreateWindow( "eRun", "eRun Loading...", WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,0, 0, x2, y2,NULL, NULL, hInstance, NULL );
    return 1;}

  2. #2
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Creating a window in a DLL

    when you create a window in a DLL how do you call it , please write entire code , the way you have gone about creating window , this can only work from WinMain not a DLL

  3. #3
    Join Date
    Jun 2008
    Posts
    592

    Re: Creating a window in a DLL

    Code:
    DLL double startup(double windowt,double x1,double y1,double x2,double y2)
    Why return a double?
    Code:
    DLL double startup(double windowt,double x1,double y1,double x2,double y2)
    I see you mentioned this was for testing. You should remove old testing code that is irrelevant before posting code
    Code:
    browse=window;
    Where is browse? If it is global, why?
    Code:
    RegisterClass( &wc );
    You should always check for failure. otherwise how you do know this is working?
    Code:
    HWND hWnd = CreateWindow( "eRun", "eRun Loading...", WS_CAPTION |  WS_POPUPWINDOW | WS_VISIBLE,0, 0, x2, y2,NULL, NULL, hInstance, NULL );
    Okay you have hWnd which is your handle, but why the new variable?
    Code:
    return 1;
    Why are you returing an int of 1? You already expect a double which an int can convert to implicitly, but I am sure returning 1 is not your intended result.

    Why is your registration code and creation of a window code in the same function. You should always try to separate code into logical steps as much as possible. For ex,

    Code:
    Handle StartUp( int x, int y, int Width, int Height )
    {
        HWND WindowHandle = NULL;
    
        if( SetupWin32() )
        {
            WindowHandle = CreateGuiWindow( x, y, Width, Height ); 
        }
    
        return WindowHandle;
    )
    You need to check for failure on api calls. If they fail, call GetLastError() and see what the error means on msdn. You also need to fix these errors I have shown you or explain in more detail what they are for.

    It is possible to create a window in a dll and have it loop through its own messaging queue. Is this what you are trying to do?

    You should always post a complete small example demonstrating your problem.
    Last edited by Joeman; July 9th, 2011 at 09:41 AM.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  4. #4
    Join Date
    Apr 2009
    Posts
    22

    Re: Creating a window in a DLL

    I'm making this DLL for gamemaker, which only allows you to pass variables in to form of Double and Char. That is why I use so many doubles, and save the HWND browse as a global variable. I called GetLastError() after RegisterClass, Create Window, and "SetWindowPos( hWnd, HWND_TOP, 0, 0, 100, 100, SWP_SHOWWINDOW);" (I added it to test the window). The only error i got was on setwindowpos, and the error was "invalid window handle" so i know it's not working, but I can't pin point the problem.

    @aamir121a that is the exact code from the DLL, what needs to be different in a DLL?

  5. #5
    Join Date
    Jun 2008
    Posts
    592

    Re: Creating a window in a DLL

    When you register your own class, it must have a message pump and a window procedure

    What is
    Code:
    wc.lpfnWndProc = NULL;
    How do you handle your messages?
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  6. #6
    Join Date
    Apr 2009
    Posts
    22

    Smile Re: Creating a window in a DLL

    I added a message pump and it works perfectly now, thanks for the help .

  7. #7
    Join Date
    Jun 2008
    Posts
    592

    Re: [RESOLVED] Creating a window in a DLL

    Also don't forget to unregister your class since dlls do not unregister classes automatically.

    Also are you sure there isn't a better way to pass HWND? Can you at least pass pointers?
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  8. #8
    Join Date
    Apr 2009
    Posts
    22

    Re: [RESOLVED] Creating a window in a DLL

    I'll make sure to unregister it, thanks . I'll look into changing the HWND into a double since that is what the gamemaker does when it's sending it's window to the DLL.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured