how to close a certain window?

I am looking for a method that is to find and close window.
I have program in a flash memory and i can get that path.
however, while the program is running i wanna close the window where the program's running when i plug the flash memory out.
It works on Windows 7, but it occurs error message like "Check the path 'f:\a\b'" and even the window is not closed.
It's a little hard to explain it.
I will post the picture of error message.
Thanks :)
The first thing I would check is the startup programs. In XP it was MSConfig (in Dos prompt)


As for the program itself I wrote something similar for an old company.

I'll explain and mention the MSDN functions in brackets.

I recalled I had a timer event (SetTimer) that fired off every second. Each second it got the foreground window (GetForegroundWindow.) Once it had the foreground window it would do a comparison (strcmp) between the caption of the window (GetWindowText) and the caption i was after. Yours might be "Check the path 'f:\a\b'".

Once you have the dialog/window you want you use SendMessage(window handle, WM_CLOSE, 0, 0); or I may of used DestroyWindow or I may of used CloseWindow. I recall one of these did not work.


Let me know if you have any probs.
I will do it like this:

Every second or so I will check if specified path does exists (if memory flash is removed you will get an error)

Get a snapshot of running processes with CreateToolhelp32Snapshot(), Process32First() and Process32Next().

Read PROCESSENTRY32 structure for szExeFile member (or even better if you use Vista or higher for full compatibily with 64-bit systems call OpenProcess() and QueryFullProcessImageName() ) and compare the path (only drive letter is enough). If you have a match then call TerminateProcess().
Last edited on
I would personally NOT DO THIS AT ALL, this is horrible practice and a bad idea. Especially concerning removable storage devices, you do know that data is almost never written to a device real time and that Windows quite often "cheats" right?

When you right click on the device in your tray and click "Safely Remove Device" you're telling Windows that it had better hurry up and close all of the file locks it has on that device and to flush the write buffer as well to ensure that any changes you made to files\data\whatever on that device are actually saved.

By simply killing that warning messege you are neutering one of the few Windows messages that actually gives you good advice.

If this is so that you can play a game or whatever and pull your thumb drive out when you see the teacher coming then why not just have your device copy the entire file to a temp directory? That way you don't have to worry about the device being spotted at all.
Last edited on
Ahh thanks computergeek my skim reading missed all of that. Somehow I thought it was a startup message :D
Topic archived. No new replies allowed.