-3

Why isn't this code running my hello world c executable

#include <windows.h>
#include <string>

int main()
{
    // path for hello world compiled program 
    std::string app_path = "C:\\Users\\test\\Desktop\\a.exe";

    BOOL inherit_handles = true;
    PROCESS_INFORMATION pi;
    STARTUPINFO si;

    CreateProcessA(app_path.c_str(),
                   NULL,
                   NULL,
                   NULL,
                   inherit_handles,
                   CREATE_NEW_CONSOLE|CREATE_PROTECTED_PROCESS|DETACHED_PROCESS,
                   0,
                   NULL,
                   &si,
                   &pi);
    return 0;
}

I am clueless to why not output is given, even when I use > out.txt, although I do see some cpu usage spikes in process hacker

I also tried to use calc.exe instead of a.exe but this also didn't help

loaded_dypper
  • 262
  • 3
  • 12
  • You don't initialise si and you ingore the return value and don't do any error checking. The documentation tells you how to check for errors, and countless examples online show you. – David Heffernan Jul 10 '22 at 10:58
  • @DavidHeffernan I am new to all of this trying my best out here, simply copy pasting from the internet does not work for me – loaded_dypper Jul 10 '22 at 11:06
  • I didn't suggest you copy paste anything. I suggested you read and understand. Examples help. – David Heffernan Jul 10 '22 at 11:32

1 Answers1

2
  • STARTUPINFO needs to be initialized:

     STARTUPINFO si;
     ZeroMemory(&si, sizeof(si));
     si.cb = sizeof(si);
    
  • Don't inherit handles unless you need to.

  • Don't use CREATE_PROTECTED_PROCESS.

  • Don't use DETACHED_PROCESS unless you want to hide stdout.

  • Check the return value of CreateProcess!

Anders
  • 97,548
  • 12
  • 110
  • 164