BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID /*lpReserved*/)
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		OutputDebugStringA("#islogkbdhooklib::DLL_PROCESS_ATTACH# called.");
		__hKbdHookModule = hModule;
		islogkbdlib::KbdSettings::getInstance()->Initialize();
		islogkbdlib::KbdLogs::getInstance()->Initialize();
		break;
	case DLL_PROCESS_DETACH:
		OutputDebugStringA("#islogkbdhooklib::DLL_PROCESS_DETACH# called.");
		if(s_hWndServer != NULL)
			ClearHook(s_hWndServer);
		islogkbdlib::KbdLogs::getInstance()->Uninitialize();
		islogkbdlib::KbdSettings::getInstance()->Uninitialize(); 
		break;
	}
	return TRUE;
}
Example #2
0
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_HOOK, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	int length = lstrlen(lpCmdLine); //_tcslen
	if (length > 0)
	{
		// Perform application initialization:
		if (!InitInstance (hInstance, SW_HIDE))
		//if (!InitInstance (hInstance, SW_SHOW))
		{
			return FALSE;
		}

		int pos = 0;
		int paramIndex = 0;
		bool paramUsed = false;
		while (pos < length)
		{
			if ((lpCmdLine[pos] >= '0') && (lpCmdLine[pos] <= '9'))
			{
				paramUsed = true;
				int val = (lpCmdLine[pos] - '0');
				switch (paramIndex)
				{
					case 0:
						hwnd *= 10;
						hwnd += val;
						break;
					case 1:
						msgId *= 10;
						msgId += val;
						break;
					case 2:
						msgId_LL *= 10;
						msgId_LL += val;
						break;
					default:
						break;
				}
			}
			else if (lpCmdLine[pos] == ' ')
			{
				if (paramUsed == true)
				{
					paramIndex++;
					paramUsed = false;
				}
			}
			else
			{
				return -1; // Invalid characters detected parsing parameters.
			}
			pos++;
		}

		if (paramUsed == false)
			paramIndex--;

		if (paramIndex != 2)
			return -2; // Invalid number of arguments
	}
	else
	{
		// Perform application initialization:
		if (!InitInstance (hInstance, SW_SHOW))
		{
			return FALSE;
		}

		// No params passed. We'll send to ourself for debugging.
		hwnd = (UINT)_hwnd;
		msgId = 0x401;
		msgId_LL = 0x402;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_HOOK));


	//HWND hwnd = FindWindow(NULL, L"Keyboard Redirector");
	if (hwnd == 1)
		hwnd = (UINT)FindWindow(NULL, L"Keyboard Redirector");
	if (hwnd == 0)
		return -3; // Invalid window handle;

	if (msgId != 0)
		SetHook((HWND)hwnd, msgId);

	if (msgId_LL != 0)
		SetHook_LL((HWND)hwnd, msgId_LL);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	if (msgId != 0)
		ClearHook((HWND)hwnd);

	if (msgId != 0)
		ClearHook_LL((HWND)hwnd);

	return (int) msg.wParam;
}