CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2002
    Location
    Australia
    Posts
    198

    ShellExecute tries to launch .exe everytime even though it is already runniong.

    Hey All,

    I have written two programs. Both of them have a mutex so that only one instance of each can ever be run at a time.

    Program one uses ShellExecute to send information to program two. Here's a code snippet:

    Code:
    if ( m_je->m_saveDCPProbePDF ) parameters = "savePDF";
    else parameters = "dontSavePDF";
    
    parameters += "*";
    
    parameters += m_je->m_compilationPath;
    
    parameters += "*";
    
    parameters += m_je->m_dcpName;
    
    ShellExecute( NULL, NULL, "DCPProbe.exe", parameters, progDir, SW_SHOWNORMAL );
    The problem I have is that even though program 2 is all ready running, ShellExecute tries to launch it again, which of course it can't because of the mutex.

    Is there a way to do this so that it uses the program already running rather than trying to launch a new one eveytime?

    Thanks so much,

    Steve Q. :-)

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: ShellExecute tries to launch .exe everytime even though it is already runniong.

    Here are few ways to find out if the second application is already running:
    • get a list running processes and see if one having the given image name is among them. Here you can find hints about how to do: Listing Processes
    • if the second application has a top-level window, get its handle (with FindWindow) then call SendMessageTimeout with WM_NULL; if SendMessageTimeout returns non-zero, it means "running";
    • also you can call CreateMutex to see if the named mutex is already created or not in the other process.
    Last edited by ovidiucucu; February 6th, 2024 at 05:49 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Mar 2002
    Location
    Australia
    Posts
    198

    Re: ShellExecute tries to launch .exe everytime even though it is already runniong.

    Quote Originally Posted by ovidiucucu View Post
    Here are few ways to find out if the second application is already running:
    • get a list running processes and see if one having the given image name is among them. Here you can find hints about how to do: Listing Processes
    • if the second application has a top-level window, get its handle (with FindWindow) then call SendMessageTimeout with WM_NULL; if SendMessageTimeout returns non-zero, it means "running";
    • also you can call CreateMutex to see if the named mutex is already created or not in the other process.
    Hi ovidiucucu,

    Thanks so much for your reply.

    Ok so once I determine the application is running. How do I then send data to it without the system trying to relaunch the app?

    Thanks heaps,

    SQ :-)

  4. #4
    Join Date
    Nov 2018
    Posts
    120

    Re: ShellExecute tries to launch .exe everytime even though it is already runniong.

    > ShellExecute( NULL, NULL, "DCPProbe.exe", parameters, progDir, SW_SHOWNORMAL );

    DCPProbe.exe is getting it's parameters via main(argc,argv)
    You have no choice but to start another process given the current way of telling DCPProbe.exe what to do.

    If you want the process to stick around and process several commands, then either
    - make one very long parameters string of everything you want it to do.

    Or https://learn.microsoft.com/en-us/cp...?view=msvc-170

    Where your current process does this just once
    FILE *fp = _popen("DCPProbe.exe","w");

    And does this when it needs DCPProbe to do some work
    fputs(parameters.c_str(), fp);

    And does this when it's finished.
    _pclose(fp);

    Meanwhile, DCPProbe now looks like this
    Code:
    int main ( ) {
        char buff[BUFSIZ];
        while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
            // buff contains a line of text looking like parameters.
        }
    }

  5. #5
    Join Date
    Mar 2002
    Location
    Australia
    Posts
    198

    Re: ShellExecute tries to launch .exe everytime even though it is already runniong.

    Quote Originally Posted by salem_c View Post
    > ShellExecute( NULL, NULL, "DCPProbe.exe", parameters, progDir, SW_SHOWNORMAL );

    DCPProbe.exe is getting it's parameters via main(argc,argv)
    You have no choice but to start another process given the current way of telling DCPProbe.exe what to do.

    If you want the process to stick around and process several commands, then either
    - make one very long parameters string of everything you want it to do.

    Or https://learn.microsoft.com/en-us/cp...?view=msvc-170

    Where your current process does this just once
    FILE *fp = _popen("DCPProbe.exe","w");

    And does this when it needs DCPProbe to do some work
    fputs(parameters.c_str(), fp);

    And does this when it's finished.
    _pclose(fp);

    Meanwhile, DCPProbe now looks like this
    Code:
    int main ( ) {
        char buff[BUFSIZ];
        while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
            // buff contains a line of text looking like parameters.
        }
    }
    Hi salem_c,

    Thanks so much for this. I'm going to give it a go first thing tomorrow!

    Really appreciate your help.

    Steve Q. :-)

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: ShellExecute tries to launch .exe everytime even though it is already runniong.

    You could also consider using a pipe
    https://learn.microsoft.com/en-us/wi...pc/named-pipes
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Mar 2002
    Location
    Australia
    Posts
    198

    Re: ShellExecute tries to launch .exe everytime even though it is already runniong.

    Thanks everyone.

    I got this going using FindWindow to determine if DCPProbe was running, and then sent it a message:

    Code:
    ::SendMessage( dcpProbe, WM_COPYDATA, ( WPARAM ) m_hWnd, ( LPARAM ) &cds );
    Thanks again for your help.

    SQ :-)

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