SendMessage not working

Posted on 2005-04-07
Last Modified: 2010-04-17
who can call SendMessage(hwnd, WM_USER+1, WPARAM, LPARAM)?

Can a non-dialog class call it?

How does it go about calling it? simply like so:

SendMessage(dialogHwnd, WM_USER+1, 0, (LPARAM)"some text");

or ::SendMessage(dialogHwnd, WM_USER+1, 0, (LPARAM)"some text");

how do you know if the dialogHwnd is a valid handle?

I'm not getting any errors but my onMyMessage() func is not being invoked...

and yes, I do have the message map so:

      ON_MESSAGE(WM_USER+1, OnMyMessage)
Question by:jade03
    LVL 9

    Accepted Solution

    >>Can a non-dialog class call it [SendMessage]?


    >>How does it go about calling it?

    ::SendMessage(dialogHwnd, WM_USER+1, 0, (LPARAM)"some text");

    >>how do you know if the dialogHwnd is a valid handle?


    1. How is onMyMessage declared?
    2. Have you tried sending the message from within a TabFive class instance (object)?

    Do this in one of the other message handlers in your TabFive class (e.g. in a button click message):

    Start the app, place a breakpopint in onMyMessage, bring up the TabFive dialog and invoke the corresponding function. Note that LPARAM will be NULL on entry in onMyMessage, but a least you'll see if it gets there.


    Author Comment


    k, I tried calling SendMessage() from a TabFive instance, and it does go into onMyMessage();

    however, it's still not going into onMyMessage() when called inside a non-dialog class using:

    ::SendMessage(dialogHwnd, WM_USER+1, 0, (LPARAM)"some text");

    I checked dialogHwnd to see if it's null like so:

    if(((CMyTabExampleDlg*)AfxGetMainWnd())->m_tabFive.m_tabFiveDlgHandle == NULL)
        AfxMessageBox("handle is null");
       AfxMessageBox("handle not null");  <-------  PRINTS OUT THIS LINE

             AfxMessageBox("handle is valid");   <---------BUT NOT THIS LINE!!

    so what does it mean it's NOT null but yet it's NOT a valid handle??

    LVL 9

    Expert Comment

    The C++ object was created but not the window. You obviously cannot send messages to a window that doesn't exit, but you can call methods on the C++ object.

    Consider this piece of code:

    class CMyDialog : public CDialog
        // stuff

    // somewhere in your program:

    CMyDialog dlg;  
    // C++ object created but not the window
    dlg.DoModal();  // while executing this, the corresponding window exists
    // C++ object still exists but no the window (DoModal returns when the window is closed)


    Author Comment

    I'm sorry, ur suggestion is...??

    I should call DoModal on tabfive? but when I do that, the whole application doesn't show up anymore...
    LVL 9

    Expert Comment

    Well, you have to think about the logic of your application.
    What is it that you're trying to achieve by sending the message?
    Who needs to send the message and why? What should be the result of handling the message?

    Are you trying to update some data in your TabFive object? Call a method instead of sending a message.

    Are you trying to update something rendered in the window corresponding to a TabFive object? Send a message - if the window is not up, there's nothing to update, no? When it is up, it will respond to the message.

    Sequence diagrams usually help (they are sometimes called interaction or activity diagrams).

    For example (I'm not sure how well aligned the diagram below will be when rendered):

    wait for user cmd
    (run app/main message loop)
                              ------> Invoke Menu Cmd -----> create TabFive object
                                                                             display TabFive window ------> run modal message loop
                                                                                                                                                                ------> Click on button -----> update or do whatever
                                                                                                                                                                --------> Click on OK  ------> update TabFive internal data and close window
                                                                             process TabFive data

    I've used leading uppercase letter for user action and leading lowercase letter for program action.

    LVL 9

    Expert Comment

    Well, the diagram doesn't look too bad on my screen. But then again, I'm running in 1920x1440 :-)


    Author Comment

    Thank you, Radu. It's working now...I gave up on trying to access the handle from the object that I created...instead, I went down to the bottom of hte code and grabbed that dialog from where it is created in another file...

    you hint on calling ::SendMessage() was helpful, and I appreciate the diagram! :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now