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

Recommended Answers

All 6 Replies

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

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
   char text[255];
   GetWindowText(hwnd, text, sizeof(text));
   if( text[0] != 0 && !strstr(text,"Default") )
    std::cout << text << "\n";
   return TRUE;
}
int main()
{
     EnumWindows(EnumWindowsProc, 0);
}

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

>>"\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.

this thread is so ** up

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

commented: Great help yet again, thank you +1

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.