-1

In c++ I want to open my text file with notepad, BUT:

  • Not to stop the program or wait to close the notepad...
  • Not to create a console/cmd tab (I'am using Visual studio 2017, windows)

Is it possible ?

I have this : _popen("notepad.exe C:\X\X\X.txt", "r"); but it open a cmd tab.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
Rafo 554
  • 99
  • 5
  • 1
    [Create a new process](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa)? – Some programmer dude Jun 09 '20 at 09:18
  • 3
    [ShellExecute](https://stackoverflow.com/questions/11007537/what-is-the-correct-way-to-use-shellexecute-in-c-to-open-a-txt) might be more appropriate so that the user's preferred text editor opens instead – Alan Birtles Jun 09 '20 at 09:19
  • Does this answer your question? [How to run a function in new process?](https://stackoverflow.com/questions/19051248/how-to-run-a-function-in-new-process) – Robert Andrzejuk Jun 09 '20 at 09:29
  • Also: https://learn.microsoft.com/en-us/windows/win32/procthread/creating-processes – Robert Andrzejuk Jun 09 '20 at 09:35
  • C++ doesn't provide any means to launch a process. You're going to have to use platform-specific so APIs for that. On Windows use `CreateProcess` to run a specific binary, or `ShellExecuteEx` to use the user's preferred handler given a particular file type. – IInspectable Jun 09 '20 at 09:39

1 Answers1

0

(Windows only solution)

By modifying the example: https://learn.microsoft.com/en-us/windows/win32/procthread/creating-processes

for C++:

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

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage: " << argv[0] << " [cmdline]\n";
        return EXIT_FAILURE;
    }

    STARTUPINFOA        si = {sizeof(si)};
    PROCESS_INFORMATION pi = {};

   // Start the child process.
    if (!CreateProcessA(nullptr, // No module name (use command line)
                        argv[1], // Command line
                        nullptr, // Process handle not inheritable
                        nullptr, // Thread handle not inheritable
                        false,   // Set handle inheritance to FALSE
                        0,       // No creation flags
                        nullptr, // Use parent's environment block
                        nullptr, // Use parent's starting directory
                        &si,     // Pointer to STARTUPINFO structure
                        &pi))    // Pointer to PROCESS_INFORMATION structure
    {
        std::cout << "CreateProcess failed (" << GetLastError() << ").\n";
        return EXIT_FAILURE;
    }

    // Wait until child process exits.
    //WaitForSingleObject(pi.hProcess, INFINITE);

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

    return EXIT_SUCCESS;
}
Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31