/admin/win/nsi/nsis_processes/src/processes.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 411 lines · 223 code · 114 blank · 74 comment · 51 complexity · c5ed4fecc7872a369517154b5e9c1d0d MD5 · raw file

  1. #include "stdafx.h"
  2. #include "processes.h"
  3. #include "string.h"
  4. //-------------------------------------------------------------------------------------------
  5. // global variables
  6. lpfEnumProcesses EnumProcesses;
  7. lpfEnumProcessModules EnumProcessModules;
  8. lpfGetModuleBaseName GetModuleBaseName;
  9. lpfEnumDeviceDrivers EnumDeviceDrivers;
  10. lpfGetDeviceDriverBaseName GetDeviceDriverBaseName;
  11. HINSTANCE g_hInstance;
  12. HWND g_hwndParent;
  13. HINSTANCE g_hInstLib;
  14. //-------------------------------------------------------------------------------------------
  15. // main DLL entry
  16. BOOL WINAPI _DllMainCRTStartup( HANDLE hInst,
  17. ULONG ul_reason_for_call,
  18. LPVOID lpReserved )
  19. {
  20. g_hInstance = (struct HINSTANCE__ *)hInst;
  21. return TRUE;
  22. }
  23. //-------------------------------------------------------------------------------------------
  24. // loads the psapi routines
  25. bool LoadPSAPIRoutines( void )
  26. {
  27. if( NULL == (g_hInstLib = LoadLibraryA( "PSAPI.DLL" )) )
  28. return false;
  29. EnumProcesses = (lpfEnumProcesses) GetProcAddress( g_hInstLib, "EnumProcesses" );
  30. EnumProcessModules = (lpfEnumProcessModules) GetProcAddress( g_hInstLib, "EnumProcessModules" );
  31. GetModuleBaseName = (lpfGetModuleBaseName) GetProcAddress( g_hInstLib, "GetModuleBaseNameA" );
  32. EnumDeviceDrivers = (lpfEnumDeviceDrivers) GetProcAddress( g_hInstLib, "EnumDeviceDrivers" );
  33. GetDeviceDriverBaseName = (lpfGetDeviceDriverBaseName) GetProcAddress( g_hInstLib, "GetDeviceDriverBaseNameA" );
  34. if( ( NULL == EnumProcesses ) ||
  35. ( NULL == EnumProcessModules ) ||
  36. ( NULL == EnumDeviceDrivers ) ||
  37. ( NULL == GetModuleBaseName ) ||
  38. ( NULL == GetDeviceDriverBaseName ) )
  39. {
  40. FreeLibrary( g_hInstLib );
  41. return false;
  42. }
  43. return true;
  44. }
  45. //-------------------------------------------------------------------------------------------
  46. // free the psapi routines
  47. bool FreePSAPIRoutines( void )
  48. {
  49. EnumProcesses = NULL;
  50. EnumProcessModules = NULL;
  51. GetModuleBaseName = NULL;
  52. EnumDeviceDrivers = NULL;
  53. if( FALSE == FreeLibrary( g_hInstLib ) )
  54. return false;
  55. return true;
  56. }
  57. //-------------------------------------------------------------------------------------------
  58. // find a process by name
  59. // return value: true - process was found
  60. // false - process not found
  61. bool FindProc( char *szProcess )
  62. {
  63. char szProcessName[ 1024 ];
  64. char szCurrentProcessName[ 1024 ];
  65. DWORD dPID[ 1024 ];
  66. DWORD dPIDSize( 1024 );
  67. DWORD dSize( 1024 );
  68. HANDLE hProcess;
  69. HMODULE phModule[ 1024 ];
  70. //
  71. // make the name lower case
  72. //
  73. memset( szProcessName, 0, 1024*sizeof(char) );
  74. sprintf( szProcessName, "%s", szProcess );
  75. strlwr( szProcessName );
  76. //
  77. // load PSAPI routines
  78. //
  79. if( false == LoadPSAPIRoutines() )
  80. return false;
  81. //
  82. // enumerate processes names
  83. //
  84. if( FALSE == EnumProcesses( dPID, dSize, &dPIDSize ) )
  85. {
  86. FreePSAPIRoutines();
  87. return false;
  88. }
  89. //
  90. // walk trough and compare see if the process is running
  91. //
  92. for( int k( dPIDSize / sizeof( DWORD ) ); k >= 0; k-- )
  93. {
  94. memset( szCurrentProcessName, 0, 1024*sizeof(char) );
  95. if( NULL != ( hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, dPID[ k ] ) ) )
  96. {
  97. if( TRUE == EnumProcessModules( hProcess, phModule, sizeof(HMODULE)*1024, &dPIDSize ) )
  98. if( GetModuleBaseName( hProcess, phModule[ 0 ], szCurrentProcessName, 1024 ) > 0 )
  99. {
  100. strlwr( szCurrentProcessName );
  101. if( NULL != strstr( szCurrentProcessName, szProcessName ) )
  102. {
  103. FreePSAPIRoutines();
  104. CloseHandle( hProcess );
  105. return true;
  106. }
  107. }
  108. CloseHandle( hProcess );
  109. }
  110. }
  111. //
  112. // free PSAPI routines
  113. //
  114. FreePSAPIRoutines();
  115. return false;
  116. }
  117. //-------------------------------------------------------------------------------------------
  118. // kills a process by name
  119. // return value: true - process was found
  120. // false - process not found
  121. bool KillProc( char *szProcess )
  122. {
  123. char szProcessName[ 1024 ];
  124. char szCurrentProcessName[ 1024 ];
  125. DWORD dPID[ 1024 ];
  126. DWORD dPIDSize( 1024 );
  127. DWORD dSize( 1024 );
  128. HANDLE hProcess;
  129. HMODULE phModule[ 1024 ];
  130. //
  131. // make the name lower case
  132. //
  133. memset( szProcessName, 0, 1024*sizeof(char) );
  134. sprintf( szProcessName, "%s", szProcess );
  135. strlwr( szProcessName );
  136. //
  137. // load PSAPI routines
  138. //
  139. if( false == LoadPSAPIRoutines() )
  140. return false;
  141. //
  142. // enumerate processes names
  143. //
  144. if( FALSE == EnumProcesses( dPID, dSize, &dPIDSize ) )
  145. {
  146. FreePSAPIRoutines();
  147. return false;
  148. }
  149. //
  150. // walk trough and compare see if the process is running
  151. //
  152. for( int k( dPIDSize / sizeof( DWORD ) ); k >= 0; k-- )
  153. {
  154. memset( szCurrentProcessName, 0, 1024*sizeof(char) );
  155. if( NULL != ( hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, dPID[ k ] ) ) )
  156. {
  157. if( TRUE == EnumProcessModules( hProcess, phModule, sizeof(HMODULE)*1024, &dPIDSize ) )
  158. if( GetModuleBaseName( hProcess, phModule[ 0 ], szCurrentProcessName, 1024 ) > 0 )
  159. {
  160. strlwr( szCurrentProcessName );
  161. if( NULL != strstr( szCurrentProcessName, szProcessName ) )
  162. {
  163. FreePSAPIRoutines();
  164. //
  165. // kill process
  166. //
  167. if( false == TerminateProcess( hProcess, 0 ) )
  168. {
  169. CloseHandle( hProcess );
  170. return true;
  171. }
  172. //
  173. // refresh systray
  174. //
  175. UpdateWindow( FindWindow( NULL, "Shell_TrayWnd" ) );
  176. //
  177. // refresh desktop window
  178. //
  179. UpdateWindow( GetDesktopWindow() );
  180. CloseHandle( hProcess );
  181. return true;
  182. }
  183. }
  184. CloseHandle( hProcess );
  185. }
  186. }
  187. //
  188. // free PSAPI routines
  189. //
  190. FreePSAPIRoutines();
  191. return false;
  192. }
  193. //-------------------------------------------------------------------------------------------
  194. bool FindDev( char *szDriverName )
  195. {
  196. char szDeviceName[ 1024 ];
  197. char szCurrentDeviceName[ 1024 ];
  198. LPVOID lpDevices[ 1024 ];
  199. DWORD dDevicesSize( 1024 );
  200. DWORD dSize( 1024 );
  201. TCHAR tszCurrentDeviceName[ 1024 ];
  202. DWORD dNameSize( 1024 );
  203. //
  204. // make the name lower case
  205. //
  206. memset( szDeviceName, 0, 1024*sizeof(char) );
  207. sprintf( szDeviceName, "%s", strlwr( szDriverName ) );
  208. //
  209. // load PSAPI routines
  210. //
  211. if( false == LoadPSAPIRoutines() )
  212. return false;
  213. //
  214. // enumerate devices
  215. //
  216. if( FALSE == EnumDeviceDrivers( lpDevices, dSize, &dDevicesSize ) )
  217. {
  218. FreePSAPIRoutines();
  219. return false;
  220. }
  221. //
  222. // walk trough and compare see if the device driver exists
  223. //
  224. for( int k( dDevicesSize / sizeof( LPVOID ) ); k >= 0; k-- )
  225. {
  226. memset( szCurrentDeviceName, 0, 1024*sizeof(char) );
  227. memset( tszCurrentDeviceName, 0, 1024*sizeof(TCHAR) );
  228. if( 0 != GetDeviceDriverBaseName( lpDevices[ k ], tszCurrentDeviceName, dNameSize ) )
  229. {
  230. sprintf( szCurrentDeviceName, "%S", tszCurrentDeviceName );
  231. if( 0 == strcmp( strlwr( szCurrentDeviceName ), szDeviceName ) )
  232. {
  233. FreePSAPIRoutines();
  234. return true;
  235. }
  236. }
  237. }
  238. //
  239. // free PSAPI routines
  240. //
  241. FreePSAPIRoutines();
  242. return false;
  243. }
  244. //-------------------------------------------------------------------------------------------
  245. extern "C" __declspec(dllexport) void FindProcess( HWND hwndParent,
  246. int string_size,
  247. char *variables,
  248. stack_t **stacktop )
  249. {
  250. char szParameter[ 1024 ];
  251. g_hwndParent = hwndParent;
  252. EXDLL_INIT();
  253. {
  254. popstring( szParameter );
  255. if( true == FindProc( szParameter ) )
  256. wsprintf( szParameter, "1" );
  257. else
  258. wsprintf( szParameter, "0" );
  259. setuservariable( INST_R0, szParameter );
  260. }
  261. }
  262. //-------------------------------------------------------------------------------------------
  263. extern "C" __declspec(dllexport) void KillProcess( HWND hwndParent,
  264. int string_size,
  265. char *variables,
  266. stack_t **stacktop )
  267. {
  268. char szParameter[ 1024 ];
  269. g_hwndParent = hwndParent;
  270. EXDLL_INIT();
  271. {
  272. popstring( szParameter );
  273. if( true == KillProc( szParameter ) )
  274. wsprintf( szParameter, "1" );
  275. else
  276. wsprintf( szParameter, "0" );
  277. setuservariable( INST_R0, szParameter );
  278. }
  279. }
  280. //-------------------------------------------------------------------------------------------
  281. extern "C" __declspec(dllexport) void FindDevice( HWND hwndParent,
  282. int string_size,
  283. char *variables,
  284. stack_t **stacktop )
  285. {
  286. char szParameter[ 1024 ];
  287. g_hwndParent = hwndParent;
  288. EXDLL_INIT();
  289. {
  290. popstring( szParameter );
  291. if( true == FindDev( szParameter ) )
  292. wsprintf( szParameter, "1" );
  293. else
  294. wsprintf( szParameter, "0" );
  295. setuservariable( INST_R0, szParameter );
  296. }
  297. }