Thread: Closing .exe

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    11

    Closing .exe

    Hi, earlier i asked how to open a .exe file. I choose the one line method: system("file.exe");
    Now im wondering how i get the darn thing to close. If this was on the faq im srry. Thanks for any help you can give,
    tyler

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You are not opening it using that function. You are invoking command.com to run the program from the prompt (essentially). This is entirely different than opening the file via C/C++.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    help me with what your are saying. So u mean if i want to be able to close it i must use a different method of opening?

    Would the fork/exec method work? It makes a copy of the parent program then turns it into a diff program. Could when i wanted it to close turn it into a program that just closes?

    THX for the help,
    tyler
    Last edited by ninjaturtle[k9]; 07-07-2004 at 01:00 AM.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I am unfamiliar with the fork/exec command, but what Bubba is saying is that when you use the system method, your C++ program basically gives control over to command.com (which is kind of like the OS. It's just the DOS prompt.) I believe that once the program finishes running, command.com passes control back to you, but I don't think you have any control as to when that will be.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    Are people telling me that there is no way to make the child process close? Like i couldnt open it then sleep(10); then close it leaving the parent process but no child? This sucks if thats the case, my program like needs that to work.

    This program never finishes running. Can i just tell command.com to close everything but "example.exe" or close just "examplea.exe"? Is it even possible at all to close a .exe thru C++?
    Last edited by ninjaturtle[k9]; 07-07-2004 at 12:33 PM.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If this second program is something you've written, why can't you just make it part of your first program? Especially if your first is just going to sit there and do nothing while the other executes. I would recommend doing this. There are facilities to allow multple threads of execution.

    In the event that you are calling an external program, I would, first of all, advise against this as you will run into all sorts of problems (dependancies should be avoided IMO). If you do decide to go ahead, I can point you inthe right direction. Look up platform invoke (commonly p/invoke) and hooks. It may not be available with C++ (I've used it with C#), but if you can get this to work, it'll solve your problem. It's like system-wide-enhanced event handling. Very cool, but it's tough to learn - you should be alright though.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You can both close and start up programs using C++ in windows. Check CreateProcess and TerminateProcess at msdn.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Quote Originally Posted by prog-bman
    check out the Faq
    Quote Originally Posted by Hunter2
    >>system() anyone?
    *SMACK* Bad! Read the FAQ!
    You might consider reading the FAQ (click on the word 'FAQ' - it's a hyperlink. Then read about Option 1 and why it's bad.)
    Last edited by Hunter2; 07-07-2004 at 02:14 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    11
    thx people, ill look into all the things youve said. The program i run is an outside program but its necessary. and thx for the heads up on system. i dont think it will work for wat im doin neways.


    Since i dont want to make a whole new topic ill ask you here: do you know if instead of this: ofstream("tyler.txt"); i could use: ofstream(arraynamehere);

    i need to do this because everyday i need my program to go to a different log file. The log files are created with mm-dd-yyyy.txt, so every day it must change. thx
    Last edited by ninjaturtle[k9]; 07-07-2004 at 02:47 PM.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes, you can. Almost anywhere you need a char*, you can pass a char array. And for sure, anywhere you need a string (i.e. "tyler.txt"), you can pass a char array.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    If you are using a *nix shell, most of the time you can add an ampersand to the end of a command and it'll run in the background
    Code:
    pacman -S mozilla-firefox &

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Quote Originally Posted by ninjaturtle[k9
    ]char day[]={"hello.txt"};
    ifstream log("c:\\program files\\stealthbot\\logs\\"<<day, ios::ate);


    you see what im trying to achieve here? Do you also see what im doing wrong? if
    so please tell me.
    First, take off the {} around "hello.txt". Second, you can't use << on strings, that's only when you're using std::cout. You'd need to create a second string:
    Code:
    char day[] = "hello.txt";
    char str[256]; //or any other big number
    strcpy(str, "c:\\program files\\stealthbot\\logs\\");
    strcat(str, day);
     
    ifstream log(str, ios::ate);
    or
    Code:
    string day = "hello.txt";
    string path = "c:\\program files\\stealthbot\\logs\\";
    path.append(day);
    ifstream log(path.c_str(), ios::ate);
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You can use it with stringstreams.
    Code:
    #include <sstream>
    ...
    std::stringstream stream;
    stream << "c:\\program files\\stealthbot\\logs\\" << day;
    ifstream log(stream.str().c_str(), ios::ate);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-09-2008, 11:09 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. .exe size with MinGW
    By Stabbsy in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2006, 06:07 AM
  4. Changing program from .cpp to .exe
    By BIt_toRreNt in forum C++ Programming
    Replies: 6
    Last Post: 02-16-2005, 04:24 PM
  5. Problem creating .exe files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-22-2002, 02:25 PM