Modeless Dialog fails to stay displayed.

This is a discussion on Modeless Dialog fails to stay displayed. within the Windows Programming forums, part of the Platform Specific Boards category; Hello, Does anybody have any idea why this snippet of code produces a modeless dialog box (when included in a ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    Modeless Dialog fails to stay displayed.

    Hello,

    Does anybody have any idea why this snippet of code produces a modeless dialog box (when included in a subroutine contained in a DLL)that flashes on the screen for a fraction of a second and then disappears . When I call another subroutine (also in the DLL) that produces a modal dialog box, then both the modeless and modal dialog boxes are displayed in the window. I am able to manipulate the dialog boxes separately.


    Code:
    // Modeless Dialog
    extern"C"
    {
    TS104_API INT TS104_S1(VOID)
    {
    int err;
    hWnd = 0;
    hInstance = GetModuleHandle(L"TS104.dll");
    if( hInstance <= NULL)
    {
    MessageBox( NULL, L"CreateDialog handle is illegal", L"TS104_S1 Warning!",
    MB_OK | MB_ICONINFORMATION );
    return FALSE;
    }
    hWndParent = NULL;
    hDlg = CreateDialog( hInstance, MAKEINTRESOURCE(IDD_DIALOG2), hWndParent, (DLGPROC)GoTo1Proc ); 
    ShowWindow( hDlg, SW_SHOW );
    return TRUE;
    }
    }
    INT_PTR CALLBACK GoTo1Proc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
    { 
    switch ( uMsg ) 
    { 
    case WM_INITDIALOG:
    return TRUE;
    case WM_COMMAND: 
    switch (LOWORD( wParam )) 
    { 
    case IDOK: 
    case IDCANCEL:
    DestroyWindow( hDlg ); 
    return TRUE; 
    }
    } 
    


    Any suggestions would be appreciated!

    Regards,
    -Frank
    Last edited by fmuir; 02-28-2013 at 07:59 PM.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    My suggestions are;

    Fix the tabbing of your code. People will ignore it because you did not take the time to make it readable.

    Follow the correct syntax (ie SWITCH, CASE, then BREAK!) I does not appear your are experienced enough to take the coding/syntax short-cuts you have.

    Look at (or turn up) the warnings on your complier / IDE (as it did not tell you that you have failed to return any value from a function you declared would have an INT return). I also suggest you RTFM to find out what that INT return signifies.

    Post your message pump.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    Code:
    // Modeless Dialog
    extern "C"
    {
    TS104_API INT TS104_S1(VOID)
    {
        int  err;
        hWnd = 0;
        hInstance = GetModuleHandle(L"TS104.dll");
        if( hInstance <= NULL)
        {
            MessageBox( NULL, L"CreateDialog handle is illegal", L"TS104_S1 Warning!",
                MB_OK | MB_ICONINFORMATION );
            return FALSE;
        }
        hWndParent = NULL;
        hDlg = CreateDialog( hInstance, MAKEINTRESOURCE(IDD_DIALOG2), hWndParent, (DLGPROC)GoTo1Proc ); 
        ShowWindow( hDlg, SW_SHOW );
        return TRUE;
    }
    }
    INT_PTR CALLBACK GoTo1Proc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
    { 
        switch ( uMsg ) 
        { 
            case WM_INITDIALOG:
                return TRUE;
            case WM_COMMAND: 
                switch (LOWORD( wParam )) 
                { 
                    case IDOK: 
                    case IDCANCEL:
                        DestroyWindow( hDlg ); 
                        return TRUE; 
                }
        } 
        return FALSE;
    }
    Code snippet re-posted. The original post only used tabs. This post had all tabs converted to spaces by the IDE.

    Regards,
    -Frank

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    You explicitly use wchar_t by using the "L" prefix in front of each string, but you don't do the same with the functions.
    What I'm saying is, use GetModuleHandleW & MessageBoxW instead.
    Devoted my life to programming...

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Also, this is just wrong( ethically, logically and practically ):
    Code:
    if ( hInstance <= NULL)
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    Quote Originally Posted by GReaper View Post
    Also, this is just wrong( ethically, logically and practically ):
    Code:
    if ( hInstance <= NULL)
    GReaper,

    Thanks for replying. I am using VS2008 with the Unicode Character Set. The compiler automatically uses GetModuleHandleW when Unicode is used. It will use GetModuleHandleA when Unicode is not specified.

    Why is using the statement 'hInstance <= NULL' ethically, logically and practically wrong? GetModuleHandle returns a NULL if it fails. Using "<" in the if statement probably really wasn't necessary, since GetModuleHandle() never returns a negative number.

    I have a second function that I am using that is also included in the DLL. It's creates a Modal DialogBox and it's functioning fine. Both functions use the same handle.Why is using the statement 'hInstance <= NULL' ethically, logically and practically wrong? GetModuleHandle returns a NULL if it fails. Using "<" in the if statement probably really wasn't necessary, since GetModuleHandle() never returns a negative number.

    My problem still exists! Why does a Modeless Dialogbox appear momentarilly and then disappear without any user intervention? When the Modal Dialogbox open, then both dialogboxes remain on the monitor screen until the user clicks the OK or Cancel button on the Modal Dialogbox. Then the two dialogboxes disappear when the third dialogbox opens.

    I'm out of ideas!

    Regards,
    -Frank

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    You did not post your message pump.

    So I will guess...

    You do not have a call to IsDialogMessage() in your message pump.

    When your modeless dlg is created it gets a WM_INITDIALOG message, you return TRUE. This sets the focus to the default control on the dialog.

    Now all messages are being sent to that (default) control and not the dialog. IsDialogMessage() is required to intercept these messages and route them to the dialogs callback.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    Quote Originally Posted by novacain View Post
    Now all messages are being sent to that (default) control and not the dialog. IsDialogMessage() is required to intercept these messages and route them to the dialogs callback.
    @ novacain.
    Your correct, there is no message pump. The code snippet is the some and substance of the DLL. The program that uses the functions in the DLL doesn't have a message pump, so no dispatch or translate functions are available.

    From what you said, I suspect that I need separate handles for the two dialog boxes and that each dialog box needs an IsDialogMessage function. Is that a correct assumption? I'll look into adding them tomorrow.

    Rergards,
    -Frank

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Quote Originally Posted by fmuir View Post
    @ novacain.
    Your correct, there is no message pump. The code snippet is the some and substance of the DLL. The program that uses the functions in the DLL doesn't have a message pump, so no dispatch or translate functions are available.
    All Win32 programs have a msg pump, usually one per thread running the UI. You need to modify the main msg pump to handle the IsDialogMessage() for each modeless dialog.

    From what you said, I suspect that I need separate handles for the two dialog boxes and that each dialog box needs an IsDialogMessage function. Is that a correct assumption? I'll look into adding them tomorrow.
    Yes. You cannot use the same HANDLE for both.

    BTW you have global variables with the same name as a local variable (hDlg). This is very poor form (ignoring the confusion it creates), because it does not allow you to access the global variable while the local has scope.

    Write your code so that the next coder that works on this app can understand. All the shotcuts you are taking are going to cost someone time (and money) later.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    @ Novacain,
    Thanks for your assistance!!. I don't disagree with any thing that you have said. I'll look into fixing those issues that you have mentioned, that I have access to. However, since this is an attempt to create a Dialog Box that will allow a user to interact with a Trading Platform (MT4, written by MetaQuotes). The functions that I am writing will be contained in a Dynamic Link Library. The Trading Platform is probably is made up of different Win32 applications. The Trading Platform interacts only with certain servers. Unfortunately, programming (Advisorsa, Indicators and Scripts) only can use just a subset of the commands and functions available in Win32. Any message pump is invisible to programmers or users. The internals of the Trading Platform are not public. No feedback from MetaQuotes.
    I just read a post on the MT4 forum, explaining how arriving ticks (changes in price) to the platform affect DLLs. It stated that each tick creates a new thread. I had been under the impression that a DLL function opened and used the same thread each time that it was called again. Obviously that isn't what happens. I have a couple of ideas that I am going to persue.
    If I knew how to delete or close a thread, this one would be a candidate?
    Regards,
    -Frank
    Last edited by fmuir; 03-13-2013 at 09:46 PM.

  11. #11
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Ahhh...

    Sounds like you need a dedicated UI thread to run your dialog (with its own message pump) AND a thread to receive msgs (ticks etc) from the trading platform.

    Your thread monitoring the trading platform can then use SendMessage() / PostMessage() to update your dialog when it gets an update.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modeless dialog quits by itself immediatly after creation
    By joseph_isreavel in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2011, 07:14 AM
  2. Fixing Z-order on a modeless dialog?
    By Viper187 in forum Windows Programming
    Replies: 6
    Last Post: 06-20-2008, 07:10 PM
  3. modeless dialog not interacting with main window
    By scwizzo in forum Windows Programming
    Replies: 3
    Last Post: 08-30-2007, 04:33 PM
  4. Modeless Dialog Communication (MFC)
    By mhandlon in forum Windows Programming
    Replies: 8
    Last Post: 08-22-2006, 01:28 PM
  5. creating modeless dialog
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 03-07-2002, 04:58 PM

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