C++ check if .exe file is open

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 35
Reputation: colmcy1 is an unknown quantity at this point 
Solved Threads: 0
colmcy1 colmcy1 is offline Offline
Light Poster

C++ check if .exe file is open

 
0
  #1
Jul 18th, 2009
Hey all,
I am trying to check if a .exe file is open, if it is open bring it to foreground, if it is not open open .exe file and bring to foreground. The code I have written doesnt seem to recognise if the program is open or not as it always opens the .exe file even if the .exe file is already open. I am using MSVC 6 MFC and the code I am using is:

if (!"\EvolveTraffic.exe")
{
HWND hWnd = ::FindWindow(NULL,"EvolveTraffic.exe");
:: SetForegroundWindow(hWnd);
}

else
{
ShellExecute(this->m_hWnd,"open",
"\EvolveTraffic.exe","","", SW_SHOW );

hEvent = CreateEvent(NULL,FALSE,FALSE,"EvolveTraffic");
WaitForSingleObject(hEvent,50);


keybd_event(VK_RETURN,0x1c,0 , 0); // Press Enter
keybd_event(VK_RETURN,0x9c,KEYEVENTF_KEYUP,0); // Release Enter
}


I know there is a:

ifstream("\EvolveTraffic.exe");

if (!"\EvolveTraffic.exe".is_open())
method, but as the file is a .exe file can I use "ifstream". Also if I use ".is_open()" I get a compile error of:

"error C2228: left of '.is_open' must have class/struct/union type"

Does anyone have any ideas,thanks for your help.

Colm
Last edited by colmcy1; Jul 18th, 2009 at 12:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ check if .exe file is open

 
0
  #2
Jul 18th, 2009
>>if (!"\EvolveTraffic.exe")
What is the world is that supposed to do ????

>>HWND hWnd = ::FindWindow(NULL,"EvolveTraffic.exe");

FindWindow() looks at the window's title, not the name of the executable. As a test, write a short program that calls EnumWindows() and see all the values that it returns. Run the program below and pick the title of the window you want to pass to FindWindow()

  1. BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
  2. {
  3. char text[255];
  4. GetWindowText(hwnd, text, sizeof(text));
  5. if( text[0] != 0 && !strstr(text,"Default") )
  6. std::cout << text << "\n";
  7. return TRUE;
  8. }
  9. int main()
  10. {
  11. EnumWindows(EnumWindowsProc, 0);
  12. }
Last edited by Ancient Dragon; Jul 18th, 2009 at 1:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 35
Reputation: colmcy1 is an unknown quantity at this point 
Solved Threads: 0
colmcy1 colmcy1 is offline Offline
Light Poster

Re: C++ check if .exe file is open

 
0
  #3
Jul 18th, 2009
Hey AncientDragon,

sorry I'm abit lost, lots of new functions . Surely the FindWindow() function will only work if the .exe file is open?? I was looking at using something like:

char buffer[256];
ifstream examplefile ("\EvolveTraffic.exe");
if (! examplefile.is_open())
but in the else part of the if statement the "ShellExecute" function doesnt seem to work then,perhaps as I have defined the file as examplefile (I have messed around wit it but no luck):

ShellExecute(this->m_hWnd,"open",
"\EvolveTraffic.exe","","", SW_SHOW );
Thanks again for your time, its much appreciated.

Colm
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ check if .exe file is open

 
0
  #4
Jul 18th, 2009
>>"\EvolveTraffic.exe"
Remove the \ character. Looks like a typo, but check the code you actually wrote.

>>sorry I'm abit lost, lots of new functions
Only one or two. Read all about them on MSDN. Also write your own little programs to test and try them out so you can understand them better.

>>Surely the FindWindow() function will only work if the .exe file is open??

Well yes, and the executable must be a windows program. It won't work with console programs because EnumWindows() only shows "C:\Windows\system32\cmd.exe", which isn't much help.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: C++ check if .exe file is open

 
0
  #5
Jul 18th, 2009
this thread is so ** up
Last edited by Ancient Dragon; Jul 18th, 2009 at 5:11 pm. Reason: edited out valgur word even though the filter did it too.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ check if .exe file is open

 
0
  #6
Jul 18th, 2009
Originally Posted by laconstantine View Post
this thread is so *** up
I suppose you got lost somewhere. If its beyond your comprehension then I'd suggest you don't read it. You need more than 1st grade reading skills for this thread
Last edited by Ancient Dragon; Jul 18th, 2009 at 5:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 35
Reputation: colmcy1 is an unknown quantity at this point 
Solved Threads: 0
colmcy1 colmcy1 is offline Offline
Light Poster

Re: C++ check if .exe file is open

 
0
  #7
Jul 18th, 2009
Hey AncientDragon,

Probably a bad way to end thread (sorry laconstantine ) but I worked my way around problem, thanks for the heads up with the two functions, actually used them in work around, thanks again

Colm
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC