CreateProcess Programm mit Datei öffnen



  • Ich rate Dir: verwende "std::wstring"...



  • Würd ich auch, nur dann muss man eben bedenken, dass man den String nicht direkt an CreateProcessW() übergeben kann, sondern zuerst z.B. in einen entsprechenden std::vector kopieren muss...



  • Ich verwende immer _wcsdup und free



  • Hier mal ein kleines Beispiel wie ich es evtl. machen würd:

    #include <algorithm>
    #include <memory>
    #include <string>
    
    #define WIN32_LEAN_AND_MEAN
    #define NOMINMAX
    #include <windows.h>
    
    std::unique_ptr<wchar_t[]> makeCString(const std::wstring& str)
    {
      std::unique_ptr<wchar_t[]> a(new wchar_t[str.length() + 1]);
      std::copy(begin(str), end(str), &a[0]);
      a[str.length()] = L'\0';
      return a;
    }
    
    int main()
    {
      std::wstring command = L"notepad bla.txt";
    
      STARTUPINFOW si = {0};
      si.cb = sizeof(si); 
      si.dwFlags = STARTF_USESHOWWINDOW | STARTF_FORCEONFEEDBACK; 
      si.wShowWindow = SW_SHOWNORMAL;
    
      PROCESS_INFORMATION pi;
    
      CreateProcessW(nullptr, &makeCString(command)[0], nullptr, nullptr, FALSE, 0U, nullptr, nullptr, &si, &pi);
    
      WaitForSingleObject(pi.hProcess, INFINITE);
    }
    

Anmelden zum Antworten