| | |
C++ check if .exe file is open
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 35
Reputation:
Solved Threads: 0
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:
I know there is a:
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
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())
"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.
>>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()
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)
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); }
Last edited by Ancient Dragon; Jul 18th, 2009 at 1:14 pm.
•
•
Join Date: Feb 2009
Posts: 35
Reputation:
Solved Threads: 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:
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):
Thanks again for your time, its much appreciated.
Colm
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())
•
•
•
•
ShellExecute(this->m_hWnd,"open",
"\EvolveTraffic.exe","","", SW_SHOW );
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.
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.
![]() |
Similar Threads
- Help! Check to see if Excel file is Open (Visual Basic 4 / 5 / 6)
- COMPILE A "STANDALONE" .EXE FILE (Visual Basic 4 / 5 / 6)
- I want to use C# to open .exe file and print it in .txt? (C#)
- how to open a exe file using java (Java)
- open exe file using c# (C#)
- Can get IExplorer to open, but not through main exe file (Viruses, Spyware and other Nasties)
- How do I start an exe file from C++ (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ to Lan Cable/Port
- Next Thread: Cook book: Multi variable minimization?
Views: 614 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






