Hi,
I want to open and close an .exe file (e.g; Notepad.exe) from a C program... I can open it usingBut I don't know how to close the application from the program... Any ideas?Code:system(".exe path");
Thanks,
- Manu
Hi,
I want to open and close an .exe file (e.g; Notepad.exe) from a C program... I can open it usingBut I don't know how to close the application from the program... Any ideas?Code:system(".exe path");
Thanks,
- Manu
(Hinted Windows as OS)
How To Terminate an Application "Cleanly" in Win32
You should also check out:
::CreateProcess(..)
::PostThreadMessage(..)
To start up and post a WM_CLOSE message respectively.
For UNIX Environmet...
You can fork() and then exec() (one of the exec family dunctions i mean). The control flow of the child goes to the called program. The process exits when the child terminates...
Thank you!
It almost worked, except that I have another problem:
Code:#include <stdio.h> int main() { system("C://WINNT//NOTEPAD.EXE"); system("taskkill /F /IM notepad.exe"); }
These commands, when they are individually called from the command prompt, they are able to open and close the applications (like this notepad.exe)... But if I use it as the above program, the program block as soon as the "notepad" is opened. The control is not handed back to the program's next line until notepad is closed (for which I wrote the next line!)
Can you tell me how do I prevent this? Or, are there any workarounds?
Is there anything equivalent to fork() in Windows?
I appreciate you help!
-Manu
What about
Code:#include<process.h> #include<stdlib.h> #include<stdio.h> void start(); void end(); int main() { _beginthread( start, 0, NULL ); // open notepad sleep( 1000 ); //pause for 1 sec _beginthread( end, 0, NULL ); // kill start end() thread. system( "PAUSE" ); return 0; } void start() { system( "notepad" ); } void end() { system("taskkill /F /IM notepad.exe"); }
All this "system()" stuff, bleh.
Edit: This is the C forumCode:#include <windows.h> BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { DWORD processId; GetWindowThreadProcessId(hwnd, &processId); if(lParam == processId) PostMessage(hwnd, WM_CLOSE, 0, 0); return TRUE; } int main(int argc, char *argv[]) { STARTUPINFO siInfo; PROCESS_INFORMATION piInfo; ZeroMemory(&siInfo, sizeof(siInfo)); ZeroMemory(&piInfo, sizeof(piInfo)); CreateProcess ("C:\\WINDOWS\\Notepad.exe", "", 0, 0, FALSE, CREATE_DEFAULT_ERROR_MODE, 0, 0, &siInfo, &piInfo); WaitForSingleObject(piInfo.hProcess, 1000); EnumWindows(&EnumWindowsProc, piInfo.dwProcessId); return 0; }
Last edited by Tonto; 10-03-2005 at 05:19 PM.
Thanks a lot guys... It worked finally....
I had probelms executing even the small statement: _beginthread... Later I found out that I need to specify "multi-threading" options to the compiler while compiling this program...
I am still facing problems with the sleep() method... Any better functions to suspend the execution for a while and then resuming it?
Thanks a lot,
-Manu
>>I am still facing problems with the sleep() method
What problems then?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
The error is while linking:
This was similar to the error I got for _beginthread()... I specified the "multi-threading" option for compiling, and it got solved... I'm still not able to figure the problem with sleep()Code:Error: Thread_Run_Tests.obj : error LNK2001: unresolved external symbol _sleep Debug/Thread_Run_Tests.exe : fatal error LNK1120: 1 unresolved externals
Thanks a lot for the good responses, guys
- Manu
Try Sleep with a capital "S" instead
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
Same problem
Code:Linking... Thread_Run_Tests.obj : error LNK2001: unresolved external symbol _Sleep Debug/Thread_Run_Tests.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe.
Your linker cannot find the object file containing that function. Are you linking with the correct parameters? What compiler are you using?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]