--- sourceforge.net/trunk/seamlessrdp/ServerExe/HookDll/hookdll.c 2006/03/09 12:00:15 1071 +++ sourceforge.net/trunk/seamlessrdp/ServerExe/HookDll/hookdll.c 2006/03/09 15:48:55 1079 @@ -27,10 +27,8 @@ #include #include -#include -#include -#include "hookdll.h" +#include "../vchannel.h" #define DLL_EXPORT __declspec(dllexport) @@ -51,125 +49,100 @@ static HANDLE g_mutex = NULL; -static HANDLE g_vchannel = NULL; - -static void -debug(char *format, ...) +static LRESULT CALLBACK +wndproc_hook_proc(int code, WPARAM cur_thread, LPARAM details) { - va_list argp; - char buf[256]; + HWND hwnd; + UINT msg; + WPARAM wparam; + LPARAM lparam; - sprintf(buf, "DEBUG1,"); + LONG style; - va_start(argp, format); - _vsnprintf(buf + sizeof("DEBUG1,") - 1, sizeof(buf) - sizeof("DEBUG1,") + 1, format, argp); - va_end(argp); + if (code < 0) + goto end; - vchannel_write(buf); -} + hwnd = ((CWPSTRUCT *) details)->hwnd; + msg = ((CWPSTRUCT *) details)->message; + wparam = ((CWPSTRUCT *) details)->wParam; + lparam = ((CWPSTRUCT *) details)->lParam; -static LRESULT CALLBACK -wndproc_hook_proc(int code, WPARAM cur_thread, LPARAM details) -{ - HWND hwnd = ((CWPSTRUCT *) details)->hwnd; - UINT msg = ((CWPSTRUCT *) details)->message; - WPARAM wparam = ((CWPSTRUCT *) details)->wParam; - LPARAM lparam = ((CWPSTRUCT *) details)->lParam; - - LONG style = GetWindowLong(hwnd, GWL_STYLE); - WINDOWPOS *wp = (WINDOWPOS *) lparam; - RECT rect; + style = GetWindowLong(hwnd, GWL_STYLE); - if (code < 0) + if (style & WS_CHILD) goto end; switch (msg) { case WM_WINDOWPOSCHANGED: - if (style & WS_CHILD) - break; + { + RECT rect; + WINDOWPOS *wp = (WINDOWPOS *) lparam; + if (wp->flags & SWP_SHOWWINDOW) + { + // FIXME: Now, just like create! + debug("SWP_SHOWWINDOW for %p!", hwnd); + vchannel_write("CREATE1,0x%p,0x%x", hwnd, 0); + + // FIXME: SETSTATE + + if (!GetWindowRect(hwnd, &rect)) + { + debug("GetWindowRect failed!\n"); + break; + } + vchannel_write("POSITION1,0x%p,%d,%d,%d,%d,0x%x", + hwnd, + rect.left, rect.top, + rect.right - rect.left, + rect.bottom - rect.top, 0); + } - if (wp->flags & SWP_SHOWWINDOW) - { - // FIXME: Now, just like create! - debug("SWP_SHOWWINDOW for %p!", hwnd); - vchannel_write("CREATE1,0x%p,0x%x", hwnd, 0); + if (wp->flags & SWP_HIDEWINDOW) + vchannel_write("DESTROY1,0x%p,0x%x", hwnd, 0); + + if (!(style & WS_VISIBLE)) + break; - // FIXME: SETSTATE + if (wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE) + break; if (!GetWindowRect(hwnd, &rect)) { debug("GetWindowRect failed!\n"); break; } + vchannel_write("POSITION1,0x%p,%d,%d,%d,%d,0x%x", hwnd, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0); - } - - - if (wp->flags & SWP_HIDEWINDOW) - vchannel_write("DESTROY1,0x%p,0x%x", hwnd, 0); - - - if (!(style & WS_VISIBLE)) - break; - - if (wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE) - break; - if (!GetWindowRect(hwnd, &rect)) - { - debug("GetWindowRect failed!\n"); break; } - vchannel_write("POSITION1,0x%p,%d,%d,%d,%d,0x%x", - hwnd, - rect.left, rect.top, - rect.right - rect.left, rect.bottom - rect.top, 0); - - break; - - - /* Note: WM_WINDOWPOSCHANGING/WM_WINDOWPOSCHANGED are - strange. Sometimes, for example when bringing up the - Notepad About dialog, only an WM_WINDOWPOSCHANGING is - sent. In some other cases, for exmaple when opening - Format->Text in Notepad, both events are sent. Also, for - some reason, when closing the Notepad About dialog, an - WM_WINDOWPOSCHANGING event is sent which looks just like - the event that was sent when the About dialog was opened... */ case WM_WINDOWPOSCHANGING: - if (style & WS_CHILD) - break; - - if (!(style & WS_VISIBLE)) - break; - - if (!(wp->flags & SWP_NOZORDER)) - vchannel_write("ZCHANGE1,0x%p,0x%p,0x%x", - hwnd, - wp->flags & SWP_NOACTIVATE ? wp->hwndInsertAfter : 0, - 0); - - break; - + { + WINDOWPOS *wp = (WINDOWPOS *) lparam; + if (!(style & WS_VISIBLE)) + break; + if (!(wp->flags & SWP_NOZORDER)) + vchannel_write("ZCHANGE1,0x%p,0x%p,0x%x", + hwnd, + wp->flags & SWP_NOACTIVATE ? wp-> + hwndInsertAfter : 0, 0); - case WM_DESTROY: - if (style & WS_CHILD) break; + } + case WM_DESTROY: vchannel_write("DESTROY1,0x%p,0x%x", hwnd, 0); - break; - default: break; } @@ -190,7 +163,7 @@ { case HCBT_MINMAX: { - int show; + int show, state; show = LOWORD(lparam); @@ -206,8 +179,14 @@ /* FIXME: Strip title of dangerous characters */ + if (show == SW_SHOWNORMAL) + state = 0; + else if (show == SW_SHOWMINIMIZED) + state = 1; + else if (show == SW_SHOWMAXIMIZED) + state = 2; vchannel_write("SETSTATE1,0x%p,%s,0x%x,0x%x", - (HWND) wparam, title, show, 0); + (HWND) wparam, title, state, 0); break; } @@ -219,72 +198,6 @@ return CallNextHookEx(g_cbt_hook, code, wparam, lparam); } -int -vchannel_open() -{ - g_vchannel = WTSVirtualChannelOpen(WTS_CURRENT_SERVER_HANDLE, - WTS_CURRENT_SESSION, CHANNELNAME); - - if (g_vchannel == NULL) - return 0; - else - return 1; -} - -int -vchannel_close() -{ - BOOL result; - - result = WTSVirtualChannelClose(g_vchannel); - - g_vchannel = NULL; - - if (result) - return 1; - else - return 0; -} - -int -vchannel_is_open() -{ - if (g_vchannel == NULL) - return 0; - else - return 1; -} - -int -vchannel_write(char *format, ...) -{ - BOOL result; - va_list argp; - char buf[1024]; - int size; - ULONG bytes_written; - - if (!vchannel_is_open()) - return 1; - - va_start(argp, format); - size = _vsnprintf(buf, sizeof(buf), format, argp); - va_end(argp); - - if (size >= sizeof(buf)) - return 0; - - WaitForSingleObject(g_mutex, INFINITE); - result = WTSVirtualChannelWrite(g_vchannel, buf, (ULONG) strlen(buf), &bytes_written); - result = WTSVirtualChannelWrite(g_vchannel, "\n", (ULONG) 1, &bytes_written); - ReleaseMutex(g_mutex); - - if (result) - return 1; - else - return 0; -} - DLL_EXPORT void SetHooks(void) { @@ -320,7 +233,7 @@ // remember our instance handle g_instance = hinstDLL; - g_mutex = CreateMutex(NULL, FALSE, "Local\\Seamless"); + g_mutex = CreateMutex(NULL, FALSE, "Local\\SeamlessDLL"); if (!g_mutex) return FALSE;