Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author: Vyacheslav, 2016.
- // Use this code whatever you like.
- // Mention me or send me a PM to make me happy if you like it. :)
- #include <stdio.h>
- #include <tchar.h>
- #define WIN32_LEAN_AND_MEAN
- #define NOMINMAX
- #include <windows.h>
- #include <Shlwapi.h>
- #ifdef UNICODE
- #define tprintf wprintf
- #else
- #define tprintf printf
- #endif
- int _tmain(int argc, LPTSTR argv[]) {
- DWORD ret = (DWORD)-2;
- if (argc <= 1) {
- printf(
- "Block PC sleep while child process is executing.\n"
- "Usage: blocksleep <exe_filename> [arguments]\n"
- "\n"
- );
- goto finish;
- }
- {
- EXECUTION_STATE old_state = SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
- if (!old_state) {
- printf("ERROR: Can't set thread execution state via SetThreadExecutionState WinAPI finction\n"
- "See https://msdn.microsoft.com/ru-ru/library/windows/desktop/aa373208(v=vs.85).aspx for more info.\n");
- goto finish;
- }
- STARTUPINFO si = {};
- si.cb = sizeof(si);
- si.dwFlags = STARTF_FORCEOFFFEEDBACK;
- PROCESS_INFORMATION pi = {};
- LPTSTR cmd = PathGetArgs(PathGetArgs(GetCommandLine()));
- if (!CreateProcess(argv[1], cmd, NULL, NULL, true, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &si, &pi)) {
- tprintf(_T(
- "ERROR: Can't start process \"%s\" with CreateProcess function\n"
- "Command line : %s\n"
- "See why this could happen at https://msdn.microsoft.com/ru-ru/library/windows/desktop/ms682425(v=vs.85).aspx\n"
- ), argv[1], (*cmd) ? cmd : _T("<empty>"));
- goto finish_reset_state;
- }
- CloseHandle(pi.hThread);
- ret = (DWORD)-3;
- DWORD result = WaitForSingleObject(pi.hProcess, INFINITE);
- if (result != WAIT_OBJECT_0) {
- printf("ERROR: WaitForSingleObject function failed with code 0x%08x\n", result);
- goto finish_close_process;
- }
- if (!GetExitCodeProcess(pi.hProcess, &result)) {
- printf("ERROR: GetExitCodeProcess function failed.\n");
- goto finish_close_process;
- }
- ret = result;
- finish_close_process:
- CloseHandle(pi.hProcess);
- finish_reset_state:
- SetThreadExecutionState(ES_CONTINUOUS | old_state);
- }
- finish:
- ExitProcess(ret);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement