2

So in my code I'm opening for example notepad using ShellExecute and I want to close it after but I cannot find any working way, so my question is, what is the easiest way to close particular aplication using c++ code?

#include <iostream>
#include <Windows.h>

int main()
{

    ShellExecute(NULL, "open", "notepad", NULL, NULL, SW_SHOWDEFAULT);

    Sleep(10000);

    //here I'm missing the part that closes previously opened notepad

    return 0;

}

drescherjm
  • 10,365
  • 5
  • 44
  • 64
Arrow25
  • 23
  • 3
  • 5
    Use `ShellExecuteEx` or `CreateProcess` so that you get the handle of the newly launched process. Then see [Using CreateProcess() and exit/close the opened Application](https://stackoverflow.com/questions/38081028/using-createprocess-and-exit-close-the-opened-application). – dxiv Mar 08 '21 at 01:18
  • Seems like a duplicate question to this post; https://stackoverflow.com/questions/12972410/close-an-open-file-via-windows-h-function – adarian Mar 08 '21 at 01:18
  • 1
    @adarian No, it's not a duplicate of that. That post is about closing a file handle. – Etienne de Martel Mar 08 '21 at 01:46

1 Answers1

2

You can use ShellExecuteEx with SEE_MASK_NOCLOSEPROCESS, then you can pass it as a parameter it to the TerminateProcess function.

Here is a sample:

#include <windows.h>

int main()
{
    SHELLEXECUTEINFO lpExecInfo{};
    lpExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    lpExecInfo.lpFile = L"notepad.exe";
    lpExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    lpExecInfo.hwnd = NULL;
    lpExecInfo.lpVerb = NULL;
    lpExecInfo.lpParameters = NULL;
    lpExecInfo.lpDirectory = NULL;
    lpExecInfo.nShow = SW_SHOWNORMAL;
    ShellExecuteEx(&lpExecInfo);

    Sleep(3000);
    if (lpExecInfo.hProcess)
    {
        TerminateProcess(lpExecInfo.hProcess, 0);
        CloseHandle(lpExecInfo.hProcess);
    }
    return 0;
}
Zeus
  • 3,703
  • 3
  • 7
  • 20
  • What does mean 'L' in this line of code ` lpExecInfo.lpFile = L"notepad.exe"; `? When I use whole code, it shows that i cannot assign const wchar_t to LPCSTR, when I delete this 'L' i can run program without any error but it doesn't close my notepad then – Arrow25 Mar 08 '21 at 07:56
  • @Arrow25 Because I call UNICODE's api `ShellExecuteExW` and maybe your default is `ShellExecuteExA`, of course, I recommend you to use the UNICDE version. The program can close Notepad normally here, I suggest you rebuild the program and try it. – Zeus Mar 08 '21 at 08:02
  • So if I'm thinking correctly I need to change my encoding type to unicode in ide in order this to work properly? – Arrow25 Mar 08 '21 at 08:38
  • @Arrow25 No, it has nothing to do with which version of the API you call. Have you checked that `lpExecInfo.hProcess` is empty? And if the `TerminateProcess` function returns TRUE. – Zeus Mar 08 '21 at 08:44
  • Oh okay, I fixed my problem, I didn't know you are calling notepad too, I thought this whole code is just for closing it and I was calling ShellExecute eariler too so i had 2 notepads, sorry I don't know functions used here I need to read about them, after deleting 'L' everything works fine for me how it is thank you, also is 'lpExecInfo{}' like name assigned to SHELLEXECUTEINFO? – Arrow25 Mar 08 '21 at 09:07
  • Just initialize the structure. – Zeus Mar 08 '21 at 09:10
  • Okay, and then it is assigning values to certain 'members' of this structure and what does 'ShellExecuteEx(&lpExecInfo)' function at the end ? – Arrow25 Mar 08 '21 at 09:25
  • Call the `ShellExecuteEx` function, just like you call `ShellExecute`, but with different parameters. – Zeus Mar 08 '21 at 09:26
  • so this function executes lpExecInfo structure right, but why this function is called at the end of lpExecInfo structure ? – Arrow25 Mar 08 '21 at 10:20
  • @Arrow25, It initializes all the parameters needed by the `ShellExecuteEx` to a `SHELLEXECUTEINFO` structure, and then pass the structure to the `ShellExecuteEx`. This function will get the parameters saved in the structure to create the process. – Drake Wu Mar 16 '21 at 09:31