PageRenderTime 285ms CodeModel.GetById 121ms app.highlight 76ms RepoModel.GetById 85ms app.codeStats 0ms

/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
  6
  7
  8
  9
 10//-------------------------------------------------------------------------------------------
 11// global variables
 12lpfEnumProcesses			EnumProcesses;
 13lpfEnumProcessModules		EnumProcessModules;
 14lpfGetModuleBaseName		GetModuleBaseName;
 15lpfEnumDeviceDrivers		EnumDeviceDrivers;
 16lpfGetDeviceDriverBaseName	GetDeviceDriverBaseName;
 17
 18HINSTANCE					g_hInstance;
 19HWND						g_hwndParent;
 20HINSTANCE					g_hInstLib;
 21
 22
 23
 24
 25
 26//-------------------------------------------------------------------------------------------
 27// main DLL entry
 28BOOL WINAPI		_DllMainCRTStartup( HANDLE	hInst, 
 29									ULONG	ul_reason_for_call,
 30									LPVOID	lpReserved )
 31{
 32    g_hInstance		= (struct HINSTANCE__ *)hInst;
 33
 34    return TRUE;
 35}
 36
 37
 38
 39
 40
 41//-------------------------------------------------------------------------------------------
 42// loads the psapi routines
 43bool	LoadPSAPIRoutines( void )
 44{
 45	if( NULL == (g_hInstLib = LoadLibraryA( "PSAPI.DLL" )) )
 46		return false;
 47
 48	EnumProcesses			= (lpfEnumProcesses)			GetProcAddress( g_hInstLib, "EnumProcesses" );
 49	EnumProcessModules		= (lpfEnumProcessModules)		GetProcAddress( g_hInstLib, "EnumProcessModules" );
 50	GetModuleBaseName		= (lpfGetModuleBaseName)		GetProcAddress( g_hInstLib, "GetModuleBaseNameA" );
 51	EnumDeviceDrivers		= (lpfEnumDeviceDrivers)		GetProcAddress( g_hInstLib, "EnumDeviceDrivers" );
 52	GetDeviceDriverBaseName	= (lpfGetDeviceDriverBaseName)	GetProcAddress( g_hInstLib, "GetDeviceDriverBaseNameA" );
 53
 54	if( ( NULL == EnumProcesses ) ||
 55		( NULL == EnumProcessModules ) ||
 56		( NULL == EnumDeviceDrivers ) ||
 57		( NULL == GetModuleBaseName ) ||
 58		( NULL == GetDeviceDriverBaseName ) )
 59	{
 60		FreeLibrary( g_hInstLib );
 61
 62		return false;
 63	}
 64
 65	return true;
 66}
 67
 68
 69
 70
 71
 72//-------------------------------------------------------------------------------------------
 73// free the psapi routines
 74bool	FreePSAPIRoutines( void )
 75{
 76	EnumProcesses		= NULL;
 77	EnumProcessModules	= NULL;
 78	GetModuleBaseName	= NULL;
 79	EnumDeviceDrivers	= NULL;
 80
 81	if( FALSE == FreeLibrary( g_hInstLib ) )
 82		return false;
 83
 84	return true;
 85}
 86
 87
 88
 89
 90
 91//-------------------------------------------------------------------------------------------
 92// find a process by name
 93// return value:	true	- process was found
 94//					false	- process not found
 95bool	FindProc( char *szProcess )
 96{
 97	char		szProcessName[ 1024 ];
 98	char		szCurrentProcessName[ 1024 ];
 99	DWORD		dPID[ 1024 ];
100	DWORD		dPIDSize( 1024 );
101	DWORD		dSize( 1024 );
102	HANDLE		hProcess;
103	HMODULE		phModule[ 1024 ];
104
105  
106	//
107	// make the name lower case
108	//
109	memset( szProcessName, 0, 1024*sizeof(char) );
110	sprintf( szProcessName, "%s", szProcess );
111	strlwr( szProcessName );
112
113	//
114    // load PSAPI routines
115	//
116	if( false == LoadPSAPIRoutines() )
117		return false;
118
119	//
120	// enumerate processes names
121	//
122    if( FALSE == EnumProcesses( dPID, dSize, &dPIDSize ) )
123	{
124		FreePSAPIRoutines();
125
126		return false;
127	}
128
129	//
130	// walk trough and compare see if the process is running
131	//
132	for( int k( dPIDSize / sizeof( DWORD ) ); k >= 0; k-- )
133	{
134		memset( szCurrentProcessName, 0, 1024*sizeof(char) );
135
136		if( NULL != ( hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, dPID[ k ] ) ) )
137		{
138			if( TRUE == EnumProcessModules( hProcess, phModule, sizeof(HMODULE)*1024, &dPIDSize ) )
139				if( GetModuleBaseName( hProcess, phModule[ 0 ], szCurrentProcessName, 1024 ) > 0 )
140				{
141					strlwr( szCurrentProcessName );
142
143					if( NULL != strstr( szCurrentProcessName, szProcessName ) )
144					{
145						FreePSAPIRoutines();
146						CloseHandle( hProcess );
147
148						return true;
149					}
150				}
151
152			CloseHandle( hProcess );
153		}
154	}
155	
156	//
157	// free PSAPI routines
158	//
159	FreePSAPIRoutines();
160
161	return false;
162}
163
164
165
166
167
168//-------------------------------------------------------------------------------------------
169// kills a process by name
170// return value:	true	- process was found
171//					false	- process not found
172bool	KillProc( char *szProcess )
173{
174	char		szProcessName[ 1024 ];
175	char		szCurrentProcessName[ 1024 ];
176	DWORD		dPID[ 1024 ];
177	DWORD		dPIDSize( 1024 );
178	DWORD		dSize( 1024 );
179	HANDLE		hProcess;
180	HMODULE		phModule[ 1024 ];
181
182  
183	//
184	// make the name lower case
185	//
186	memset( szProcessName, 0, 1024*sizeof(char) );
187	sprintf( szProcessName, "%s", szProcess );
188	strlwr( szProcessName );
189
190	//
191    // load PSAPI routines
192	//
193	if( false == LoadPSAPIRoutines() )
194		return false;
195
196	//
197	// enumerate processes names
198	//
199    if( FALSE == EnumProcesses( dPID, dSize, &dPIDSize ) )
200	{
201		FreePSAPIRoutines();
202
203		return false;
204	}
205
206	//
207	// walk trough and compare see if the process is running
208	//
209	for( int k( dPIDSize / sizeof( DWORD ) ); k >= 0; k-- )
210	{
211		memset( szCurrentProcessName, 0, 1024*sizeof(char) );
212
213		if( NULL != ( hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, dPID[ k ] ) ) )
214		{
215			if( TRUE == EnumProcessModules( hProcess, phModule, sizeof(HMODULE)*1024, &dPIDSize ) )
216				if( GetModuleBaseName( hProcess, phModule[ 0 ], szCurrentProcessName, 1024 ) > 0 )
217				{
218					strlwr( szCurrentProcessName );
219
220					if( NULL != strstr( szCurrentProcessName, szProcessName ) )
221					{
222						FreePSAPIRoutines();
223
224						//
225						// kill process
226						//
227						if( false == TerminateProcess( hProcess, 0 ) )
228						{
229							CloseHandle( hProcess );
230
231							return true;
232						}
233
234						//
235						// refresh systray
236						//
237						UpdateWindow( FindWindow( NULL, "Shell_TrayWnd" ) );
238
239						//
240						// refresh desktop window
241						//
242						UpdateWindow( GetDesktopWindow() );
243
244						CloseHandle( hProcess );
245
246						return true;
247					}
248				}
249
250			CloseHandle( hProcess );
251		}
252	}
253	
254	//
255	// free PSAPI routines
256	//
257	FreePSAPIRoutines();
258
259	return false;
260}
261
262
263
264
265
266//-------------------------------------------------------------------------------------------
267bool	FindDev( char *szDriverName )
268{
269	char		szDeviceName[ 1024 ];
270	char		szCurrentDeviceName[ 1024 ];
271	LPVOID		lpDevices[ 1024 ];
272	DWORD		dDevicesSize( 1024 );
273	DWORD		dSize( 1024 );
274	TCHAR		tszCurrentDeviceName[ 1024 ];
275	DWORD		dNameSize( 1024 );
276
277  
278	//
279	// make the name lower case
280	//
281	memset( szDeviceName, 0, 1024*sizeof(char) );
282	sprintf( szDeviceName, "%s", strlwr( szDriverName ) );
283
284	//
285    // load PSAPI routines
286	//
287	if( false == LoadPSAPIRoutines() )
288		return false;
289
290	//
291	// enumerate devices
292	//
293    if( FALSE == EnumDeviceDrivers( lpDevices, dSize, &dDevicesSize ) )
294	{
295		FreePSAPIRoutines();
296
297		return false;
298	}
299
300	//
301	// walk trough and compare see if the device driver exists
302	//
303	for( int k( dDevicesSize / sizeof( LPVOID ) ); k >= 0; k-- )
304	{
305		memset( szCurrentDeviceName, 0, 1024*sizeof(char) );
306		memset( tszCurrentDeviceName, 0, 1024*sizeof(TCHAR) );
307
308		if( 0 != GetDeviceDriverBaseName( lpDevices[ k ], tszCurrentDeviceName, dNameSize ) )
309		{
310			sprintf( szCurrentDeviceName, "%S", tszCurrentDeviceName );
311
312			if( 0 == strcmp( strlwr( szCurrentDeviceName ), szDeviceName ) )
313			{
314				FreePSAPIRoutines();
315
316				return true;
317			}
318		}
319	}
320	
321	//
322	// free PSAPI routines
323	//
324	FreePSAPIRoutines();
325
326	return false;
327}
328
329
330
331
332
333//-------------------------------------------------------------------------------------------
334extern "C" __declspec(dllexport) void	FindProcess( HWND		hwndParent, 
335													 int		string_size,
336													 char		*variables, 
337													 stack_t	**stacktop )
338{
339	char		szParameter[ 1024 ];
340
341
342	g_hwndParent	= hwndParent;
343
344	EXDLL_INIT();
345	{
346		popstring( szParameter );
347
348		if( true == FindProc( szParameter ) )
349			wsprintf( szParameter, "1" );
350		else
351			wsprintf( szParameter, "0" );
352
353		setuservariable( INST_R0, szParameter );
354	}
355}
356
357
358
359
360
361//-------------------------------------------------------------------------------------------
362extern "C" __declspec(dllexport) void	KillProcess( HWND		hwndParent, 
363													 int		string_size,
364													 char		*variables, 
365													 stack_t	**stacktop )
366{
367	char		szParameter[ 1024 ];
368
369
370	g_hwndParent	= hwndParent;
371
372	EXDLL_INIT();
373	{
374		popstring( szParameter );
375
376		if( true == KillProc( szParameter ) )
377			wsprintf( szParameter, "1" );
378		else
379			wsprintf( szParameter, "0" );
380
381		setuservariable( INST_R0, szParameter );
382	}
383}
384
385
386
387
388
389//-------------------------------------------------------------------------------------------
390extern "C" __declspec(dllexport) void	FindDevice( HWND		hwndParent, 
391													 int		string_size,
392													 char		*variables, 
393													 stack_t	**stacktop )
394{
395	char		szParameter[ 1024 ];
396
397
398	g_hwndParent	= hwndParent;
399
400	EXDLL_INIT();
401	{
402		popstring( szParameter );
403
404		if( true == FindDev( szParameter ) )
405			wsprintf( szParameter, "1" );
406		else
407			wsprintf( szParameter, "0" );
408
409		setuservariable( INST_R0, szParameter );
410	}
411}