/[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 1064 by ossman_, Wed Mar 8 15:52:38 2006 UTC revision 1065 by ossman_, Wed Mar 8 16:12:49 2006 UTC
# Line 37  void SendDebug( char *format, ... ) Line 37  void SendDebug( char *format, ... )
37  {  {
38      va_list argp;      va_list argp;
39      char buf [ 256 ];      char buf [ 256 ];
40        
41      va_start( argp, format );      va_start( argp, format );
42      vsprintf( buf, format, argp );      vsprintf( buf, format, argp );
43      va_end( argp );      va_end( argp );
44        
45      WriteToChannel( "DEBUG1," );      WriteToChannel( "DEBUG1," );
46      WriteToChannel( buf );      WriteToChannel( buf );
47      WriteToChannel( "\n" );      WriteToChannel( "\n" );
# Line 52  void SendDebug( char *format, ... ) Line 52  void SendDebug( char *format, ... )
52  extern "C" BOOL APIENTRY DllMain( HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved )  extern "C" BOOL APIENTRY DllMain( HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved )
53  {  {
54      switch ( ul_reason_for_call ) {      switch ( ul_reason_for_call ) {
55          case DLL_PROCESS_ATTACH: {      case DLL_PROCESS_ATTACH: {
56              // remember our instance handle              // remember our instance handle
57              hInst = hinstDLL;              hInst = hinstDLL;
58              ++iInstanceCount;              ++iInstanceCount;
59              OpenVirtualChannel();              OpenVirtualChannel();
60              break;              break;
61          }          }
62            
63          case DLL_THREAD_ATTACH:      case DLL_THREAD_ATTACH:
64          break;          break;
65          case DLL_THREAD_DETACH:      case DLL_THREAD_DETACH:
66          break;          break;
67          case DLL_PROCESS_DETACH: {      case DLL_PROCESS_DETACH: {
68              --iInstanceCount;              --iInstanceCount;
69              CloseVirtualChannel();              CloseVirtualChannel();
70          }          }
71          break;          break;
72      }      }
73        
74      return TRUE;      return TRUE;
75  }  }
76    
# Line 79  LRESULT CALLBACK CallWndProc( int nCode, Line 79  LRESULT CALLBACK CallWndProc( int nCode,
79      if ( nCode < 0 ) {      if ( nCode < 0 ) {
80          return CallNextHookEx( hWndProc, nCode, wParam, lParam );          return CallNextHookEx( hWndProc, nCode, wParam, lParam );
81      }      }
82        
83      char windowTitle[ 150 ] = { ""      char windowTitle[ 150 ] = { ""
84                                };                                };
85      HWND windowHandle = NULL;      HWND windowHandle = NULL;
# Line 91  LRESULT CALLBACK CallWndProc( int nCode, Line 91  LRESULT CALLBACK CallWndProc( int nCode,
91      LONG dwStyle = GetWindowLong( details->hwnd, GWL_STYLE );      LONG dwStyle = GetWindowLong( details->hwnd, GWL_STYLE );
92      WINDOWPOS *wp = ( WINDOWPOS * ) details->lParam;      WINDOWPOS *wp = ( WINDOWPOS * ) details->lParam;
93      RECT rect;      RECT rect;
94        
95      switch ( details->message ) {      switch ( details->message ) {
96        
97          case WM_WINDOWPOSCHANGED:      case WM_WINDOWPOSCHANGED:
98              if ( dwStyle & WS_CHILD)          if ( dwStyle & WS_CHILD)
99                  break;              break;
100    
101    
102              if ( wp->flags & SWP_SHOWWINDOW ) {          if ( wp->flags & SWP_SHOWWINDOW ) {
103                  // FIXME: Now, just like create!              // FIXME: Now, just like create!
104                  SendDebug("SWP_SHOWWINDOW for %p!", details->hwnd);              SendDebug("SWP_SHOWWINDOW for %p!", details->hwnd);
105                    
106                  snprintf( result, sizeof( result ),              snprintf( result, sizeof( result ),
107                            "CREATE1,0x%p,0x%x\n",                        "CREATE1,0x%p,0x%x\n",
108                            details->hwnd, 0 );                        details->hwnd, 0 );
109                  result[ sizeof( result ) - 1 ] = '\0';              result[ sizeof( result ) - 1 ] = '\0';
110                  WriteToChannel( result );              WriteToChannel( result );
111    
112                  // FIXME: SETSTATE              // FIXME: SETSTATE
113                
114                  if ( !GetWindowRect( details->hwnd, &rect ) ) {              if ( !GetWindowRect( details->hwnd, &rect ) ) {
115                      SendDebug( "GetWindowRect failed!\n" );                  SendDebug( "GetWindowRect failed!\n" );
116                      break;                  break;
117                  }              }
118                  snprintf( result, sizeof( result ),              snprintf( result, sizeof( result ),
119                            "POSITION1,0x%p,%d,%d,%d,%d,0x%x\n",                        "POSITION1,0x%p,%d,%d,%d,%d,0x%x\n",
120                            details->hwnd,                        details->hwnd,
121                            rect.left, rect.top,                        rect.left, rect.top,
122                            rect.right - rect.left,                        rect.right - rect.left,
123                            rect.bottom - rect.top,                        rect.bottom - rect.top,
124                            0 );                        0 );
125                  result[ sizeof( result ) - 1 ] = '\0';              result[ sizeof( result ) - 1 ] = '\0';
126                  WriteToChannel( result );              WriteToChannel( result );
127    
128              }          }
129    
130    
131              if ( wp->flags & SWP_HIDEWINDOW ) {          if ( wp->flags & SWP_HIDEWINDOW ) {
132                  snprintf( result, sizeof( result ),              snprintf( result, sizeof( result ),
133                            "DESTROY1,0x%p,0x%x\n",                        "DESTROY1,0x%p,0x%x\n",
134                            details->hwnd, 0 );                        details->hwnd, 0 );
135                  result[ sizeof( result ) - 1 ] = '\0';              result[ sizeof( result ) - 1 ] = '\0';
136                  WriteToChannel( result );              WriteToChannel( result );
137    
138              }          }
139    
140    
141          if ( !( dwStyle & WS_VISIBLE ) )          if ( !( dwStyle & WS_VISIBLE ) )
142                  break;              break;
143                
144          if ( wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE )          if ( wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE )
145              break;              break;
146                
147          if ( !GetWindowRect( details->hwnd, &rect ) ) {          if ( !GetWindowRect( details->hwnd, &rect ) ) {
148              SendDebug( "GetWindowRect failed!\n" );              SendDebug( "GetWindowRect failed!\n" );
149              break;              break;
150          }          }
151            
152          snprintf( result, sizeof( result ),          snprintf( result, sizeof( result ),
153                    "POSITION1,0x%p,%d,%d,%d,%d,0x%x\n",                    "POSITION1,0x%p,%d,%d,%d,%d,0x%x\n",
154                    details->hwnd,                    details->hwnd,
# Line 158  LRESULT CALLBACK CallWndProc( int nCode, Line 158  LRESULT CALLBACK CallWndProc( int nCode,
158                    0 );                    0 );
159          result[ sizeof( result ) - 1 ] = '\0';          result[ sizeof( result ) - 1 ] = '\0';
160          WriteToChannel( result );          WriteToChannel( result );
161            
162          break;          break;
163            
164            
165          /* Note: WM_WINDOWPOSCHANGING/WM_WINDOWPOSCHANGED are          /* Note: WM_WINDOWPOSCHANGING/WM_WINDOWPOSCHANGED are
166          strange. Sometimes, for example when bringing up the          strange. Sometimes, for example when bringing up the
167          Notepad About dialog, only an WM_WINDOWPOSCHANGING is          Notepad About dialog, only an WM_WINDOWPOSCHANGING is
# Line 170  LRESULT CALLBACK CallWndProc( int nCode, Line 170  LRESULT CALLBACK CallWndProc( int nCode,
170          some reason, when closing the Notepad About dialog, an          some reason, when closing the Notepad About dialog, an
171          WM_WINDOWPOSCHANGING event is sent which looks just like          WM_WINDOWPOSCHANGING event is sent which looks just like
172          the event that was sent when the About dialog was opened...  */          the event that was sent when the About dialog was opened...  */
173          case WM_WINDOWPOSCHANGING:      case WM_WINDOWPOSCHANGING:
174              if ( dwStyle & WS_CHILD)          if ( dwStyle & WS_CHILD)
175                  break;              break;
176    
177              if ( !( dwStyle & WS_VISIBLE ) )          if ( !( dwStyle & WS_VISIBLE ) )
178                  break;              break;
179                
180          if ( !( wp->flags & SWP_NOZORDER ) ) {          if ( !( wp->flags & SWP_NOZORDER ) ) {
181              snprintf( result, sizeof( result ),              snprintf( result, sizeof( result ),
182                        "ZCHANGE1,0x%p,0x%p,0x%x\n",                        "ZCHANGE1,0x%p,0x%p,0x%x\n",
# Line 187  LRESULT CALLBACK CallWndProc( int nCode, Line 187  LRESULT CALLBACK CallWndProc( int nCode,
187              WriteToChannel( result );              WriteToChannel( result );
188          }          }
189          break;          break;
           
         
         
           
         case WM_DESTROY:  
             if ( dwStyle & WS_CHILD)  
                 break;  
190    
191              snprintf( result, sizeof( result ),  
192                        "DESTROY1,0x%p,0x%x\n",  
193                        details->hwnd, 0 );  
194              result[ sizeof( result ) - 1 ] = '\0';      case WM_DESTROY:
195              WriteToChannel( result );          if ( dwStyle & WS_CHILD)
196                        break;
197    
198            snprintf( result, sizeof( result ),
199                      "DESTROY1,0x%p,0x%x\n",
200                      details->hwnd, 0 );
201            result[ sizeof( result ) - 1 ] = '\0';
202            WriteToChannel( result );
203    
204          break;          break;
205    
206          
207          default:      default:
208          break;          break;
209      }      }
210        
211      return CallNextHookEx( hWndProc, nCode, wParam, lParam );      return CallNextHookEx( hWndProc, nCode, wParam, lParam );
212  }  }
213    
# Line 216  LRESULT CALLBACK CbtProc( int nCode, WPA Line 216  LRESULT CALLBACK CbtProc( int nCode, WPA
216      if ( nCode < 0 ) {      if ( nCode < 0 ) {
217          return CallNextHookEx( hCbtProc, nCode, wParam, lParam );          return CallNextHookEx( hCbtProc, nCode, wParam, lParam );
218      }      }
219        
220      char windowTitle[ 150 ] = { ""      char windowTitle[ 150 ] = { ""
221                                };                                };
222      HWND windowHandle = NULL;      HWND windowHandle = NULL;
223      char result[ 255 ] = { ""      char result[ 255 ] = { ""
224                           };                           };
225      switch ( nCode ) {      switch ( nCode ) {
226          case HCBT_MINMAX:      case HCBT_MINMAX:
227            
228          if ( ( LOWORD( lParam ) == SW_SHOWMINIMIZED )          if ( ( LOWORD( lParam ) == SW_SHOWMINIMIZED )
229                  || ( LOWORD( lParam ) == SW_MINIMIZE ) ) {                  || ( LOWORD( lParam ) == SW_MINIMIZE ) ) {
230              MessageBox( 0, "Minimizing windows is not allowed in this version. Sorry!", "SeamlessRDP", MB_OK );              MessageBox( 0, "Minimizing windows is not allowed in this version. Sorry!", "SeamlessRDP", MB_OK );
231              return 1;              return 1;
232          }          }
233            
234          GetWindowText( ( HWND ) wParam, windowTitle, 150 );          GetWindowText( ( HWND ) wParam, windowTitle, 150 );
235            
236          snprintf( result, sizeof( result ),          snprintf( result, sizeof( result ),
237                    "SETSTATE1,0x%p,%s,0x%x,0x%x\n",                    "SETSTATE1,0x%p,%s,0x%x,0x%x\n",
238                    ( HWND ) wParam,                    ( HWND ) wParam,
# Line 242  LRESULT CALLBACK CbtProc( int nCode, WPA Line 242  LRESULT CALLBACK CbtProc( int nCode, WPA
242          result[ sizeof( result ) - 1 ] = '\0';          result[ sizeof( result ) - 1 ] = '\0';
243          WriteToChannel( result );          WriteToChannel( result );
244          break;          break;
245            
246            
247          default:      default:
248          break;          break;
249      }      }
250        
251        
252        
253      return CallNextHookEx( hCbtProc, nCode, wParam, lParam );      return CallNextHookEx( hCbtProc, nCode, wParam, lParam );
254  }  }
255    
# Line 259  LRESULT CALLBACK ShellProc( int nCode, W Line 259  LRESULT CALLBACK ShellProc( int nCode, W
259      if ( nCode < 0 ) {      if ( nCode < 0 ) {
260          return CallNextHookEx( hShellProc, nCode, wParam, lParam );          return CallNextHookEx( hShellProc, nCode, wParam, lParam );
261      }      }
262        
263      char windowTitle[ 150 ] = { ""      char windowTitle[ 150 ] = { ""
264                                };                                };
265      HWND windowHandle = NULL;      HWND windowHandle = NULL;
# Line 272  LRESULT CALLBACK ShellProc( int nCode, W Line 272  LRESULT CALLBACK ShellProc( int nCode, W
272      char strX[ 5 ];      char strX[ 5 ];
273      char strH[ 5 ];      char strH[ 5 ];
274      RECT rect;      RECT rect;
275        
276      switch ( nCode ) {      switch ( nCode ) {
277          case HSHELL_WINDOWCREATED:      case HSHELL_WINDOWCREATED:
278            
279          //get window id          //get window id
280          windowHandle = ( HWND ) wParam;          windowHandle = ( HWND ) wParam;
281          itoa( ( int ) windowHandle, strWindowId, 10 );          itoa( ( int ) windowHandle, strWindowId, 10 );
282            
283          //get coords          //get coords
284          GetWindowRect( windowHandle, &rect );          GetWindowRect( windowHandle, &rect );
285          b = rect.bottom;          b = rect.bottom;
# Line 290  LRESULT CALLBACK ShellProc( int nCode, W Line 290  LRESULT CALLBACK ShellProc( int nCode, W
290          ltoa( t, strY, 10 );          ltoa( t, strY, 10 );
291          ltoa( r - l, strW, 10 );          ltoa( r - l, strW, 10 );
292          ltoa( l, strX, 10 );          ltoa( l, strX, 10 );
293            
294          //get name          //get name
295          GetWindowText( windowHandle, windowTitle, 150 );          GetWindowText( windowHandle, windowTitle, 150 );
296            
297          ////setup return string          ////setup return string
298          strcat( result, "MSG=HSHELL_WINDOWCREATED;OP=0;" );          strcat( result, "MSG=HSHELL_WINDOWCREATED;OP=0;" );
299          strcat( result, "ID=" );          strcat( result, "ID=" );
# Line 316  LRESULT CALLBACK ShellProc( int nCode, W Line 316  LRESULT CALLBACK ShellProc( int nCode, W
316          strcat( result, "." );          strcat( result, "." );
317          WriteToChannel( result );          WriteToChannel( result );
318          break;          break;
319            
320          case HSHELL_WINDOWDESTROYED:      case HSHELL_WINDOWDESTROYED:
321            
322          //get window id          //get window id
323          windowHandle = ( HWND ) wParam;          windowHandle = ( HWND ) wParam;
324          itoa( ( int ) windowHandle, strWindowId, 10 );          itoa( ( int ) windowHandle, strWindowId, 10 );
325            
326          //get coords          //get coords
327          GetWindowRect( windowHandle, &rect );          GetWindowRect( windowHandle, &rect );
328          b = rect.bottom;          b = rect.bottom;
# Line 333  LRESULT CALLBACK ShellProc( int nCode, W Line 333  LRESULT CALLBACK ShellProc( int nCode, W
333          ltoa( t, strY, 10 );          ltoa( t, strY, 10 );
334          ltoa( r - l, strW, 10 );          ltoa( r - l, strW, 10 );
335          ltoa( l, strX, 10 );          ltoa( l, strX, 10 );
336            
337          //get name          //get name
338          GetWindowText( windowHandle, windowTitle, 150 );          GetWindowText( windowHandle, windowTitle, 150 );
339            
340          ////setup return string          ////setup return string
341          strcat( result, "MSG=HSHELL_WINDOWDESTROYED;OP=1;" );          strcat( result, "MSG=HSHELL_WINDOWDESTROYED;OP=1;" );
342          strcat( result, "ID=" );          strcat( result, "ID=" );
# Line 359  LRESULT CALLBACK ShellProc( int nCode, W Line 359  LRESULT CALLBACK ShellProc( int nCode, W
359          strcat( result, "." );          strcat( result, "." );
360          WriteToChannel( result );          WriteToChannel( result );
361          break;          break;
362            
363            
364          default:      default:
365          break;          break;
366      }      }
367        
368        
369      return CallNextHookEx( hShellProc, nCode, wParam, lParam );      return CallNextHookEx( hShellProc, nCode, wParam, lParam );
370  }  }
371    
# Line 374  DLL_EXPORT void SetHooks( void ) Line 374  DLL_EXPORT void SetHooks( void )
374      if ( !hCbtProc ) {      if ( !hCbtProc ) {
375          hCbtProc = SetWindowsHookEx( WH_CBT, ( HOOKPROC ) CbtProc, hInst, ( DWORD ) NULL );          hCbtProc = SetWindowsHookEx( WH_CBT, ( HOOKPROC ) CbtProc, hInst, ( DWORD ) NULL );
376      }      }
377        
378  #if 0  #if 0
379      if ( !hShellProc ) {      if ( !hShellProc ) {
380          hShellProc = SetWindowsHookEx( WH_SHELL, ( HOOKPROC ) ShellProc, hInst, ( DWORD ) NULL );          hShellProc = SetWindowsHookEx( WH_SHELL, ( HOOKPROC ) ShellProc, hInst, ( DWORD ) NULL );
381      }      }
382  #endif  #endif
383        
384      if ( !hWndProc ) {      if ( !hWndProc ) {
385          hWndProc = SetWindowsHookEx( WH_CALLWNDPROC, ( HOOKPROC ) CallWndProc, hInst, ( DWORD ) NULL );          hWndProc = SetWindowsHookEx( WH_CALLWNDPROC, ( HOOKPROC ) CallWndProc, hInst, ( DWORD ) NULL );
386      }      }
# Line 391  DLL_EXPORT void RemoveHooks( void ) Line 391  DLL_EXPORT void RemoveHooks( void )
391      if ( hCbtProc ) {      if ( hCbtProc ) {
392          UnhookWindowsHookEx( hCbtProc );          UnhookWindowsHookEx( hCbtProc );
393      }      }
394        
395      if ( hShellProc ) {      if ( hShellProc ) {
396          UnhookWindowsHookEx( hShellProc );          UnhookWindowsHookEx( hShellProc );
397      }      }
398        
399      if ( hWndProc ) {      if ( hWndProc ) {
400          UnhookWindowsHookEx( hWndProc );          UnhookWindowsHookEx( hWndProc );
401      }      }
# Line 409  DLL_EXPORT int GetInstanceCount() Line 409  DLL_EXPORT int GetInstanceCount()
409  int OpenVirtualChannel()  int OpenVirtualChannel()
410  {  {
411      m_vcHandle = WTSVirtualChannelOpen( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, CHANNELNAME );      m_vcHandle = WTSVirtualChannelOpen( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, CHANNELNAME );
412        
413      if ( m_vcHandle == NULL ) {      if ( m_vcHandle == NULL ) {
414          return 0;          return 0;
415      } else {      } else {
# Line 420  int OpenVirtualChannel() Line 420  int OpenVirtualChannel()
420  int CloseVirtualChannel()  int CloseVirtualChannel()
421  {  {
422      BOOL result = WTSVirtualChannelClose( m_vcHandle );      BOOL result = WTSVirtualChannelClose( m_vcHandle );
423        
424      m_vcHandle = NULL;      m_vcHandle = NULL;
425        
426      if ( result ) {      if ( result ) {
427          return 1;          return 1;
428      } else {      } else {
# Line 443  int WriteToChannel( PCHAR buffer ) Line 443  int WriteToChannel( PCHAR buffer )
443  {  {
444      PULONG bytesRead = 0;      PULONG bytesRead = 0;
445      PULONG pBytesWritten = 0;      PULONG pBytesWritten = 0;
446        
447      if ( !ChannelIsOpen() )      if ( !ChannelIsOpen() )
448          return 1;          return 1;
449            
450      BOOL result = WTSVirtualChannelWrite( m_vcHandle, buffer, ( ULONG ) strlen( buffer ), pBytesWritten );      BOOL result = WTSVirtualChannelWrite( m_vcHandle, buffer, ( ULONG ) strlen( buffer ), pBytesWritten );
451        
452      if ( result ) {      if ( result ) {
453          return 1;          return 1;
454      } else {      } else {

Legend:
Removed from v.1064  
changed lines
  Added in v.1065

  ViewVC Help
Powered by ViewVC 1.1.26