HomeHome
border border Developer Documentation border border
We just use the SystemParametersInfo API to temporary desactivate screensaver, low power, and power off.
That correponds to the SPI_SETSCREENSAVEACTIVE, SPI_SETLOWPOWERACTIVE and SPI_SETPOWEROFFACTIVE uiAction parameter for this function.
We use a named semaphore too, to avoid simultaneous multiple use troubles :
all is disable when the first application using DisableScreenSaver is launched, and restored when the last application using DisableScreenSaver is closed.

Full small source code :
  1. #include <windows.h>
  2.  
  3. #define ERROR_NUMBER_ARGS -1
  4. #define ERROR_CREATING_SEMAPHORE -2
  5.  
  6. #define SEMAPHORE_NAME "DISABLE_SCREENSAVER_SEMAPHORE"
  7.  
  8. BOOL show_last_error()
  9. {
  10. char pcError[255];
  11. DWORD dw=FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,
  12. GetLastError(),GetSystemDefaultLangID(),
  13. pcError,sizeof(pcError)-1,NULL);
  14.  
  15. if (dw==0)
  16. return FALSE;// FormatMessage failed
  17. else
  18. {
  19. MessageBox(NULL,pcError,"Error",MB_OK|MB_ICONERROR);
  20. return TRUE;
  21. }
  22. }
  23.  
  24. void show_usage()
  25. {
  26. MessageBox(NULL,"The use of this software is the following:\r\n
  27. DisableScreenSaver.exe /ApplicationName\r\n
  28. where \"ApplicationName\" is the full path of the application you want to launch",
  29. "Information",MB_OK|MB_ICONINFORMATION);
  30. }
  31.  
  32. // disable screensaver
  33. void disable_screensaver()
  34. {
  35. BOOL b_res;
  36. b_res=SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
  37. if (!b_res)
  38. show_last_error();
  39. b_res=SystemParametersInfo(SPI_SETLOWPOWERACTIVE,0,NULL,0);
  40. if (!b_res)
  41. show_last_error();
  42. b_res=SystemParametersInfo(SPI_SETPOWEROFFACTIVE,0,NULL,0);
  43. if (!b_res)
  44. show_last_error();
  45. }
  46.  
  47. void launch_and_wait_process(char* pc_fullpath)
  48. {
  49. STARTUPINFO si;
  50. PROCESS_INFORMATION pi;
  51.  
  52. ZeroMemory( &si, sizeof(si) );
  53. si.cb = sizeof(si);
  54. ZeroMemory( &pi, sizeof(pi) );
  55.  
  56. // create directory path
  57. char pc_application_directory[MAX_PATH];
  58. char* ptr_application_directory;
  59.  
  60. int len=(int)strlen(pc_fullpath);
  61. if (len>0)
  62. {
  63. // remove / if any
  64. if (pc_fullpath[0]=='/')
  65. {
  66. pc_fullpath=&pc_fullpath[1];
  67. len--;
  68. }
  69. }
  70. if (len>0)
  71. {
  72. // remove " at the begining of pc_fullpath if any
  73. if (pc_fullpath[0]=='"')
  74. {
  75. pc_fullpath=&pc_fullpath[1];
  76. len--;
  77. }
  78. }
  79. if (len>0)
  80. {
  81. // remove " at the end of pc_fullpath if any
  82. if (pc_fullpath[len-1]=='"')
  83. pc_fullpath[len-1]=0;
  84. }
  85.  
  86. strncpy(pc_application_directory,pc_fullpath,MAX_PATH);
  87. char* ptr=strrchr(pc_application_directory,'\\');
  88. if (ptr)
  89. {
  90. *ptr=0;
  91. ptr_application_directory=pc_application_directory;
  92. }
  93. else
  94. ptr_application_directory=NULL;
  95.  
  96. // Start the child process.
  97. if( !CreateProcess( NULL, // No module name (use command line).
  98. pc_fullpath, // Command line.
  99. NULL, // Process handle not inheritable.
  100. NULL, // Thread handle not inheritable.
  101. FALSE, // Set handle inheritance to FALSE.
  102. 0, // No creation flags.
  103. NULL, // Use parent's environment block.
  104. ptr_application_directory,// Use parent's starting directory.
  105. &si, // Pointer to STARTUPINFO structure.
  106. &pi ) // Pointer to PROCESS_INFORMATION structure.
  107. )
  108. {
  109. show_last_error();
  110. // Close process and thread handles.
  111. CloseHandle( pi.hProcess );
  112. CloseHandle( pi.hThread );
  113. return;
  114. }
  115.  
  116. // Wait until child process exits.
  117. // WaitForSingleObject( pi.hProcess, INFINITE );
  118.  
  119. // Wait until child process exits and assume
  120. // screen saver is disable every minute (min screen saver enabling)
  121. DWORD dwRes=WAIT_TIMEOUT;
  122. while (dwRes==WAIT_TIMEOUT)
  123. {
  124. dwRes=WaitForSingleObject( pi.hProcess, 59000 );
  125. disable_screensaver();
  126. }
  127.  
  128. // Close process and thread handles.
  129. CloseHandle( pi.hProcess );
  130. CloseHandle( pi.hThread );
  131. }
  132.  
  133. int __stdcall WinMain(HINSTANCE hInstance,
  134. HINSTANCE hPrevInstance,
  135. LPSTR lpCmdLine,
  136. int nCmdShow)
  137. {
  138. UNREFERENCED_PARAMETER(hInstance);
  139. UNREFERENCED_PARAMETER(hPrevInstance);
  140. UNREFERENCED_PARAMETER(nCmdShow);
  141. BOOL b_res;
  142. HANDLE hnd_semaphore;
  143. DWORD dw_res_wait;
  144.  
  145. // check if there's arguments
  146. if (strlen(lpCmdLine)==0)
  147. {
  148. show_usage();
  149. return ERROR_NUMBER_ARGS;
  150. }
  151. //try to open semaphore
  152. hnd_semaphore=OpenSemaphore(EVENT_ALL_ACCESS,FALSE,SEMAPHORE_NAME);
  153. // if not named semaphore is not existing, create semaphore and disable screensaver
  154. if (!hnd_semaphore)
  155. {
  156. // create semaphore
  157. hnd_semaphore=CreateSemaphore(
  158. NULL,// security attr
  159. 0,// initial count
  160. 0x7FFF,// max count
  161. SEMAPHORE_NAME
  162. );
  163. if (!hnd_semaphore)
  164. {
  165. show_last_error();
  166. return ERROR_CREATING_SEMAPHORE;
  167. }
  168. // disable screensaver
  169. disable_screensaver();
  170. }
  171. else
  172. {
  173. b_res=ReleaseSemaphore(hnd_semaphore,1,NULL);
  174. if (!b_res)
  175. show_last_error();
  176. }
  177. // launch process and wait for it's end
  178. launch_and_wait_process(lpCmdLine);
  179.  
  180. // WaitForSingleObject decrease the count of semaphores
  181. // WaitForSingleObject time out --> there's no more other instance of disable_screensaver --> enable screensaver
  182. dw_res_wait=WaitForSingleObject(hnd_semaphore,0);
  183. if (dw_res_wait==WAIT_TIMEOUT)
  184. {
  185. // enable screensaver
  186. b_res=SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
  187. if (!b_res)
  188. show_last_error();
  189. b_res=SystemParametersInfo(SPI_SETLOWPOWERACTIVE,1,NULL,0);
  190. if (!b_res)
  191. show_last_error();
  192. b_res=SystemParametersInfo(SPI_SETPOWEROFFACTIVE,1,NULL,0);
  193. if (!b_res)
  194. show_last_error();
  195. }
  196.  
  197. // close semaphore handle
  198. CloseHandle(hnd_semaphore);
  199.  
  200. return ERROR_SUCCESS;
  201. }
Top
border border border border