Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

How to close a window opened with ShellExecute?

Hi,

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.

Thanks
0
l_madhavi
Asked:
l_madhavi
  • 3
  • 2
1 Solution
 
rajeev_devinCommented:
Use CreateProcess instead.
By using CreateProcess you can get the processId that you can use to close the application.
0
 
rajeev_devinCommented:
Something like this

STARTUPINFO si;
PROCESS_INFORMATION pi;

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);
0
 
mahesh1402Commented:
Madhavi,
 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.

MAHESH
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
rajeev_devinCommented:
MAHESH,
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.
0
 
mahesh1402Commented:
Rajeev,
 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.

MAHESH
0
 
l_madhaviAuthor Commented:
Hi,

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

Bye
Madhavi
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now