C++ popen command without console

后端 未结 4 1180
借酒劲吻你
借酒劲吻你 2020-12-20 17:20

when I use popen to get the output of a command, say dir, it will prompt out a console.

however, can I get the output of a command without the appearance of the cons

4条回答
  •  天命终不由人
    2020-12-20 17:41

    Assuming Windows (since this is the only platform where this behavior is endemic):

    CreatePipe() to create the pipes necessary to communicate, and CreateProcess to create the child process.

    HANDLE StdInHandles[2]; 
    HANDLE StdOutHandles[2]; 
    HANDLE StdErrHandles[2]; 
    
    CreatePipe(&StdInHandles[0], &StdInHandles[1], NULL, 4096); 
    CreatePipe(&StdOutHandles[0], &StdOutHandles[1], NULL, 4096); 
    CreatePipe(&StdErrHandles[0], &StdErrHandles[1], NULL, 4096); 
    
    
    STARTUPINFO si;   memset(&si, 0, sizeof(si));  /* zero out */ 
    
    si.dwFlags =  STARTF_USESTDHANDLES; 
    si.hStdInput = StdInHandles[0];  /* read handle */ 
    si.hStdOutput = StdOutHandles[1];  /* write handle */
    si.hStdError = StdErrHandles[1];  /* write handle */
    
    /* fix other stuff in si */
    
    PROCESS_INFORMATION pi; 
    /* fix stuff in pi */
    
    
    CreateProcess(AppName, commandline, SECURITY_ATTRIBUTES, SECURITY_ATTRIBUTES, FALSE, CREATE_NO_WINDOW |DETACHED_PROCESS, lpEnvironment, lpCurrentDirectory, &si, &pi); 
    

    This should more than get you on your way to doing what you wish to accomplish.

提交回复
热议问题