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 !
Printable View
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 !
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 !
Hi, check out this article.
http://www.codeproject.com/KB/thread...ProcessEx.aspx
Kevin Choong
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.
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.
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.
Just look for CreateProcess in MSDN or Google it.
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.
This should make the running app to quit after running for 5 seconds.Code:int iWaitSecond = 5;
WaitForSingleObject( processInformation.hProcess, 1000*iWaitSecond );
btw, fopen() is used to open a file, not to execute a file.
Hope this helps. :)
Kevin Choong
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 );
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
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.
Not to my knowledge. That's the limitation of using system commands.Quote:
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 ?
Using system commands, it would need to be something likeQuote:
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 );
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).
sweet. thanks for the answers Speedo !
I'll look into the other methods.
really appreciate the help.
cheers.
one question....
what's the " IM " for ?
thanks
Open
CloseCode:::ShellExecute(NULL,
"open",
"Calc.exe",
"",
"",
SW_SHOWNORMAL);
Code:CWnd *window = CWnd::FindWindow(NULL, LPCTSTR("Calculator"));
if (window != NULL)
{
window->PostMessage(WM_CLOSE);
}
Hey,
thanks John. Examples really seem to help me grasp this stuff quicker.
thanks a million !
cheers.
hey John,
the open calc.exe code works great !
the close one I'm doing something wrong.... perhaps I need to include another header ?
I get
error `CWnd' was not declared in this scope.
here's what I tried....
Code:#include <windows.h>
#include <stdio.h>
int main()
{
CWnd *window = CWnd::FindWindow(NULL, LPCTSTR("Calculator"));
if (window != NULL)
{
window->PostMessage(WM_CLOSE);
}
}
I'm sure I'm missing something being a newbie and all.
any clues are appreciated.
Thanks everyone. : )
*window is a pointer right ?
so I guess I have to declare that first...
again... I'm so " newbie " I don't know, that's my best guess.
any insight is appreciated.
cheers.
: )
My example was for an MFC application. If you are using straight Win32 then you will have to change it to the following.
Actually both methods should work with MFC.
Code:HWND hwnd = ::FindWindow(NULL, LPCTSTR("Calculator"));
if (hwnd != NULL)
{
::PostMessage(hwnd, WM_CLOSE);
}
Hey thanks again John.
I'm still having trouble getting it to work. get an error now about bool too few arguments for HWND_*
I obviously need more knowledge to figure this stuff out.
Can You recommend a good book for me perhaps ? there seems to be so frickin' many I can't tell which is a good one.
I need one that starts from the beginning. However I am a good problem solver, I understand electronics very well, and I am good with HTML and a little PHP so....
not tooooooo beginner either.
any insight on a good book would be greatly appreciated.
cheers.
Can you post some minimal code to replicate the compiler error?
sure thanks for the help John.
Code:#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;
int main()
{
::ShellExecute(NULL,
"open",
"calc.exe",
"",
"",
SW_SHOWNORMAL);
cout<< " alright ! ";
cin.get();
HWND hwnd = ::FindWindow(NULL, LPCTSTR("Calculator"));
if (hwnd != NULL)
{
::PostMessage(hwnd, WM_CLOSE);
}
}
PostMessage takes four arguments, not two. Pass zeros for WPARAM and LPARAM.
hey thanks plasmator,
that's got it.
however....Code:#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;
int main()
{
::ShellExecute(NULL,
"open",
"calc.exe",
"",
"",
SW_SHOWNORMAL);
cout<< " alright ! ";
cin.get();
HWND hwnd = ::FindWindow(NULL, LPCTSTR("Calculator"));
if (hwnd != NULL)
{
::PostMessage(hwnd, WM_CLOSE, 0 , 0 );
}
}
I don't underSTAND it. what should I read up on to understand
the -
part...Code:HWND hwnd = :FindWindow(NULL, LPCTSTR("Calculator"));
and the 4 arguments you mentioned.
what " topic " would this be considered ?
thanks.
The issue you need to realize is that you have two stumbling blocks. The first is learning the C++ language. The second is learning Windows programming. Learning the C++ language itself is difficult. Learning the Windows API is a totally different topic, and should be undertaken once you know the language you will be using. It makes no sense learning the API of an operating system using a computer language you don't know how to use properly.
See above. Get a good C++ only book. There is no need to introduce Windows programming.Quote:
Can You recommend a good book for me perhaps ? there seems to be so frickin' many I can't tell which is a good one.
Afterwards you use that knowledge to interface to the OS, in this case Windows. You then get Windows programming books. Most of these books are not language tutorial books -- they assume you know how to use 'C' or C++ already.
But it seems you're just throwing random things together to learn, and learning C++ doesn't work that way.Quote:
I need one that starts from the beginning. However I am a good problem solver,
I have seen very bad programmers who supposedly knew "eletronics", and HTML and PHP are a far cry from C++ in terms of learning and complexity.Quote:
wit I understand electronics very well, and I am good with HTML and a little PHP so....
Regards,
Paul McKenzie
hey Paul. I'm actually a teacher, so I know that everyone learns different. I will take your advice, however I am better at reverse engineering to see how something works. I learn much faster that way. I didn't want to use windows programming, it was the only short code way of opening the calculator in my first game. I'm sure my knowledge of C++ will get better.
maybe so, but trying things out bring up problems and errors. Then I learn why there's a problem or error. Then when I do come across that situation in a tutorial... I understand what they're saying more... that's all. In other words, it helps me learn LATER by making mistakes now. It's just how I work. Might not be for everyone. I do listen to people when they tell me to try it a better way. thanks for your help though.
cheers.
There is one big flaw in all of this -- how do you know what to do or learn next? You must get material from somewhere, or else you never progress.
For example, how do you know about std::string, std::vector, algorithms, streams, etc. unless you read about them and see how to use them?
Second, when you say "tutorial", where exactly are you getting this tutorial? Many, if not most websites and so-called tutorials are notorious for giving incorrect and outdated information on the C++ language. This is why books are the defacto way to learn C++ properly -- they are peer-reviewed, unlike websites and other on-line tutorials.
Regards,
Paul McKenzie
um well I have read about all of that, I just like to try things and not JUST read about it. otherwise it's boring. Also, like I said... if I go ahead and try something and it doesn't work... I learn a lot from finding out why it doesn't work. I'm just having fun with this and it's not like I want to make this a carreer or anything. I've been getting most of my info from here
http://www.cprogramming.com/tutorial.html#c++tutorial
The problem with all of this is that you're asking questions here, where most of the people here have programmed as a profession or at the very least, on a very advanced "amateur" level.
What I'm driving at is this -- it is difficult if not impossible to answer questions from someone who uses a "throw anything on the wall and see what sticks" or ad-hoc approaches to programming in C++. The reason is that it is expected that you have the prerequisite knowledge of the topics required to understand our answers and responses to you. Otherwise it gets frustrating to you and the people trying to help you. You can't "reverse engineer" yourself into an answer that requires you to read up on topics long before you actually use them.
Every good C++ book has examples that you type in and understand how they work. You can't just dive into complex GUI programming without knowledge of the language. That's already been demonstrated by the many questions you see in the Visual C++ forum from people who use the GUI wizards, and then ask the most basic of C++ questions that are answered in chapters 1 or 2 of any C++ book.
If you embarked on learning C++ on your own without intervention from anyone, then you can learn whatever way you want. However, when you are asking others to help you while going down this unorthodoxed approach of attempting to learn C++, then that is where it gets problematic.
Regards,
Paul McKenzie
no offence Paul, but I have been told I learn things backwards all my life. You're not the first. I hold some pretty serious patents because of my backwards out of the box thinking, including a the first floatless carburetor design for a company called Briggs and Stratton. Go look in your garage... you might already have one. I have no intensions of becoming a professional programmer. I am just into this for fun. I am having fun, making my own games already after just a few weeks. If you think that is wrong, or bad technique somehow.... I'm sorry but I disagree. I haven't had any problems yet doing what I want to do, and any snags I've run into because I don't understand something... I read about it. If I can't find an answer, I ask someone who knows. I don't see the problem with this approach, but if it bothers you that much, please feel free to not answer any of my questions because they seem silly to you.
I think his point is not that the questions are silly, but that you won't necessarily understand the answers, because your knowledge of C++ is gained in a haphazard fashion. If you'd learnt some of the basics first then you may not have needed to ask the question in the first place.
it may seem rediculas or impossible to you, but I just don't learn the " normal " way. I've been told that all my life, and I've been told not to do it that way, or that I can't do it that way...... just like my small engine repair teacher in high school said... " you need to learn this first... and that will never work " and when I asked why won't it work ? his answer was similar to yours. " You need to understand more of the principals behind it fist " but now I make about $200,000 per year off my invention that " couldn't work ". Not to mention a few smaller patents. Learning the traditional way has never made sense to me. Why do things the same way everyone else does. How could one expect to come up with something new if following the same path as everyone else ?
I am a person with an inventors mind. I'm sorry if my way of thinking seems totally backwards, unorthodox, or just plain wrong to you. I learn things by cramming through basics, right into the " expert level " stuff.... and fill in the blanks later. For me I can learn things 50 to 100 times faster this way. I would agree with the fact it's not for everyone.
Here's an example.
I did DAYS of reading... and couldn't find a real solid answer on how to open an executable file from my C++ code. I asked in this forum and several others for some help or tips. I got LOTS.
All were different ways to do it. They all seems really long and extravagant to me... just to open the silly little windows calculator. When I asked people that know, why such long code ? isn't there a one line code that will open the calculator for me ? I was told by many experts... NO. I was told It can't be done, I was told it involves more knowledge than I have, I was told if I knew more... I'd understand why it won't work. Then I was shown a short code that would do it, but then wouldn't allow the program to continue without the calculator being closed first....
and then you John showed me this fabulous little code
and it worked just dandy ! ( thanks again by the way ). So there.... all that talk of " it can't be done that way " " If you want the program to continue... you NEED to do it the long way " and " you need to understand C++ better before writing a game that will do that "Code:::ShellExecute(NULL,
"open",
"Calc.exe",
"",
"",
SW_SHOWNORMAL);
all went out the window in one short little line of code.
Once you handed me this great little code... I did the reasearch to understand shell commands and whats going on under the hood in the code. And THAT's how I choose what to read up on next. Not what I should be reading in any particular order. If that seems messed up or wrong to any of you folks out there.... I'm sorry. It's just the way I invent things and learn. Again... Not for everyone. And I most certainly do appreciate all the help I've been getting in this forum.
cheers.
You've steered right into what myself and others are talking about.
Quote:
and then you John showed me this fabulous little code
This code is not standard C++ code. It is Windows API code. This code will not work on another OS. If you are working in Windows, you should have posted your question in the Windows API forum. Maybe that's why you couldn't find answers in C++ books. C++ isn't specific to the Windows API or any other OS specific functions and functionality.Code:::ShellExecute(NULL,
"open",
"Calc.exe",
"",
"",
SW_SHOWNORMAL);
Regards,
Paul McKenzie
yes , I know that all now. After having fun with my game, which only ever needed to run on windows. Why would I try to open WINDOWS CALCULATOR if it wasn't running on a windows platform ???
Using it lead me to the research to understand it. Otherwise I wouldn't have even known it existed. It may not be C++ but It worked, and that's all I cared about. I never really cared if it would run on anyone elses computer. Not sure why it bothers you so much Paul. I'm just having fun, and I haven't run into any " snags " as you put it. I'm sure I'll be making better code that " makes sense to you " in a year or so, with all the do's and don'ts weeded out.
and how on God's green earth would I post my question in the windows API section, when up until a few days ago I had no idea what the heck that was ?
or the fact that I didn't know there was a windows API section in this forum until right now ?
or the fact that my game started out with C and then I used a windows function ?
don't worry so much Paul. You don't need to answer any questions of mine if you don't want to.
You could have explained that in your last post, but it was left to the reader that you didn't understand that there is a difference between the C++ language and the Windows APIDon't ask me -- you posted the question in this forum that is not for Windows API programming. A "Windows Calculator" could mean anything here -- you could have tried to start it using Wine under Linux. This forum is for non-Windows API, general C++, and other OSes such as Linux/Unix/Macintosh. There is a Windows API forum already set up for questions concerning Windows.Quote:
After having fun with my game, which only ever needed to run on windows. Why would I try to open WINDOWS CALCULATOR if it wasn't running on a windows platform ???
That is not the point. Post your questions in the appropriate forum, that is all that is being asked of you.Quote:
It may not be C++ but It worked, and that's all I cared about. I never really cared if it would run on anyone elses computer.
Persons should follow posting guidelines and etiquette. Otherwise what's the use of forums?Quote:
Not sure why it bothers you so much Paul.
Regards,
Paul McKenzie
:rolleyes: Yes, I've had that before. We once had great trouble with a synchronous detector (a sort of rectifier with oscillator) that kept going out of sync. I suggested a precision rectifier instead (diodes with an op-amp). "No, that won't work anywhere near as well when there's noise in the signal" they said. So I build one, injected noise and put in a switch between the two and asked them to identify which was which in a blind test. We used the precision rectifier. :DQuote:
I was told by many experts... NO. I was told It can't be done,
I'm sure the 'dive in and work it out as you go along' technique has its advantages (shortcuts to novel solutions) as well as its disadvantages (blind alleys & inelegant solutions), but I think the majority of people here will tell you that it's a terrible way to learn a complex programming language. Long term contributors to CodeGuru have seen it time and again where people have attempted to code for something where it is obvious they have not grasped the fundametals and have made a complete 'pigs-ear' of their code. Memory leaks, undefined behaviour etc abound.
The problem with learning a programming language is that,without the fundamentals, bad coding practices are nearly always learnt first and, once this is discovered, the coder has to do a complete relearning exercise.
And the code they produce in the interim causes their colleagues to want to hang them up by their thumbs until they repent. :D
Hi,
for closing single exe we can use following code.
HWND hWnd = ::FindWindow(NULL,"procman");
::SendMessage(hWnd,WM_CLOSE,0,0);
i have multiple exe files running on the window.how to close all windows.
Hi,
for closing single exe we can use following code.
HWND hWnd = ::FindWindow(NULL,"procman");
::SendMessage(hWnd,WM_CLOSE,0,0);
i have multiple exe files running on the window.how to close all windows.