CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 43
  1. #1
    Join Date
    Feb 2009
    Posts
    80

    Exclamation How Can I Close exe files with C++ code ?

    Hi,
    I'm really new to C++.

    Can someone please tell me the easiest way to close running exe files with C++ code ?

    and then re-start the exe file later in the code ?

    thanks a million !

  2. #2
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    anyone ??

    I want my program to open windows calculator... and also close it later in the program.

    can someone help me with this ? I'm sure with C++ it's a simple thing, yet I can't find anything
    good on the subject of starting and closing exe files with your C++ program.

    if anyone could even steer me to a good tutorial or article that would be great too.
    better yet a short code example.

    Thanks in advance !

  3. #3
    Join Date
    Mar 2004
    Location
    KL, Malaysia
    Posts
    63

    Re: How Can I Close exe files with C++ code ?

    Hi, check out this article.

    http://www.codeproject.com/KB/thread...ProcessEx.aspx

    Kevin Choong

  4. #4
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: How Can I Close exe files with C++ code ?

    You can use CreateProcess Or ShellExecute APi to launch the program and then use SendMessage(WM_CLOSE) to the main window of that app to ask it to get closed Or call TerminateProcess APi to kill it.
    Regards,
    Ramkrishna Pawar

  5. #5
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    hey thanks Kevin.

    Looks way more complicated than I was hoping for. I will definately read it all though.

    isn't there a way to use

    fopen("myFileName.exe")

    or something more simple ?

    Thanks.

  6. #6
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    hey thanks Krishnaa,

    can you give me a code example ?

    I want to open calculator.exe,

    then have a few more un-related functions take place,

    then have the code close calculator.exe before the end of the program.

    thanks.

  7. #7
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: How Can I Close exe files with C++ code ?

    Just look for CreateProcess in MSDN or Google it.
    Regards,
    Ramkrishna Pawar

  8. #8
    Join Date
    Mar 2004
    Location
    KL, Malaysia
    Posts
    63

    Re: How Can I Close exe files with C++ code ?

    The article I pointed to you is using CreateProcess, and one of the ways to signal a running program to quit is to modify the WaitForSingleObject.

    Code:
    int iWaitSecond = 5;
    WaitForSingleObject( processInformation.hProcess, 1000*iWaitSecond );
    This should make the running app to quit after running for 5 seconds.

    btw, fopen() is used to open a file, not to execute a file.

    Hope this helps.

    Kevin Choong

  9. #9
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    cool, thanks Kevin.
    that's exacty what I meant....

    how do I find out the what to put in for " processInformation.hProcess " ?


    Code:
    int iWaitSecond = 5;
    WaitForSingleObject(   ?  , 1000*iWaitSecond );

  10. #10
    Join Date
    Mar 2004
    Location
    KL, Malaysia
    Posts
    63

    Re: How Can I Close exe files with C++ code ?

    The processInformation is a structure object of PROCESS_INFORMATION.
    It is filled in by the CreateProcess function with information about a newly created process (in your case the Calculator program).

    Kevin Choong

  11. #11
    Join Date
    Aug 2007
    Posts
    858

    Re: How Can I Close exe files with C++ code ?

    Quote Originally Posted by Jeff++ View Post
    hey thanks Kevin.

    Looks way more complicated than I was hoping for. I will definately read it all though.

    isn't there a way to use

    fopen("myFileName.exe")

    or something more simple ?

    Thanks.
    You can do

    system("calc");

    but IIRC once that executes your program will be blocked until the user closes the calculator.

  12. #12
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    thank you Speedo !
    that's what I wanted....
    now is there a way to unblock the rest of the code in the program with the calculator still open ?

    and how can I use C++ code to close the calc again.

    what is the syntax for close window ?

    would it be like system ( close.window );

    or something to that effect ?

    thanks everyone for the awsome help... I seem to learn much faster from live examples like this than eyeball-drying tutorials..

    cheers.

  13. #13
    Join Date
    Aug 2007
    Posts
    858

    Re: How Can I Close exe files with C++ code ?

    that's what I wanted....
    now is there a way to unblock the rest of the code in the program with the calculator still open ?
    Not to my knowledge. That's the limitation of using system commands.

    and how can I use C++ code to close the calc again.

    what is the syntax for close window ?

    would it be like system ( close.window );
    Using system commands, it would need to be something like

    system("taskkill /IM calc.exe");

    However, it's irrelevant here because your program is not going to continue execution until calc is closed anyway. To get the kind of control you're asking for you'll have to use more complex methods (see the first few replies).

  14. #14
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    sweet. thanks for the answers Speedo !

    I'll look into the other methods.
    really appreciate the help.

    cheers.

  15. #15
    Join Date
    Feb 2009
    Posts
    80

    Re: How Can I Close exe files with C++ code ?

    one question....

    what's the " IM " for ?


    thanks

Page 1 of 3 123 LastLast

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