/** Map one key to another key. @param nCode Code used to determine how to process the message. @param wParam Identifier of the keyboard message. @param lParam Pointer to KBDLLHOOKSTRUCT structure. @return 1 if the keyboard message is processed, i.e. a key is mapped to another key, otherwise call CallNextHookEx and return the value it returns. */ LRESULT CALLBACK keyboardHook(int nCode, WPARAM wParam, LPARAM lParam) { KBDLLHOOKSTRUCT *p = (KBDLLHOOKSTRUCT *) lParam; WORD keyCode = (WORD) p->vkCode; WORD mapCode = my.keymap[keyCode]; if (my.debug || my.file) { logKey(wParam, lParam); } if (mapCode == 0) { /* If key pressed is unmapped, disable the key press. */ return 1; } else if (keyCode != mapCode && !(p->flags & LLKHF_INJECTED) && nCode >= 0) { /* If key is mapped, translate it to what it is mapped to. */ INPUT inputs[1]; PKEYBDINPUT ki = &inputs[0].ki; inputs[0].type = INPUT_KEYBOARD; ki->dwExtraInfo = ki->time = ki->wScan = 0; ki->wVk = mapCode; ki->dwFlags = (wParam == WM_KEYUP || wParam == WM_SYSKEYUP) ? KEYEVENTF_KEYUP : 0; SendInput(1, inputs, sizeof *inputs); return 1; } return CallNextHookEx(my.hook, nCode, wParam, lParam); }
bool CNtlLogSystem::RegisterChannel( DWORD dwSource, BYTE byChannel, char* pszChannelName, char* pszLogFileNamePrefix, char* pszLogFileNameSuffix, char* pszLogFileExtName, bool bIsOn) { if (NULL == pszChannelName || NULL == pszLogFileNamePrefix || NULL == pszLogFileNameSuffix || NULL == pszLogFileExtName) { return false; } ::EnterCriticalSection(&m_lock); sSOURCE_INFO* pSourceInfo = GetSourceInfo(dwSource); if (NULL == pSourceInfo) { ::LeaveCriticalSection(&m_lock); return false; } sCHANNEL_INFO* pChannelInfo = GetChannelInfo(pSourceInfo, byChannel); if (NULL != pChannelInfo) { ::LeaveCriticalSection(&m_lock); return false; } // Creates channel info. { pChannelInfo = new sCHANNEL_INFO(bIsOn); if (NULL == pChannelInfo) { ::LeaveCriticalSection(&m_lock); return false; } NTL_SAFE_STRCPY(pChannelInfo->szChannelName, pszChannelName); if (false == (pSourceInfo->mapChannel).insert(std::pair<BYTE, sCHANNEL_INFO*>(byChannel, pChannelInfo)).second) { SAFE_DELETE(pChannelInfo); ::LeaveCriticalSection(&m_lock); return false; } } sLOG_FILE_INFO* pLogFileInfo = NULL; // Creates log file info. { CNtlString strLogFileKey; strLogFileKey.Format("%s_%s", pszLogFileNamePrefix, pszLogFileNameSuffix); std::map<CNtlString, sLOG_FILE_INFO*>::iterator iterLogFile; iterLogFile = (pSourceInfo->mapLoggingFileInfo).find(strLogFileKey); if ((pSourceInfo->mapLoggingFileInfo).end() == iterLogFile) { pLogFileInfo = new sLOG_FILE_INFO; if (NULL == pLogFileInfo) { (pSourceInfo->mapChannel).erase(byChannel); SAFE_DELETE(pChannelInfo); ::LeaveCriticalSection(&m_lock); return false; } NTL_SAFE_STRCPY(pLogFileInfo->szLogFileNamePrefix, pszLogFileNamePrefix); NTL_SAFE_STRCPY(pLogFileInfo->szLogFileNameSuffix, pszLogFileNameSuffix); NTL_SAFE_STRCPY(pLogFileInfo->szLogFileExtName, pszLogFileExtName); if (false == (pSourceInfo->mapLoggingFileInfo).insert(std::pair<CNtlString, sLOG_FILE_INFO*>(strLogFileKey, pLogFileInfo)).second) { SAFE_DELETE(pLogFileInfo); (pSourceInfo->mapChannel).erase(byChannel); SAFE_DELETE(pChannelInfo); ::LeaveCriticalSection(&m_lock); return false; } if (false == OpenLogFile(pLogFileInfo)) { (pSourceInfo->mapLoggingFileInfo).erase(strLogFileKey); SAFE_DELETE(pLogFileInfo); (pSourceInfo->mapChannel).erase(byChannel); SAFE_DELETE(pChannelInfo); ::LeaveCriticalSection(&m_lock); return false; } ::EnterCriticalSection(&(pLogFileInfo->lock)); pLogFileInfo->dwRefCount = 1; } else { pLogFileInfo = iterLogFile->second; ::EnterCriticalSection(&(pLogFileInfo->lock)); (pLogFileInfo->dwRefCount)++; } sLOG_FILE_KEY logKey(dwSource, byChannel); if (false == m_mapLogFileRef.insert(std::pair<sLOG_FILE_KEY, sLOG_FILE_INFO*>(logKey, pLogFileInfo)).second) { (pLogFileInfo->dwRefCount)--; if (0 == pLogFileInfo->dwRefCount) { ::LeaveCriticalSection(&(pLogFileInfo->lock)); (pSourceInfo->mapLoggingFileInfo).erase(strLogFileKey); SAFE_DELETE(pLogFileInfo); } else { ::LeaveCriticalSection(&(pLogFileInfo->lock)); } (pSourceInfo->mapChannel).erase(byChannel); SAFE_DELETE(pChannelInfo); ::LeaveCriticalSection(&m_lock); return false; } else { ::LeaveCriticalSection(&(pLogFileInfo->lock)); } } ::LeaveCriticalSection(&m_lock); return true; }