SHARE
TWEET

blocksleep.cpp

a guest Jan 8th, 2016 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Author: Vyacheslav, 2016.
  2. // Use this code whatever you like.
  3. // Mention me or send me a PM to make me happy if you like it. :)
  4.  
  5. #include <stdio.h>
  6. #include <tchar.h>
  7. #define WIN32_LEAN_AND_MEAN
  8. #define NOMINMAX
  9. #include <windows.h>
  10. #include <Shlwapi.h>
  11.  
  12. #ifdef UNICODE
  13. #define tprintf wprintf
  14. #else
  15. #define tprintf printf
  16. #endif
  17.  
  18. int _tmain(int argc, LPTSTR argv[]) {
  19.     DWORD ret = (DWORD)-2;
  20.     if (argc <= 1) {
  21.         printf(
  22.             "Block PC sleep while child process is executing.\n"
  23.             "Usage: blocksleep <exe_filename> [arguments]\n"
  24.             "\n"
  25.         );
  26.         goto finish;
  27.     }
  28.  
  29.     {
  30.         EXECUTION_STATE old_state = SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
  31.         if (!old_state) {
  32.             printf("ERROR: Can't set thread execution state via SetThreadExecutionState WinAPI finction\n"
  33.                 "See https://msdn.microsoft.com/ru-ru/library/windows/desktop/aa373208(v=vs.85).aspx for more info.\n");
  34.             goto finish;
  35.         }
  36.  
  37.         STARTUPINFO si = {};
  38.         si.cb = sizeof(si);
  39.         si.dwFlags =  STARTF_FORCEOFFFEEDBACK;
  40.         PROCESS_INFORMATION pi = {};
  41.         LPTSTR cmd = PathGetArgs(PathGetArgs(GetCommandLine()));
  42.         if (!CreateProcess(argv[1], cmd, NULL, NULL, true, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &si, &pi)) {
  43.             tprintf(_T(
  44.                 "ERROR: Can't start process \"%s\" with CreateProcess function\n"
  45.                 "Command line : %s\n"
  46.                 "See why this could happen at https://msdn.microsoft.com/ru-ru/library/windows/desktop/ms682425(v=vs.85).aspx\n"
  47.             ), argv[1], (*cmd) ? cmd : _T("<empty>"));
  48.             goto finish_reset_state;
  49.         }
  50.         CloseHandle(pi.hThread);
  51.         ret = (DWORD)-3;
  52.  
  53.         DWORD result = WaitForSingleObject(pi.hProcess, INFINITE);
  54.         if (result != WAIT_OBJECT_0) {
  55.             printf("ERROR: WaitForSingleObject function failed with code 0x%08x\n", result);
  56.             goto finish_close_process;
  57.         }
  58.         if (!GetExitCodeProcess(pi.hProcess, &result)) {
  59.             printf("ERROR: GetExitCodeProcess function failed.\n");
  60.             goto finish_close_process;
  61.         }
  62.         ret = result;
  63.     finish_close_process:
  64.         CloseHandle(pi.hProcess);
  65.     finish_reset_state:
  66.         SetThreadExecutionState(ES_CONTINUOUS | old_state);
  67.     }
  68. finish:
  69.     ExitProcess(ret);
  70.     return 0;
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top