Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
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;
}