devcon.exe information to save in file

Hi,

I need to create "temp.dat" with information devcon.exe, but doesn't work.
Source code below, could you help me?


//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>

#define TAM_BUF 150

int main()
{
char cmdLine[TAM_BUF] = { "c:\\instala\\devcon.exe hwids USB* > temp.dat" };
STARTUPINFO si;
PROCESS_INFORMATION pi;
char sz_msg[125] = { 0 };
//

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

// Start the child process.
if (!CreateProcess(NULL, // No module name (use command line)
cmdLine,// Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi)) // Pointer to PROCESS_INFORMATION structure
{
return -1;
}

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

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

return 0;
}

Well the > temp.dat part of a command line is parsed by the shell before the process is run.

But since you don't have a shell, the program itself just sees > temp.dat as two weird strings stuffed in the argv of the created process.

https://docs.microsoft.com/en-us/windows/desktop/procthread/creating-a-child-process-with-redirected-input-and-output
You need to specify that you're redirecting to a file in the STARTUPINFO struct (and remove the "> temp.dat").

Please see:
- https://stackoverflow.com/questions/7018228/how-do-i-redirect-output-to-a-file-with-createprocess
- https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/ns-processthreadsapi-_startupinfoa

i.e. set the HANDLE hStdOutput parameter of STARTUPINFO.
The handle you give it should be the handle returned from CreateFile (misleading name, can be used to open an existing file or create a file, just like fstream).
Last edited on
Topic archived. No new replies allowed.