static BOOL RPSendMessagex (UINT uMessage, WPARAM wParam, LPARAM lParam, LPCVOID pData, DWORD dwDataSize, const RPGUESTINFO *pInfo, LRESULT *plResult) { BOOL v = FALSE; static int cnt; int ncnt; int dolog = log_rp; if (!pInfo) { write_log (_T("RPSEND: pInfo == NULL!\n")); return FALSE; } if (!pInfo->hHostMessageWindow) { write_log (_T("RPSEND: pInfo->hHostMessageWindow == NULL!\n")); return FALSE; } if (uMessage == RP_IPC_TO_HOST_DEVICESEEK) dolog = 0; recursive++; cnt++; ncnt = cnt; if (dolog & 1) write_log (_T("RPSEND_%d->\n"), ncnt); v = RPSendMessage (uMessage, wParam, lParam, pData, dwDataSize, pInfo, plResult); recursive--; if (dolog & 1) { write_log (_T("RPSEND_%d(%s [%d], %08x, %08x, %08x, %d)\n"), ncnt, getmsg (uMessage), uMessage - WM_APP, wParam, lParam, pData, dwDataSize); if (v == FALSE) write_log (_T("ERROR %d\n"), GetLastError ()); } return v; }
HRESULT RPInitializeGuest(RPGUESTINFO *pInfo, HINSTANCE hInstance, LPCTSTR pszHostInfo, RPGUESTMSGFN pfnMsgFunction, LPARAM lMsgFunctionParam) { _TCHAR szGuestClass[(sizeof(g_szGuestWndClass)/sizeof(_TCHAR))+20]; _TCHAR *pszHostClass; LRESULT lr; if (!pInfo || !pszHostInfo) return E_POINTER; pInfo->hInstance = hInstance; pInfo->hHostMessageWindow = NULL; pInfo->hGuestMessageWindow = NULL; pInfo->bGuestClassRegistered = FALSE; pInfo->pfnMsgFunction = pfnMsgFunction; pInfo->lMsgFunctionParam = lMsgFunctionParam; // find the host message window // pszHostClass = (_TCHAR *)LocalAlloc(LMEM_FIXED, (_tcslen(g_szHostWndClass) + _tcslen(pszHostInfo) + 1) * sizeof(_TCHAR)); if (!pszHostClass) return E_OUTOFMEMORY; wsprintf(pszHostClass, g_szHostWndClass, pszHostInfo); pInfo->hHostMessageWindow = FindWindow(pszHostClass, NULL); LocalFree(pszHostClass); if (!pInfo->hHostMessageWindow) return HRESULT_FROM_WIN32(ERROR_HOST_UNREACHABLE); // create the guest message window // wsprintf(szGuestClass, g_szGuestWndClass, GetCurrentProcessId()); if (!RegisterWndClass(szGuestClass, hInstance)) return HRESULT_FROM_WIN32(GetLastError()); pInfo->bGuestClassRegistered = TRUE; // pInfo->hGuestMessageWindow = CreateWindow(szGuestClass, NULL, 0, 0,0, 1,1, NULL, NULL, hInstance, (LPVOID)pInfo); if (!pInfo->hGuestMessageWindow) { RPUninitializeGuest(pInfo); return HRESULT_FROM_WIN32(GetLastError()); } // register with the host // if (!RPSendMessage(RP_IPC_TO_HOST_REGISTER, 0, 0, g_szRegistration, sizeof(g_szRegistration), pInfo, &lr)) { RPUninitializeGuest(pInfo); return HRESULT_FROM_WIN32(ERROR_HOST_UNREACHABLE); } if (!lr) { RPUninitializeGuest(pInfo); return HRESULT_FROM_WIN32(ERROR_INVALID_ACCESS); } return S_OK; }
HRESULT RPInitializeGuest(RPGUESTINFO *pInfo, HINSTANCE hInstance, LPCTSTR pszHostInfo, RPGUESTMSGFN pfnMsgFunction, LPARAM lMsgFunctionParam) { _TCHAR szGuestClass[(sizeof(g_szGuestWndClass)/sizeof(_TCHAR))+20]; _TCHAR *pszHostClass; RAWINPUTDEVICE rid; LRESULT lr; if (!pInfo || !pszHostInfo) return E_POINTER; pInfo->hInstance = hInstance; pInfo->hHostMessageWindow = NULL; pInfo->hGuestMessageWindow = NULL; pInfo->bGuestClassRegistered = FALSE; pInfo->pfnMsgFunction = pfnMsgFunction; pInfo->lMsgFunctionParam = lMsgFunctionParam; // find the host message window // pszHostClass = (_TCHAR *)LocalAlloc(LMEM_FIXED, (_tcslen(g_szHostWndClass) + _tcslen(pszHostInfo) + 1) * sizeof(_TCHAR)); if (!pszHostClass) { RPUninitializeGuest(pInfo); return E_OUTOFMEMORY; } wsprintf(pszHostClass, g_szHostWndClass, pszHostInfo); pInfo->hHostMessageWindow = FindWindow(pszHostClass, NULL); LocalFree(pszHostClass); if (!pInfo->hHostMessageWindow) { RPUninitializeGuest(pInfo); return HRESULT_FROM_WIN32(ERROR_HOST_UNREACHABLE); } // create the guest message window // wsprintf(szGuestClass, g_szGuestWndClass, GetCurrentProcessId()); if (!RegisterWndClass(szGuestClass, hInstance)) { RPUninitializeGuest(pInfo); return HRESULT_FROM_WIN32(GetLastError()); } pInfo->bGuestClassRegistered = TRUE; // pInfo->hGuestMessageWindow = CreateWindow(szGuestClass, NULL, 0, 0,0, 1,1, NULL, NULL, hInstance, (LPVOID)pInfo); if (!pInfo->hGuestMessageWindow) { RPUninitializeGuest(pInfo); return HRESULT_FROM_WIN32(GetLastError()); } // register with the host // if (!RPSendMessage(RP_IPC_TO_HOST_REGISTER, 0, 0, g_szRegistration, sizeof(g_szRegistration), pInfo, &lr)) { RPUninitializeGuest(pInfo); return HRESULT_FROM_WIN32(ERROR_HOST_UNREACHABLE); } if (!lr) { RPUninitializeGuest(pInfo); return HRESULT_FROM_WIN32(ERROR_INVALID_ACCESS); } // disable system shortcuts (e.g. Windows-key shortcuts) while the guest is the foreground app rid.usUsagePage = 0x01; rid.usUsage = 0x06; rid.dwFlags = RIDEV_NOHOTKEYS; rid.hwndTarget = 0; RegisterRawInputDevices(&rid, 1, sizeof(rid)); return S_OK; }