CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Sending/receiving message between apps

  1. #1
    Join Date
    May 2001
    Posts
    112

    Sending/receiving message between apps

    Hi All,

    It's long but it's an interesting problem.

    I have a main application that starts another application to get some user input(sounds crazy I know but it's easier this way). To make it so the user can not use the main application until the new one is closed I bring up a dialog that has a cancel button on it. In theory if the second app became locked up or was having problems the user could click the Cancel button and it would call TerminateProcess. The problem is TerminateProcess is not the best way to exit. If the app isn't locked up I don't want it to close that way. What I want to do is send/post a message to the second app to tell it to close. If it can it will close and the main app will get notification that it closed and not call TerminateProcess. If after five seconds it has not received notification that it closed then I want to close it with TerminateProcess. I can't seem to find a way to do this. I send messages back and forth using WM_COPYDATA and use OnTimer as my timer. I need to post the initial message to close because if I call SendMessage and the other app is locked up then my main app will be locked up too. So I tried posting the message and then creating a timer but the second app never gets the posted message. So I tried posting the message to the second app and then posting a message to the main app to start the timer. Again the second app never got the message.

    Has anyone ever tried something like this or have any ideas how I could do this?

    Thanks in advance for any help.
    -Ben

  2. #2

    Unhappy a bit confused

    I don't understand if you have 2 application (and what kinds..document..dialog...)
    please post more details

  3. #3
    Join Date
    May 2001
    Posts
    112
    Hi bikelink,

    There are two applications. The main one and the second one are both Doc/View apps. I run the second app from the main app and have it act like a dialog. It shows up and allows the user to enter in some information. When they're done they close the second app and control returns to the main app. The problems occure with the messages that get sent back and forth. Once the user is in the second app I don't want them to be able to do anything in the orginal app until the second app is closed. So I bring up a dialog box in the main app that says "Please close the Content Editor to continue." There is also a cancel button on that dialog incase something happens to the second app. If it gets locked up I want the user to be able to cancel out. That's where the problems begin. If the user clicks Cancel and the second app isn't locked up I want the second app to close down nicely. But if it is locked up I want to close it by calling TerminateProcess.

    I hope that makes it a little more clear. I'd paste in some code but there really isn't anything interesting to see.

    The reason I use the second app instead of a dialog is I already have all the functionality in this sencond app that I need. Putting all that functionality in a dialog would be difficult and I would be duplicating which would require more upkeep.

    Thanks for the reply,
    -Ben

  4. #4
    Join Date
    Nov 1999
    Location
    Dresden / Germoney
    Posts
    1,402
    If you have a HWND to the main second app, you can use SendMessageTimeout.

    if you just want to "ping" it to see if it's alive, use a "harmless" message such as WM_GETTEXTLENGTH. Or you just SMTO(WM_CLOSE) to the other app, and if it doesn't respond after X seconds, you can TerminateProcess.


    To find the main window of th other app... please don't use FindWindow. Rather, use EnumWindows, and GetWindowThreadprocessID, until you find a match.

  5. #5
    Join Date
    May 2001
    Posts
    112
    Hi Peter,

    Thank you. The SendMessageTimeout is exactly what I needed. Currently I am using FindWindow to get the handle. Is it safer to use the EnumWindows method that you mentioned? What are the problems using FindWindow?

    Thanks!
    -Ben

  6. #6
    Join Date
    Nov 1999
    Location
    Dresden / Germoney
    Posts
    1,402
    problems with FindWindow(Ex):

    * FindWindow(Ex) will hang, if there is any (even invisible) desktop window that belongs to a hung app

    * FindWindow relies on the application title, which is quick to change with new versions, opened documents, translation to other languages and desktop toys* Further, you might be caugth touching another app that
    (FindWindowEx improves matters somewhat, but only if you embed a GUID in the WNDCLASS name, and you actually have a chance to control the WNDCLASS name of the other app)

    * FindWindow gives you bad karma

    As welcome bonus for the converts I have my trusty EnumWndInVector function pair (see htp://buerger.metropolis.de/bitbucket/howto/closeall.html - sorry for the popup, I'm still moving..)


    Peter preachin'
    ----------------------

    *) for fun, try SendMessage(HWND_BROADCAST, WM_SETTEXT, 0, "Killroy was here") one time. After saving your work - I've found several background processes that nicely hang Win9x this way

  7. #7
    Join Date
    May 2001
    Posts
    112
    Hi Peter,

    Thanks for the info. I don't want any bad karma in my programming. I build up enough driving back and forth from work.

    -Ben

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)