Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4.  
  5.  
  6. DWORD dwError;
  7.  
  8. #define DEBUG puts("DEBUG");
  9.  
  10. INT WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
  11. {
  12.     SECURITY_ATTRIBUTES Sa;
  13.     Sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  14.     Sa.bInheritHandle = TRUE;
  15.     Sa.lpSecurityDescriptor = NULL;
  16.    
  17.     LPTSTR lpCmd = "C:\\Windows\\System32\\cmd.exe";
  18.    
  19.     HANDLE hRead;
  20.     HANDLE hWrite;
  21.    
  22.     STARTUPINFO Si;
  23.     PROCESS_INFORMATION Pi;
  24.    
  25.     ZeroMemory(&Si, sizeof(STARTUPINFO));
  26.     ZeroMemory(&Pi, sizeof(PROCESS_INFORMATION));
  27.  
  28.     if(!CreatePipe(&hRead, &hWrite, &Sa, 0))
  29.         goto FAILURE;
  30.  
  31.     if(!SetHandleInformation(hRead, HANDLE_FLAG_INHERIT, 0))
  32.         goto FAILURE;
  33.        
  34.     Si.cb = sizeof(STARTUPINFO);
  35.     Si.hStdError = hWrite;
  36.     Si.hStdOutput = hWrite;
  37.     Si.hStdInput = hRead;
  38.    
  39.     Si.dwFlags |= STARTF_USESTDHANDLES;
  40.    
  41.     if(!CreateProcess(lpCmd, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &Si, &Pi))
  42.         goto FAILURE;
  43.        
  44.     TCHAR tBuffer[4096];
  45.     DWORD dwRead = 0;
  46.    
  47.     if(!ReadFile(hRead, tBuffer, 4096, &dwRead, NULL))
  48.         goto FAILURE;
  49.        
  50.     TerminateProcess(Pi.hProcess, ERROR_SUCCESS);
  51.        
  52.     puts(tBuffer);
  53.        
  54.    
  55.            
  56.     return ERROR_SUCCESS;
  57.    
  58. FAILURE:
  59.    
  60.     dwError = GetLastError();
  61.    
  62.     printf("%ld\r\n", dwError);
  63.    
  64.     return dwError;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement