/[rdesktop]/sourceforge.net/trunk/seamlessrdp/ServerExe/HookDll/hookdll.cpp
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /sourceforge.net/trunk/seamlessrdp/ServerExe/HookDll/hookdll.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 993 by astrand, Sun Aug 28 12:58:39 2005 UTC revision 1004 by astrand, Tue Aug 30 11:28:55 2005 UTC
# Line 5  Line 5 
5  #include "hookdll.h"  #include "hookdll.h"
6  #include <windows.h>  #include <windows.h>
7  #include <winuser.h>  #include <winuser.h>
8    #include <stdio.h>
9    #include <stdarg.h>
10    
11  #include "wtsapi32.h"  #include "wtsapi32.h"
12  #include "Cchannel.h"  #include "Cchannel.h"
# Line 22  HWND hWnd = 0; Line 24  HWND hWnd = 0;
24    
25  #pragma comment(linker, "/section:SHAREDDATA,rws")  #pragma comment(linker, "/section:SHAREDDATA,rws")
26    
27    #define snprintf _snprintf
28    
29  bool bHooked = false;  bool bHooked = false;
30  bool bHooked2 = false;  bool bHooked2 = false;
31  bool bHooked3 = false;  bool bHooked3 = false;
32  HHOOK hhook = 0;                //cbt  HHOOK hhook = 0; //cbt
33  HHOOK hhook2 = 0;               //shell  HHOOK hhook2 = 0; //shell
34  HHOOK hhook3 = 0;               //wnd proc  HHOOK hhook3 = 0; //wnd proc
35  HINSTANCE hInst = 0;  HINSTANCE hInst = 0;
36  HANDLE m_vcHandle = 0;  HANDLE m_vcHandle = 0;
37    
38  BOOL APIENTRY DllMain( HINSTANCE hinstDLL, DWORD ul_reason_for_call,  
39                         LPVOID lpReserved )  void SendDebug( char *format, ... )
40    {
41        va_list argp;
42        char buf [ 256 ];
43        
44        va_start( argp, format );
45        vsprintf( buf, format, argp );
46        va_end( argp );
47        
48        WriteToChannel( "DEBUG1," );
49        WriteToChannel( buf );
50        WriteToChannel( "\n" );
51    }
52    
53    
54    
55    BOOL APIENTRY DllMain( HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved )
56  {  {
57      switch ( ul_reason_for_call ) {      switch ( ul_reason_for_call ) {
58          case DLL_PROCESS_ATTACH: {          case DLL_PROCESS_ATTACH: {
# Line 63  LRESULT CALLBACK CallWndProc( int nCode, Line 83  LRESULT CALLBACK CallWndProc( int nCode,
83          return CallNextHookEx( hhook3, nCode, wParam, lParam );          return CallNextHookEx( hhook3, nCode, wParam, lParam );
84      }      }
85            
     PCHAR buffer = NULL;  
86      char windowTitle[ 150 ] = { ""      char windowTitle[ 150 ] = { ""
87                                };                                };
88      HWND windowHandle = NULL;      HWND windowHandle = NULL;
89        HWND windowHandle2 = NULL;
90      char result[ 255 ] = { ""      char result[ 255 ] = { ""
91                           };                           };
     char strWindowId[ 25 ];  
     char type[ 25 ];  
       
     LONG b, t, l, r;  
     char strB[ 5 ];  
     char strT[ 5 ];  
     char strL[ 5 ];  
     char strR[ 5 ];  
     RECT rect;  
       
92      CWPSTRUCT *details = ( CWPSTRUCT * ) lParam;      CWPSTRUCT *details = ( CWPSTRUCT * ) lParam;
93        CREATESTRUCT *cs = ( CREATESTRUCT * ) details->lParam;
94        LONG dwStyle = GetWindowLong( details->hwnd, GWL_STYLE );
95        WINDOWPOS *wp = ( WINDOWPOS * ) details->lParam;
96        RECT *rect = ( RECT * ) details->lParam;
97            
98      switch ( details->message ) {      switch ( details->message ) {
99        
100          case WM_SIZING:          case WM_SIZING:
101          case WM_MOVING:          case WM_MOVING:
102            if ( !( dwStyle & WS_VISIBLE ) )
103                break;
104                
105            if ( !( dwStyle & WS_DLGFRAME ) )
106                break;
107                
108            snprintf( result, sizeof( result ),
109                      "POSITION1,0x%p,%d,%d,%d,%d,0x%x",
110                      details->hwnd,
111                      rect->left, rect->top,
112                      rect->right - rect->left,
113                      rect->bottom - rect->top,
114                      0 );
115            result[ sizeof( result ) - 1 ] = '\0';
116            break;
117                    
         windowHandle = details->hwnd;  
         //get win name  
         GetWindowText( windowHandle, windowTitle, 150 );  
118                    
119          //get an id for it          /* Note: WM_WINDOWPOSCHANGING/WM_WINDOWPOSCHANGED are
120          itoa( ( int ) windowHandle, strWindowId, 10 );          strange. Sometimes, for example when bringing up the
121            Notepad About dialog, only an WM_WINDOWPOSCHANGING is
122            sent. In some other cases, for exmaple when opening
123            Format->Text in Notepad, both events are sent. Also, for
124            some reason, when closing the Notepad About dialog, an
125            WM_WINDOWPOSCHANGING event is sent which looks just like
126            the event that was sent when the About dialog was opened...  */
127            case WM_WINDOWPOSCHANGING:
128                    
129          //get coords          if ( !( dwStyle & WS_VISIBLE ) )
130          GetWindowRect( windowHandle, &rect );              break;
131          b = rect.bottom;              
132          t = rect.top;          if ( !( dwStyle & WS_DLGFRAME ) )
133          l = rect.left;              break;
134          r = rect.right;              
135          ltoa( b, strB, 10 );          if ( !( wp->flags & SWP_NOZORDER ) ) {
136          ltoa( t, strT, 10 );              snprintf( result, sizeof( result ),
137          ltoa( r, strR, 10 );                        "ZCHANGE1,0x%p,0x%p,0x%x\n",
138          ltoa( l, strL, 10 );                        details->hwnd,
139                          wp->flags & SWP_NOACTIVATE ? wp->hwndInsertAfter : 0,
140                          0 );
141                result[ sizeof( result ) - 1 ] = '\0';
142            }
143            break;
144                    
         ////setup return string  
         strcat( result, "MSG=CALLWNDPROC_WM_MOVING;" );  
         strcat( result, "ID=" );  
         strcat( result, strWindowId );  
         strcat( result, ";" );  
         strcat( result, "TITLE=" );  
         strcat( result, windowTitle );  
         strcat( result, ";" );  
         strcat( result, "POS=" );  
         strcat( result, strL );  
         strcat( result, "~" );  
         strcat( result, strT );  
         strcat( result, "~" );  
         strcat( result, strR );  
         strcat( result, "~" );  
         strcat( result, strB );  
         strcat( result, ";" );  
145                    
146          buffer = result;          case WM_CREATE:
147            if ( cs->style & WS_DLGFRAME ) {
148                snprintf( result, sizeof( result ),
149                          "CREATE1,0x%p,0x%x\n",
150                          details->hwnd, 0 );
151            }
152            break;
153            
154            
155            case WM_DESTROY:
156            if ( dwStyle & WS_DLGFRAME ) {
157                snprintf( result, sizeof( result ),
158                          "DESTROY1,0x%p,0x%x\n",
159                          details->hwnd, 0 );
160            }
161                    
162          break;          break;
163                    
164            
165          default:          default:
166          break;          break;
167      }      }
168            
169      if ( ChannelIsOpen() ) {      if ( result[ 0 ] != '\0' ) {
170          if ( buffer != NULL ) {          WriteToChannel( result );
             WriteToChannel( buffer );  
         }  
171      }      }
172            
173      return CallNextHookEx( hhook3, nCode, wParam, lParam );      return CallNextHookEx( hhook3, nCode, wParam, lParam );
# Line 144  LRESULT CALLBACK CbtProc( int nCode, WPA Line 179  LRESULT CALLBACK CbtProc( int nCode, WPA
179          return CallNextHookEx( hhook, nCode, wParam, lParam );          return CallNextHookEx( hhook, nCode, wParam, lParam );
180      }      }
181            
182        char windowTitle[ 150 ] = { ""
183                                  };
184        HWND windowHandle = NULL;
185        char result[ 255 ] = { ""
186                             };
187        switch ( nCode ) {
188            case HCBT_MINMAX:
189            
190            if ( ( LOWORD( lParam ) == SW_SHOWMINIMIZED )
191                    || ( LOWORD( lParam ) == SW_MINIMIZE ) ) {
192                MessageBox( 0, "Minimizing windows is not allowed in this version. Sorry!", "SeamlessRDP", MB_OK );
193                return 1;
194            }
195            
196            GetWindowText( ( HWND ) wParam, windowTitle, 150 );
197            
198            snprintf( result, sizeof( result ),
199                      "SETSTATE1,0x%p,%s,0x%x,0x%x\n",
200                      ( HWND ) wParam,
201                      windowTitle,
202                      LOWORD( lParam ),
203                      0 );
204            break;
205            
206            
207            default:
208            break;
209        }
210            
211      PCHAR buffer = NULL;      if ( result[ 0 ] != '\0' ) {
212            WriteToChannel( result );
213        }
214            
215        return CallNextHookEx( hhook, nCode, wParam, lParam );
216    }
217    
218    
219    LRESULT CALLBACK ShellProc( int nCode, WPARAM wParam, LPARAM lParam )
220    {
221        if ( nCode < 0 ) {
222            return CallNextHookEx( hhook, nCode, wParam, lParam );
223        }
224            
225      char windowTitle[ 150 ] = { ""      char windowTitle[ 150 ] = { ""
226                                };                                };
# Line 154  LRESULT CALLBACK CbtProc( int nCode, WPA Line 228  LRESULT CALLBACK CbtProc( int nCode, WPA
228      char result[ 255 ] = { ""      char result[ 255 ] = { ""
229                           };                           };
230      char strWindowId[ 25 ];      char strWindowId[ 25 ];
     char type[ 25 ];  
       
       
231      LONG b, t, l, r;      LONG b, t, l, r;
232      char strB[ 5 ];      char strW[ 5 ];
233      char strT[ 5 ];      char strY[ 5 ];
234      char strL[ 5 ];      char strX[ 5 ];
235      char strR[ 5 ];      char strH[ 5 ];
236      RECT rect;      RECT rect;
237            
     int i = 0;                  //tmp  
       
238      switch ( nCode ) {      switch ( nCode ) {
239          case HCBT_MINMAX:          case HSHELL_WINDOWCREATED:
240                    
241            //get window id
242          windowHandle = ( HWND ) wParam;          windowHandle = ( HWND ) wParam;
         //get win name  
         GetWindowText( windowHandle, windowTitle, 150 );  
           
         //get an id for it  
243          itoa( ( int ) windowHandle, strWindowId, 10 );          itoa( ( int ) windowHandle, strWindowId, 10 );
244                    
         //get operation type(min,max). if max, do not clip at all,if min use window's previous coords  
         //codes are:  
           
         // SW_HIDE= 0  SW_SHOWNORMAL=1  SW_NORMAL=1  SW_SHOWMINIMIZED=2  SW_SHOWMAXIMIZED=3  SW_MAXIMIZE=3  
         // SW_SHOWNOACTIVATE=4  SW_SHOW=5  SW_MINIMIZE=6  SW_SHOWMINNOACTIVE=7  SW_SHOWNA=8  SW_RESTORE=9  
         // SW_SHOWDEFAULT=10  SW_FORCEMINIMIZE=11  SW_MAX=11  
           
         itoa( ( int ) lParam, type, 10 );  
           
245          //get coords          //get coords
246          GetWindowRect( windowHandle, &rect );          GetWindowRect( windowHandle, &rect );
247          b = rect.bottom;          b = rect.bottom;
248          t = rect.top;          t = rect.top;
249          l = rect.left;          l = rect.left;
250          r = rect.right;          r = rect.right;
251          ltoa( b, strB, 10 );          ltoa( b - t, strH, 10 );
252          ltoa( t, strT, 10 );          ltoa( t, strY, 10 );
253          ltoa( r, strR, 10 );          ltoa( r - l, strW, 10 );
254          ltoa( l, strL, 10 );          ltoa( l, strX, 10 );
255                    
256          //get name          //get name
257          GetWindowText( windowHandle, windowTitle, 150 );          GetWindowText( windowHandle, windowTitle, 150 );
258                    
259          ////setup return string          ////setup return string
260          strcat( result, "MSG=HCBT_MINMAX;" );          strcat( result, "MSG=HSHELL_WINDOWCREATED;OP=0;" );
261          strcat( result, "ID=" );          strcat( result, "ID=" );
262          strcat( result, strWindowId );          strcat( result, strWindowId );
263          strcat( result, ";" );          strcat( result, ";" );
264          strcat( result, "TITLE=" );          strcat( result, "TITLE=" );
265          strcat( result, windowTitle );          strcat( result, windowTitle );
266          strcat( result, ";" );          strcat( result, ";" );
267          strcat( result, "POS=" );          strcat( result, "X=" );
268          strcat( result, strL );          strcat( result, strX );
         strcat( result, "~" );  
         strcat( result, strT );  
         strcat( result, "~" );  
         strcat( result, strR );  
         strcat( result, "~" );  
         strcat( result, strB );  
269          strcat( result, ";" );          strcat( result, ";" );
270          strcat( result, "TYPE=" );          strcat( result, "Y=" );
271          strcat( result, type );          strcat( result, strY );
272          strcat( result, ";" );          strcat( result, ";" );
273                    strcat( result, "H=" );
274          //-------------------------------------------------------------------------------------------------          strcat( result, strH );
275          // code to prevent minimising windows (can be removed once minimise has been implemented)          strcat( result, ";" );
276          i = ( int ) lParam;          strcat( result, "W=" );
277          //if (i==0 || i==2 || i==6 || i==7 || i==8 || i==11)          strcat( result, strW );
278          if ( i == 2 || i == 6 ) {          strcat( result, "." );
             MessageBox( 0,  
                         "Minimising windows is not allowed in this version. Sorry!",  
                         "TS Window Clipper", MB_OK );  
             return 1;  
         }  
         //-----------------------------------------------------------------------------------------  
           
         //-------------------------------------------------------------------------------------------------  
         // code to prevent maximising windows (can be removed once maximise has been implemented)  
         i = ( int ) lParam;  
         //if (i==3 || i==9 || i==11)  
         if ( i == 3 || i == 11 ) {  
             MessageBox( 0,  
                         "Maximising windows is not allowed in this version. Sorry!",  
                         "TS Window Clipper", MB_OK );  
             return 1;  
         }  
         //-----------------------------------------------------------------------------------------  
           
         buffer = result;  
279                    
280          break;          break;
281                    
282          case HCBT_MOVESIZE:          case HSHELL_WINDOWDESTROYED:
283                    
284            //get window id
285          windowHandle = ( HWND ) wParam;          windowHandle = ( HWND ) wParam;
         //get win name  
         GetWindowText( windowHandle, windowTitle, 150 );  
           
         //get an id for it  
286          itoa( ( int ) windowHandle, strWindowId, 10 );          itoa( ( int ) windowHandle, strWindowId, 10 );
287                    
288          //get coords          //get coords
# Line 263  LRESULT CALLBACK CbtProc( int nCode, WPA Line 291  LRESULT CALLBACK CbtProc( int nCode, WPA
291          t = rect.top;          t = rect.top;
292          l = rect.left;          l = rect.left;
293          r = rect.right;          r = rect.right;
294          ltoa( b, strB, 10 );          ltoa( b - t, strH, 10 );
295          ltoa( t, strT, 10 );          ltoa( t, strY, 10 );
296          ltoa( r, strR, 10 );          ltoa( r - l, strW, 10 );
297          ltoa( l, strL, 10 );          ltoa( l, strX, 10 );
298                    
299          //get name          //get name
300          GetWindowText( windowHandle, windowTitle, 150 );          GetWindowText( windowHandle, windowTitle, 150 );
301                    
302          ////setup return string          ////setup return string
303          strcat( result, "MSG=HCBT_MOVESIZE;" );          strcat( result, "MSG=HSHELL_WINDOWDESTROYED;OP=1;" );
304          strcat( result, "ID=" );          strcat( result, "ID=" );
305          strcat( result, strWindowId );          strcat( result, strWindowId );
306          strcat( result, ";" );          strcat( result, ";" );
307          strcat( result, "TITLE=" );          strcat( result, "TITLE=" );
308          strcat( result, windowTitle );          strcat( result, windowTitle );
309          strcat( result, ";" );          strcat( result, ";" );
310          strcat( result, "POS=" );          strcat( result, "X=" );
311          strcat( result, strL );          strcat( result, strX );
         strcat( result, "~" );  
         strcat( result, strT );  
         strcat( result, "~" );  
         strcat( result, strR );  
         strcat( result, "~" );  
         strcat( result, strB );  
312          strcat( result, ";" );          strcat( result, ";" );
313                    strcat( result, "Y=" );
314          buffer = result;          strcat( result, strY );
315            strcat( result, ";" );
316            strcat( result, "H=" );
317            strcat( result, strH );
318            strcat( result, ";" );
319            strcat( result, "W=" );
320            strcat( result, strW );
321            strcat( result, "." );
322                    
323          break;          break;
         case HCBT_SETFOCUS:  
         //buffer = "HCBT_SETFOCUS";  
         //not needed yet  
         break;  
324          default:          default:
325          break;          break;
326      }      }
327            
328      if ( ChannelIsOpen() ) {      if ( result[ 0 ] != '\0' ) {
329          if ( buffer != NULL ) {          WriteToChannel( result );
             WriteToChannel( buffer );  
         }  
     }  
       
     return CallNextHookEx( hhook, nCode, wParam, lParam );  
 }  
   
   
 LRESULT CALLBACK ShellProc( int nCode, WPARAM wParam, LPARAM lParam )  
 {  
     if ( nCode < 0 ) {  
         return CallNextHookEx( hhook, nCode, wParam, lParam );  
     }  
       
     if ( ChannelIsOpen() ) {  
         PCHAR buffer = NULL;  
           
         char windowTitle[ 150 ] = { ""  
                                   };  
         HWND windowHandle = NULL;  
         char result[ 255 ] = { ""  
                              };  
         char strWindowId[ 25 ];  
         LONG b, t, l, r;  
         char strB[ 5 ];  
         char strT[ 5 ];  
         char strL[ 5 ];  
         char strR[ 5 ];  
         RECT rect;  
           
         switch ( nCode ) {  
             case HSHELL_WINDOWCREATED:  
               
             //get window id  
             windowHandle = ( HWND ) wParam;  
             itoa( ( int ) windowHandle, strWindowId, 10 );  
               
             //get coords  
             GetWindowRect( windowHandle, &rect );  
             b = rect.bottom;  
             t = rect.top;  
             l = rect.left;  
             r = rect.right;  
             ltoa( b, strB, 10 );  
             ltoa( t, strT, 10 );  
             ltoa( r, strR, 10 );  
             ltoa( l, strL, 10 );  
               
             //get name  
             GetWindowText( windowHandle, windowTitle, 150 );  
               
             ////setup return string  
             strcat( result, "MSG=HSHELL_WINDOWCREATED;" );  
             strcat( result, "ID=" );  
             strcat( result, strWindowId );  
             strcat( result, ";" );  
             strcat( result, "TITLE=" );  
             strcat( result, windowTitle );  
             strcat( result, ";" );  
             strcat( result, "POS=" );  
             strcat( result, strL );  
             strcat( result, "~" );  
             strcat( result, strT );  
             strcat( result, "~" );  
             strcat( result, strR );  
             strcat( result, "~" );  
             strcat( result, strB );  
             strcat( result, ";" );  
               
             buffer = result;  
               
             break;  
               
             case HSHELL_WINDOWDESTROYED:  
               
             //get window id  
             windowHandle = ( HWND ) wParam;  
             itoa( ( int ) windowHandle, strWindowId, 10 );  
               
             //get name  
             GetWindowText( windowHandle, windowTitle, 150 );  
               
             ////setup return string  
             strcat( result, "MSG=HSHELL_WINDOWDESTROYED;" );  
             strcat( result, "ID=" );  
             strcat( result, strWindowId );  
             strcat( result, ";" );  
             strcat( result, "TITLE=" );  
             strcat( result, windowTitle );  
             strcat( result, ";" );  
               
             buffer = result;  
               
             break;  
             default:  
             break;  
         }  
           
         if ( buffer != NULL ) {  
             WriteToChannel( buffer );  
         }  
330      }      }
331            
332      return CallNextHookEx( hhook, nCode, wParam, lParam );      return CallNextHookEx( hhook, nCode, wParam, lParam );
# Line 411  LRESULT CALLBACK ShellProc( int nCode, W Line 335  LRESULT CALLBACK ShellProc( int nCode, W
335  DLL_EXPORT void SetCbtHook( void )  DLL_EXPORT void SetCbtHook( void )
336  {  {
337      if ( !bHooked ) {      if ( !bHooked ) {
338          hhook =          hhook = SetWindowsHookEx( WH_CBT, ( HOOKPROC ) CbtProc, hInst, ( DWORD ) NULL );
             SetWindowsHookEx( WH_CBT, ( HOOKPROC ) CbtProc, hInst, ( DWORD ) NULL );  
339          bHooked = true;          bHooked = true;
340      }      }
341            
342      if ( !bHooked2 ) {      if ( !bHooked2 ) {
343          hhook2 =          hhook2 = SetWindowsHookEx( WH_SHELL, ( HOOKPROC ) ShellProc, hInst, ( DWORD ) NULL );
             SetWindowsHookEx( WH_SHELL, ( HOOKPROC ) ShellProc, hInst,  
                               ( DWORD ) NULL );  
344          bHooked2 = true;          bHooked2 = true;
345      }      }
346            
347      if ( !bHooked3 ) {      if ( !bHooked3 ) {
348          hhook3 =          hhook3 = SetWindowsHookEx( WH_CALLWNDPROC, ( HOOKPROC ) CallWndProc, hInst, ( DWORD ) NULL );
             SetWindowsHookEx( WH_CALLWNDPROC, ( HOOKPROC ) CallWndProc, hInst,  
                               ( DWORD ) NULL );  
349          bHooked3 = true;          bHooked3 = true;
350      }      }
351  }  }
# Line 456  DLL_EXPORT int GetInstanceCount() Line 375  DLL_EXPORT int GetInstanceCount()
375    
376  int OpenVirtualChannel()  int OpenVirtualChannel()
377  {  {
378      m_vcHandle =      m_vcHandle = WTSVirtualChannelOpen( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, CHANNELNAME );
379          WTSVirtualChannelOpen( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION,      
                                CHANNELNAME );  
                                 
380      if ( m_vcHandle == NULL ) {      if ( m_vcHandle == NULL ) {
381          return 0;          return 0;
382      } else {      } else {
# Line 494  int WriteToChannel( PCHAR buffer ) Line 411  int WriteToChannel( PCHAR buffer )
411      PULONG bytesRead = 0;      PULONG bytesRead = 0;
412      PULONG pBytesWritten = 0;      PULONG pBytesWritten = 0;
413            
414      BOOL result =      if ( !ChannelIsOpen() )
415          WTSVirtualChannelWrite( m_vcHandle, buffer, ( ULONG ) strlen( buffer ),          return 1;
416                                  pBytesWritten );          
417                                        BOOL result = WTSVirtualChannelWrite( m_vcHandle, buffer, ( ULONG ) strlen( buffer ), pBytesWritten );
418        
419      if ( result ) {      if ( result ) {
420          return 1;          return 1;
421      } else {      } else {

Legend:
Removed from v.993  
changed lines
  Added in v.1004

  ViewVC Help
Powered by ViewVC 1.1.26