943,685 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1417
  • C++ RSS
Jul 18th, 2009
0

C++ check if .exe file is open

Expand Post »
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:

Quote ...
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:

Quote ...
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
colmcy1 is offline Offline
39 posts
since Feb 2009
Jul 18th, 2009
0

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

>>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()

C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Jul 18th, 2009
0

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

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:

Quote ...
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):

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

Colm
Reputation Points: 10
Solved Threads: 0
Light Poster
colmcy1 is offline Offline
39 posts
since Feb 2009
Jul 18th, 2009
0

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

>>"\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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Jul 18th, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
Jul 18th, 2009
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Jul 18th, 2009
0

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

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
Reputation Points: 10
Solved Threads: 0
Light Poster
colmcy1 is offline Offline
39 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ to Lan Cable/Port
Next Thread in C++ Forum Timeline: Cook book: Multi variable minimization?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC