How to close a window opened with ShellExecute?


I am woking with a dialog based application.On click of a button I opened a text file using ShellExecute(NULL, "C:\\Text.txt", szPath, NULL, NULL, SW_MAXIMIZE ); On clicking another button I should be able to close this file.

Can anyone please help with some function that automatically closes a file when passed the file path to it or how do I close a window opened with SehllExecute.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Use CreateProcess instead.
By using CreateProcess you can get the processId that you can use to close the application.
Something like this


ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

if(!CreateProcess( NULL, "notepad.exe C:\\Text.txt", 0, 0, FALSE, 0, 0, 0, &si, &pi)) {
        // Failure

// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

DWORD processId = pi.dwProcessId; // Use this processId to close the application.

Something like this
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId);
TerminateProcess(hProcess, 0);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 You may also use FindWindow() function for this purpose if you have full title of open window.
e.g. Above example opens notepad window with title 'Text.txt - Notepad'. You may use FindWindow() to get handle of this window by passing this title name and then send messag WM_CLOSE to close window.

e.g. for above example on another button put code something like this :

      ::SendMessage(::FindWindow(NULL,"Text.txt - Notepad"),WM_CLOSE,0,0);

Otherwise CreateProcess() is preferred to execute controlled process as said above by Rajeev.

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

There is one risk if we use FindWindow(...).
What if the default application is not notepad.
Instead it is some other application like Notepad++ or metapad or wordpad.
In that case the title will vary.
 Yes you are right . Above method works only if you know title of your window. Alternately if you can retrieve if using current active window title using functions like GetWindowText(). CreateProcess() is preferred as above said.

l_madhaviAuthor Commented:

Thank you all for your inputs..My problem is solved & sorry for the delay in replying back.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.