Thread: Custom Windows Message.

  1. #1
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195

    Custom Windows Message.

    Hi. im trying to get messages from one application to the next. The two apps are not sharing any resources and are totally independant. So, I decided to use the windows message pump to pass messages back and forth. Can anyone provide me wih some info about this topic because I apperently I cannot find any

    The messages are going to be custom made, so they dont interfere with the windows messages that get passed around.

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Well in order to use custom messages, they need to be defined as a value greater than WM_USER. So, if I wanted a message like WM_MYMESSAGE:-
    Code:
    #define WM_MYMESSAGE    (WM_USER + 1)
    The only problem is: what's stopping another program from doing the same thing for its own messages?
    The answer: RegisterWindowMessage.
    Code:
    unsigned int uiMessage;
    
    uiMessage = RegisterWindowMessage("WM_MYMESSAGE");
    The return value of RegisterWindowMessage is guaranteed to be unique across the entire system and within the range WM_USER to 0x7FFF. As many applications that use the message may call RegisterWindowMessage, they will all get the same value to use.

    So, you've done all that, now what about passing stuff? Passing an integer here and there is easily done, wParam and lParam are at your disposal...
    Code:
    LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
       switch (uMsg)
       {
          ...
          case WM_MYMESSAGE:
          {
             if (wParam == 1)
                MessageBox(hwnd, "Woot!", "Hello!", MB_ICONEXCLAMATION);
    
             break;
          }
    
       }
    
       return FALSE;
    }
    BUT... what if you want to pass a string, or a pointer to a structure? Then things get tougher. The thing is, each process's memory is protected from other processes. They can't just pop inside each other's address space and compare notes. What you need is, specifically for messaging, some shared memory. This is known as custom marshalling.

    And it's not something I know how to do.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Consider proxy.

    WM_COPYDATA

    Kuphryn

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>#define WM_MYMESSAGE (WM_USER + 1)

    This should technically be
    #define WM_MYMESSAGE (MAXINTATOM+1)

    as WM_USER + 1 is for private msgs sent internally within the app and may conflict with already defined msgs.

    MAXINTATOM = 0xC000 = 49152

    Look here....

    http://blogs.msdn.com/oldnewthing/ar.../02/55914.aspx
    Last edited by novacain; 03-16-2006 at 07:10 PM.
    "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

  5. #5
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    This might do the trick for now because I doubt ill be passing anything more complex then just a bunch of numbers. If it does come up that I need custom marshalling, I would really like if you guys could link a few pages where I can read about this.

    Followup question to the second post: If i define WM_USER + 1 as my own message, would this interfere with other messages or programs that already registered WM_USER + 1 for thier own use? Since its system wide, This may be a problem of securely transmitting info from one app to the other.

    If there is a simpler solution to talking between apps, I would love for you to tell me

    EDIT: Would using PostMessage() be a simple way of doing this? All I need to know is the hWnd of the window im sending messages to? Then in this case I dont really have to register a new message, they will be passed directly between my two apps. Am I right or totally whacked?
    Last edited by Mastadex; 03-17-2006 at 04:11 AM.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>Would using PostMessage() be a simple way of doing this?

    Yes. Use HWND_BROADCAST with your already registered message.

    Use SendMessage() if you want to wait for a response.

    Without a HWND_BRAODCAST you will have to find the HWND of the target window (not app) each time you want to send a msg (in case it has changed HWND by being closed ect). This can be an issue when there are multiple instances of an app or the window is not visible/active or has a conflicting name.
    "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

  7. #7
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    Ok Thnx for the help. Tho i do have more questions, How would i get the hWnd of a registered window, is there a lookup function?

  8. #8
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by Mastadex
    Ok Thnx for the help. Tho i do have more questions, How would i get the hWnd of a registered window, is there a lookup function?
    FindWindow

  9. #9
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    Quote Originally Posted by novacain

    Yes. Use HWND_BROADCAST with your already registered message.

    Use SendMessage() if you want to wait for a response.

    Without a HWND_BRAODCAST you will have to find the HWND of the target window (not app) each time you want to send a msg (in case it has changed HWND by being closed ect). This can be an issue when there are multiple instances of an app or the window is not visible/active or has a conflicting name.

    So is there a way to find the handle of a certain isntance??? Also, is there a way to find the handle of a service thats running if i know the name of the server....Only one instance of the service is running. I am sure of this.

    EDIT: Also is ther a way to get the handle of the window that sent a particular message??
    Last edited by Mastadex; 05-10-2006 at 12:42 PM.
    Founder and avid member of the Internationsl Typo Associateion

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    unsigned int uiMessage;
    
    uiMessage = RegisterWindowMessage("WM_MYMESSAGE");
    
    ...
    
    LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
       switch (uMsg)
       {
          ...
          case WM_MYMESSAGE:
          {
             if (wParam == 1)
                MessageBox(hwnd, "Woot!", "Hello!", MB_ICONEXCLAMATION);
    
             break;
          }
    
       }
    
       return FALSE;
    }
    I was wondering how you would know what WM_MYMESSAGE would be in the WndProc. Would you have to make it a global and then like case uiMessage?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message by windows on keypress
    By cfrost in forum Windows Programming
    Replies: 3
    Last Post: 07-20-2004, 03:58 AM
  2. custom message map in MFC
    By bonkey in forum Windows Programming
    Replies: 2
    Last Post: 09-24-2003, 01:53 PM
  3. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  4. Codec Bitrates?
    By gvector1 in forum C# Programming
    Replies: 2
    Last Post: 06-16-2003, 08:39 AM