1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <windows.h>
int cmd(char *cmd, char *output, DWORD maxbuffer)
{
HANDLE readHandle;
HANDLE writeHandle;
HANDLE stdHandle;
DWORD bytesRead;
DWORD retCode;
SECURITY_ATTRIBUTES sa;
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&sa,sizeof(SECURITY_ATTRIBUTES));
ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));
ZeroMemory(&si,sizeof(STARTUPINFO));
sa.bInheritHandle=true;
sa.lpSecurityDescriptor=NULL;
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
si.cb=sizeof(STARTUPINFO);
si.dwFlags=STARTF_USESHOWWINDOW;
si.wShowWindow=SW_HIDE;
if (!CreatePipe(&readHandle,&writeHandle,&sa,NULL))
{
OutputDebugString("cmd: CreatePipe failed!\n");
return 0;
}
stdHandle=GetStdHandle(STD_OUTPUT_HANDLE);
if (!SetStdHandle(STD_OUTPUT_HANDLE,writeHandle))
{
OutputDebugString("cmd: SetStdHandle(writeHandle) failed!\n");
return 0;
}
if (!CreateProcess(NULL,cmd,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))
{
OutputDebugString("cmd: CreateProcess failed!\n");
return 0;
}
GetExitCodeProcess(pi.hProcess,&retCode);
while (retCode==STILL_ACTIVE)
{
GetExitCodeProcess(pi.hProcess,&retCode);
}
if (!ReadFile(readHandle,output,maxbuffer,&bytesRead,NULL))
{
OutputDebugString("cmd: ReadFile failed!\n");
return 0;
}
output[bytesRead]='\0';
if (!SetStdHandle(STD_OUTPUT_HANDLE,stdHandle))
{
OutputDebugString("cmd: SetStdHandle(stdHandle) failed!\n");
return 0;
}
if (!CloseHandle(readHandle))
{
OutputDebugString("cmd: CloseHandle(readHandle) failed!\n");
}
if (!CloseHandle(writeHandle))
{
OutputDebugString("cmd: CloseHandle(writeHandle) failed!\n");
}
return 1;
}
int WINAPI WinMain(HINSTANCE hInstanceHandle,
HINSTANCE hPreviousInstanceHandle,
LPSTR lpszCommandLine,
INT nShowState) {
char PingOutput[2048];
cmd("ping", PingOutput, 2048);
MessageBox(NULL, PingOutput, "Ping-Output", MB_OK);
return EXIT_SUCCESS;
}
|