I am capturing the output of a command run through CreateProcess
after redirecting the output to a file and then reading the file contents. I store the string read from the file into a char array inside the function and print it. All good so far. Then I return the char array to the main function and attempt to print it from there. It seems to print inconsistent garbage. I am not sure why. Throwing the executable in a debugger, I see that the printf inside main is called with correct pointer address. I am at loss understanding why it behaves the way it does. Need some pointers. Here is my code:
#include <Windows.h>
#include <stdio.h>
char * run_cmd(char * cmd ) {
char output[2000];
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
HANDLE hFile;
hFile = CreateFileA("out.log",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ,
&sa, // this seems important!
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
PROCESS_INFORMATION pi;
STARTUPINFO si;
BOOL ret = FALSE;
DWORD flags = CREATE_NO_WINDOW;
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags |= STARTF_USESTDHANDLES;
si.hStdInput = NULL;
si.hStdError = NULL;
si.hStdOutput = hFile;
ret = CreateProcessA(NULL, cmd, NULL, NULL, TRUE, flags, NULL, NULL, &si, &pi);
Sleep(2000);
CloseHandle(hFile);
DWORD lpNumberOfBytesRead; // return value
hFile = CreateFileA("out.log",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
DWORD dwBytesToRead = GetFileSize(hFile, NULL);
ReadFile(hFile, (void *)output, dwBytesToRead, &lpNumberOfBytesRead, NULL);
output[lpNumberOfBytesRead] = '\0';
CloseHandle(hFile);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
printf("%s\n", output);
printf("------------------------------------------------------------------------");
return output;
}
int main(void) {
printf("%s\n", run_cmd("ipconfig"));
}
``