/[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 918 by astrand, Thu Jun 30 08:53:04 2005 UTC revision 996 by astrand, Mon Aug 29 09:11:50 2005 UTC
# Line 1  Line 1 
1  //*********************************************************************************  //
2  //  // Copyright (C) 2004-2005 Martin Wickett
3  //Title: Terminal Services Window Clipper  //
4  //  
5  //Author: Martin Wickett  #include "hookdll.h"
6  //  #include <windows.h>
7  //Date: 2004  #include <winuser.h>
8  //  #include <stdio.h>
9  //*********************************************************************************  
10    #include "wtsapi32.h"
11  #include "hookdll.h"  #include "Cchannel.h"
12  #include <windows.h>  
13  #include <winuser.h>  #define DLL_EXPORT extern "C" __declspec(dllexport)
14    
15  #include "wtsapi32.h"  // Shared DATA
16  #include "Cchannel.h"  #pragma data_seg ( "SHAREDDATA" )
17    
18  #define DLL_EXPORT extern "C" __declspec(dllexport)  // this is the total number of processes this dll is currently attached to
19    int iInstanceCount = 0;
20  // Shared DATA  HWND hWnd = 0;
21  #pragma data_seg ( "SHAREDDATA" )  
22    #pragma data_seg ()
23          // this is the total number of processes this dll is currently attached to  
24          int                             iInstanceCount          = 0;  #pragma comment(linker, "/section:SHAREDDATA,rws")
25          HWND                    hWnd                            = 0;  
26    bool bHooked = false;
27  #pragma data_seg ()  bool bHooked2 = false;
28    bool bHooked3 = false;
29  #pragma comment(linker, "/section:SHAREDDATA,rws")  HHOOK hhook = 0; //cbt
30    HHOOK hhook2 = 0; //shell
31  bool                    bHooked        = false;  HHOOK hhook3 = 0; //wnd proc
32  bool                    bHooked2       = false;  HINSTANCE hInst = 0;
33  bool                    bHooked3       = false;  HANDLE m_vcHandle = 0;
34  HHOOK                   hhook          = 0;//cbt  
35  HHOOK                   hhook2         = 0;//shell  BOOL APIENTRY DllMain( HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved )
36  HHOOK                   hhook3             = 0;//wnd proc  {
37  HINSTANCE               hInst          = 0;      switch ( ul_reason_for_call ) {
38  HANDLE                  m_vcHandle         = 0;          case DLL_PROCESS_ATTACH: {
39                // remember our instance handle
40  BOOL APIENTRY DllMain( HINSTANCE hinstDLL, DWORD  ul_reason_for_call, LPVOID lpReserved )              hInst = hinstDLL;
41  {              ++iInstanceCount;
42      switch (ul_reason_for_call)              OpenVirtualChannel();
43          {              break;
44                  case DLL_PROCESS_ATTACH:          }
45                          {          
46                                  // remember our instance handle          case DLL_THREAD_ATTACH:
47                                  hInst = hinstDLL;          break;
48                                  ++iInstanceCount;          case DLL_THREAD_DETACH:
49                                  OpenVirtualChannel();          break;
50                                   break;          case DLL_PROCESS_DETACH: {
51                          }              --iInstanceCount;
52                CloseVirtualChannel();
53                  case DLL_THREAD_ATTACH:          }
54                                  break;          break;
55                  case DLL_THREAD_DETACH:      }
56                          break;      
57                  case DLL_PROCESS_DETACH:      return TRUE;
58                          {  }
59                                  --iInstanceCount;  
60                                  CloseVirtualChannel();  LRESULT CALLBACK CallWndProc( int nCode, WPARAM wParam, LPARAM lParam )
61                          }  {
62                          break;      if ( nCode < 0 ) {
63      }          return CallNextHookEx( hhook3, nCode, wParam, lParam );
64        }
65      return TRUE;      
66  }      PCHAR buffer = NULL;
67        char windowTitle[ 150 ] = { ""
68  LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam )                                };
69  {      HWND windowHandle = NULL;
70          if (nCode<0)      HWND windowHandle2 = NULL;
71          {      char result[ 255 ] = { ""
72                  return CallNextHookEx(hhook3, nCode, wParam, lParam);                           };
73          }      char strWindowId[ 25 ];
74        char type[ 25 ];
75          PCHAR buffer          = NULL;      
76          char windowTitle[150] = {""};      LONG b, t, l, r;
77          HWND windowHandle     = NULL;      char strX[ 5 ];
78          char result[255]      = {""};      char strY[ 5 ];
79          char strWindowId[25];      char strW[ 5 ];
80          char type[25];      char strH[ 5 ];
81        RECT rect;
82          LONG b,t,l,r;      
83      char strB[5];      CWPSTRUCT *details = ( CWPSTRUCT * ) lParam;
84          char strT[5];      CREATESTRUCT *cs = ( CREATESTRUCT * ) details->lParam;
85          char strL[5];      LONG dwStyle = GetWindowLong( details->hwnd, GWL_STYLE );
86      char strR[5];      
87          RECT rect;      switch ( details->message ) {
88            case WM_SIZING:
89          CWPSTRUCT* details = (CWPSTRUCT*) lParam;          windowHandle = details->hwnd;
90            //get win name
91          switch( details->message )          GetWindowText( windowHandle, windowTitle, 150 );
92          {                
93                  case WM_SIZING:          //get an id for it
94                  case WM_MOVING:          itoa( ( int ) windowHandle, strWindowId, 10 );
95                                    
96                          windowHandle = details->hwnd;          //get coords
97                          //get win name          GetWindowRect( windowHandle, &rect );
98                          GetWindowText(windowHandle,windowTitle,150);          b = rect.bottom;
99                                    t = rect.top;
100                          //get an id for it          l = rect.left;
101                          itoa((int)windowHandle, strWindowId, 10);          r = rect.right;
102                                    ltoa( b - t, strH, 10 );
103                          //get coords          ltoa( t, strY, 10 );
104                          GetWindowRect(windowHandle,&rect);          ltoa( r - l, strW, 10 );
105                          b = rect.bottom;          ltoa( l, strX, 10 );
106                          t = rect.top;          
107                          l = rect.left;          ////setup return string
108                          r = rect.right;          strcat( result, "MSG=CALLWNDPROC_WM_SIZING;OP=6;" );
109                          ltoa(b,strB,10);          strcat( result, "ID=" );
110                          ltoa(t,strT,10);          strcat( result, strWindowId );
111                          ltoa(r,strR,10);          strcat( result, ";" );
112                          ltoa(l,strL,10);          strcat( result, "TITLE=" );
113            strcat( result, windowTitle );
114                          ////setup return string          strcat( result, ";" );
115                          strcat(result, "MSG=CALLWNDPROC_WM_MOVING;");          strcat( result, "X=" );
116                          strcat( result, "ID=" );          strcat( result, strX );
117                          strcat( result, strWindowId );          strcat( result, ";" );
118                          strcat( result, ";" );          strcat( result, "Y=" );
119                          strcat( result, "TITLE=" );          strcat( result, strY );
120                          strcat( result, windowTitle);          strcat( result, ";" );
121                          strcat( result, ";" );          strcat( result, "H=" );
122                          strcat( result, "POS=" );          strcat( result, strH );
123                          strcat( result, strL);          strcat( result, ";" );
124                          strcat( result, "~");          strcat( result, "W=" );
125                          strcat( result, strT);          strcat( result, strW );
126                          strcat( result, "~");          strcat( result, "." );
127                          strcat( result, strR);          
128                          strcat( result, "~");          buffer = result;
129                          strcat( result, strB);          
130                          strcat( result, ";" );          break;
131                                    case WM_MOVING:
132                          buffer = result;          
133                            windowHandle = details->hwnd;
134                  break;          //get win name
135            GetWindowText( windowHandle, windowTitle, 150 );
136                  default:          
137                          break;          //get an id for it
138          }          itoa( ( int ) windowHandle, strWindowId, 10 );
139            
140          if (ChannelIsOpen())          //get coords
141          {          GetWindowRect( windowHandle, &rect );
142                  if ( buffer != NULL)          b = rect.bottom;
143                  {          t = rect.top;
144                          WriteToChannel(buffer);          l = rect.left;
145                  }          r = rect.right;
146          }          ltoa( b - t, strH, 10 );
147            ltoa( t, strY, 10 );
148          return CallNextHookEx(hhook3, nCode, wParam, lParam);          ltoa( r - l, strW, 10 );
149  }          ltoa( l, strX, 10 );
150            
151  LRESULT CALLBACK CbtProc(int nCode, WPARAM wParam, LPARAM lParam )          ////setup return string
152  {          strcat( result, "MSG=CALLWNDPROC_WM_MOVING;OP=2;" );
153          if (nCode<0)          strcat( result, "ID=" );
154          {          strcat( result, strWindowId );
155                  return CallNextHookEx(hhook, nCode, wParam, lParam);          strcat( result, ";" );
156          }          strcat( result, "TITLE=" );
157            strcat( result, windowTitle );
158            strcat( result, ";" );
159                  PCHAR buffer          = NULL;          strcat( result, "X=" );
160            strcat( result, strX );
161            strcat( result, ";" );
162                  char windowTitle[150] = {""};          strcat( result, "Y=" );
163                  HWND windowHandle     = NULL;          strcat( result, strY );
164                  char result[255]      = {""};          strcat( result, ";" );
165                  char strWindowId[25];          strcat( result, "H=" );
166                  char type[25];          strcat( result, strH );
167            strcat( result, ";" );
168            strcat( result, "W=" );
169                  LONG b,t,l,r;          strcat( result, strW );
170          char strB[5];          strcat( result, "." );
171                  char strT[5];          
172              char strL[5];          buffer = result;
173          char strR[5];          
174                  RECT rect;          break;
175            
176                  int i = 0;//tmp          case WM_WINDOWPOSCHANGED:
177                    
178                  switch( nCode )          windowHandle = details->hwnd;
179                  {          //windowHandle2 = details->hwndInsertAfter;
180                          case HCBT_MINMAX:          //get win name
181            GetWindowText( windowHandle, windowTitle, 150 );
182                                  windowHandle = (HWND)wParam;          
183                                  //get win name          //get an id for it
184                                  GetWindowText(windowHandle,windowTitle,150);          itoa( ( int ) windowHandle, strWindowId, 10 );
185                                            
186                                  //get an id for it          //get coords
187                                  itoa((int)windowHandle, strWindowId, 10);          GetWindowRect( windowHandle, &rect );
188                                            b = rect.bottom;
189                                  //get operation type(min,max). if max, do not clip at all,if min use window's previous coords          t = rect.top;
190                                  //codes are:          l = rect.left;
191            r = rect.right;
192                                  // SW_HIDE= 0  SW_SHOWNORMAL=1  SW_NORMAL=1  SW_SHOWMINIMIZED=2  SW_SHOWMAXIMIZED=3  SW_MAXIMIZE=3          ltoa( b - t, strH, 10 );
193                                  // SW_SHOWNOACTIVATE=4  SW_SHOW=5  SW_MINIMIZE=6  SW_SHOWMINNOACTIVE=7  SW_SHOWNA=8  SW_RESTORE=9            ltoa( t, strY, 10 );
194                                  // SW_SHOWDEFAULT=10  SW_FORCEMINIMIZE=11  SW_MAX=11              ltoa( r - l, strW, 10 );
195            ltoa( l, strX, 10 );
196                                  itoa((int)lParam, type, 10);          
197            ////setup return string
198                                  //get coords          strcat( result, "MSG=CALLWNDPROC_WM_WINDOWPOSCHANGED;OP=8;" );
199                                  GetWindowRect(windowHandle,&rect);          strcat( result, "ID=" );
200                                  b = rect.bottom;          strcat( result, strWindowId );
201                                  t = rect.top;          strcat( result, ";" );
202                                  l = rect.left;          strcat( result, "TITLE=" );
203                                  r = rect.right;          strcat( result, windowTitle );
204                                  ltoa(b,strB,10);          strcat( result, ";" );
205                                  ltoa(t,strT,10);          strcat( result, "X=" );
206                                  ltoa(r,strR,10);          strcat( result, strX );
207                                  ltoa(l,strL,10);          strcat( result, ";" );
208            strcat( result, "Y=" );
209                                  //get name          strcat( result, strY );
210                                  GetWindowText(windowHandle,windowTitle,150);          strcat( result, ";" );
211            strcat( result, "H=" );
212                                  ////setup return string          strcat( result, strH );
213                                  strcat(result, "MSG=HCBT_MINMAX;");          strcat( result, ";" );
214                                  strcat( result, "ID=" );          strcat( result, "W=" );
215                                  strcat( result, strWindowId );          strcat( result, strW );
216                                  strcat( result, ";" );          strcat( result, "." );
217                                  strcat( result, "TITLE=" );          
218                                  strcat( result, windowTitle);          buffer = result;
219                                  strcat( result, ";" );          
220                                  strcat( result, "POS=" );          break;
221                                  strcat( result, strL);          
222                                  strcat( result, "~");          
223                                  strcat( result, strT);          case WM_CREATE:
224                                  strcat( result, "~");          if ( cs->style & WS_DLGFRAME ) {
225                                  strcat( result, strR);              sprintf( result, "DEBUG:WM_CREATE:%dx%d at %d,%d, title=%s, menu=%p, window=%p, WS_BORDER=%d, WS_DLGFRAME=%d, WS_POPUP=%d",
226                                  strcat( result, "~");                       cs->cx, cs->cy, cs->x, cs->y, cs->lpszName, cs->hMenu, details->hwnd,
227                                  strcat( result, strB);                       cs->style & WS_BORDER, cs->style & WS_DLGFRAME,
228                                  strcat( result, ";" );                       cs->style & WS_POPUP );
229                                  strcat( result, "TYPE=" );              buffer = result;
230                                  strcat( result, type );          }
231                                  strcat( result, ";" );          
232            break;
233                                  //-------------------------------------------------------------------------------------------------          
234                                  // code to prevent minimising windows (can be removed once minimise has been implemented)          
235                                  i = (int)lParam;          case WM_DESTROY:
236                                  //if (i==0 || i==2 || i==6 || i==7 || i==8 || i==11)          if ( dwStyle & WS_DLGFRAME ) {
237                                  if ( i==2 || i==6 )              sprintf( result, "WM_DESTROY:%p", details->hwnd );
238                                  {              buffer = result;
239                                          MessageBox(0,"Minimising windows is not allowed in this version. Sorry!","TS Window Clipper", MB_OK);          }
240                                          return 1;          
241                                  }          break;
242                                  //-----------------------------------------------------------------------------------------          
243            default:
244                                  //-------------------------------------------------------------------------------------------------          break;
245                                  // code to prevent maximising windows (can be removed once maximise has been implemented)      }
246                                  i = (int)lParam;      
247                                  //if (i==3 || i==9 || i==11)      if ( ChannelIsOpen() ) {
248                                  if (i==3 || i==11)          if ( buffer != NULL ) {
249                                  {              WriteToChannel( buffer );
250                                          MessageBox(0,"Maximising windows is not allowed in this version. Sorry!","TS Window Clipper", MB_OK);          }
251                                          return 1;      }
252                                  }      
253                                  //-----------------------------------------------------------------------------------------      return CallNextHookEx( hhook3, nCode, wParam, lParam );
254    }
255                                  buffer = result;  
256    LRESULT CALLBACK CbtProc( int nCode, WPARAM wParam, LPARAM lParam )
257                                  break;  {
258        if ( nCode < 0 ) {
259                          case HCBT_MOVESIZE:          return CallNextHookEx( hhook, nCode, wParam, lParam );
260                }
261                                  windowHandle = (HWND)wParam;      
262                                  //get win name      
263                                  GetWindowText(windowHandle,windowTitle,150);      PCHAR buffer = NULL;
264                                        
265                                  //get an id for it      
266                                  itoa((int)windowHandle, strWindowId, 10);      char windowTitle[ 150 ] = { ""
267                                  };
268                                  //get coords      HWND windowHandle = NULL;
269                                  GetWindowRect(windowHandle,&rect);      char result[ 255 ] = { ""
270                                  b = rect.bottom;                           };
271                                  t = rect.top;      char strWindowId[ 25 ];
272                                  l = rect.left;      char type[ 25 ];
273                                  r = rect.right;      
274                                  ltoa(b,strB,10);      
275                                  ltoa(t,strT,10);      LONG b, t, l, r;
276                                  ltoa(r,strR,10);      char strW[ 5 ];
277                                  ltoa(l,strL,10);      char strY[ 5 ];
278        char strX[ 5 ];
279                                  //get name      char strH[ 5 ];
280                                  GetWindowText(windowHandle,windowTitle,150);      RECT rect;
281        
282                                  ////setup return string      int i = 0; //tmp
283                                  strcat(result, "MSG=HCBT_MOVESIZE;");      
284                                  strcat( result, "ID=" );      switch ( nCode ) {
285                                  strcat( result, strWindowId );          case HCBT_MINMAX:
286                                  strcat( result, ";" );          
287                                  strcat( result, "TITLE=" );          windowHandle = ( HWND ) wParam;
288                                  strcat( result, windowTitle);          //get win name
289                                  strcat( result, ";" );          GetWindowText( windowHandle, windowTitle, 150 );
290                                  strcat( result, "POS=" );          
291                                  strcat( result, strL);          //get an id for it
292                                  strcat( result, "~");          itoa( ( int ) windowHandle, strWindowId, 10 );
293                                  strcat( result, strT);          
294                                  strcat( result, "~");          //get operation type(min,max). if max, do not clip at all,if min use window's previous coords
295                                  strcat( result, strR);          //codes are:
296                                  strcat( result, "~");          
297                                  strcat( result, strB);          // SW_HIDE= 0  SW_SHOWNORMAL=1  SW_NORMAL=1  SW_SHOWMINIMIZED=2  SW_SHOWMAXIMIZED=3  SW_MAXIMIZE=3
298                                  strcat( result, ";" );          // SW_SHOWNOACTIVATE=4  SW_SHOW=5  SW_MINIMIZE=6  SW_SHOWMINNOACTIVE=7  SW_SHOWNA=8  SW_RESTORE=9
299            // SW_SHOWDEFAULT=10  SW_FORCEMINIMIZE=11  SW_MAX=11
300                                  buffer = result;          
301            itoa( ( int ) lParam, type, 10 );
302                                  break;          
303                          case HCBT_SETFOCUS:          //get coords
304                                  //buffer = "HCBT_SETFOCUS";          GetWindowRect( windowHandle, &rect );
305                                  //not needed yet          b = rect.bottom;
306                          break;          t = rect.top;
307                          default:          l = rect.left;
308                                  break;          r = rect.right;
309                  }          ltoa( b - t, strW, 10 );
310            ltoa( t, strY, 10 );
311          if (ChannelIsOpen())          ltoa( r - l, strH, 10 );
312          {          ltoa( l, strX, 10 );
313                  if ( buffer != NULL)          
314                  {          //get name
315                          WriteToChannel(buffer);          GetWindowText( windowHandle, windowTitle, 150 );
316                  }          
317          }          ////setup return string
318            strcat( result, "MSG=HCBT_MINMAX;OP=4;" );
319          return CallNextHookEx(hhook, nCode, wParam, lParam);          
320  }          // SW_SHOWNOACTIVATE=4  SW_SHOW=5  SW_MINIMIZE=6  SW_SHOWMINNOACTIVE=7  SW_SHOWNA=8  SW_RESTORE=9
321            // SW_SHOWDEFAULT=10  SW_FORCEMINIMIZE=11  SW_MAX=11
322            strcat( result, "ID=" );
323  LRESULT CALLBACK ShellProc(int nCode, WPARAM wParam, LPARAM lParam )          strcat( result, strWindowId );
324  {          strcat( result, ";" );
325          if (nCode<0)          strcat( result, "TITLE=" );
326          {          strcat( result, windowTitle );
327                  return CallNextHookEx(hhook, nCode, wParam, lParam);          strcat( result, ";" );
328          }          strcat( result, "X=" );
329            strcat( result, strX );
330          if (ChannelIsOpen())          strcat( result, ";" );
331          {          strcat( result, "Y=" );
332                  PCHAR buffer          = NULL;          strcat( result, strY );
333            strcat( result, ";" );
334                  char windowTitle[150] = {""};          strcat( result, "H=" );
335                  HWND windowHandle     = NULL;          strcat( result, strH );
336                  char result[255]      = {""};          strcat( result, ";" );
337                  char strWindowId[25];          strcat( result, "W=" );
338                  LONG b,t,l,r;          strcat( result, strW );
339          char strB[5];          strcat( result, ";" );
340                  char strT[5];          strcat( result, "TYPE=" );
341              char strL[5];          strcat( result, type );
342          char strR[5];          strcat( result, "." );
343                  RECT rect;          
344            //-------------------------------------------------------------------------------------------------
345                  switch( nCode )          // code to prevent minimising windows (can be removed once minimise has been implemented)
346                          {          //i = (int)lParam;
347                                  case HSHELL_WINDOWCREATED:          //if (i==0 || i==2 || i==6 || i==7 || i==8 || i==11)
348                                                    //if ( i==2 || i==6 )
349                                          //get window id          //{
350                                          windowHandle = (HWND) wParam;          // MessageBox(0,"Minimising windows is not allowed in this version. Sorry!","TS Window Clipper", MB_OK);
351                                          itoa((int)windowHandle, strWindowId, 10);          // return 1;
352            //}
353                                          //get coords          //-----------------------------------------------------------------------------------------
354                                          GetWindowRect(windowHandle,&rect);          
355                                          b = rect.bottom;          //-------------------------------------------------------------------------------------------------
356                                          t = rect.top;          // code to prevent maximising windows (can be removed once maximise has been implemented)
357                                          l = rect.left;          //i = (int)lParam;
358                                          r = rect.right;          //if (i==3 || i==9 || i==11)
359                                          ltoa(b,strB,10);          //if (i==3 || i==11)
360                                          ltoa(t,strT,10);          //{
361                                          ltoa(r,strR,10);          // MessageBox(0,"Maximising windows is not allowed in this version. Sorry!","TS Window Clipper", MB_OK);
362                                          ltoa(l,strL,10);          // return 1;
363            //}
364                                          //get name          //-----------------------------------------------------------------------------------------
365                                          GetWindowText(windowHandle,windowTitle,150);          
366            buffer = result;
367                                          ////setup return string          
368                                          strcat(result, "MSG=HSHELL_WINDOWCREATED;");          break;
369                                          strcat( result, "ID=" );          
370                                          strcat( result, strWindowId );          case HCBT_MOVESIZE:
371                                          strcat( result, ";" );          
372                                          strcat( result, "TITLE=" );          windowHandle = ( HWND ) wParam;
373                                          strcat( result, windowTitle);          //get win name
374                                          strcat( result, ";" );          GetWindowText( windowHandle, windowTitle, 150 );
375                                          strcat( result, "POS=" );          
376                                          strcat( result, strL);          //get an id for it
377                                          strcat( result, "~");          itoa( ( int ) windowHandle, strWindowId, 10 );
378                                          strcat( result, strT);          
379                                          strcat( result, "~");          //get coords
380                                          strcat( result, strR);          GetWindowRect( windowHandle, &rect );
381                                          strcat( result, "~");          b = rect.bottom;
382                                          strcat( result, strB);          t = rect.top;
383                                          strcat( result, ";" );          l = rect.left;
384            r = rect.right;
385                                          buffer = result;          ltoa( b - t, strH, 10 );
386            ltoa( t, strY, 10 );
387                                          break;          ltoa( r - l, strW, 10 );
388            ltoa( l, strX, 10 );
389                                  case HSHELL_WINDOWDESTROYED:          
390                                            //get name
391                                          //get window id          GetWindowText( windowHandle, windowTitle, 150 );
392                                          windowHandle = (HWND) wParam;          
393                                          itoa((int)windowHandle, strWindowId, 10);          ////setup return string
394                                                    strcat( result, "MSG=HCBT_MOVESIZE;OP=5;" );
395                                          //get name          strcat( result, "ID=" );
396                                          GetWindowText(windowHandle,windowTitle,150);          strcat( result, strWindowId );
397            strcat( result, ";" );
398                                          ////setup return string          strcat( result, "TITLE=" );
399                                          strcat(result, "MSG=HSHELL_WINDOWDESTROYED;");          strcat( result, windowTitle );
400                                          strcat( result, "ID=" );          strcat( result, ";" );
401                                          strcat( result, strWindowId );          strcat( result, "X=" );
402                                          strcat( result, ";" );          strcat( result, strX );
403                                          strcat( result, "TITLE=" );          strcat( result, ";" );
404                                          strcat( result, windowTitle);          strcat( result, "Y=" );
405                                          strcat( result, ";" );          strcat( result, strY );
406            strcat( result, ";" );
407                                          buffer = result;          strcat( result, "H=" );
408            strcat( result, strH );
409                                          break;          strcat( result, ";" );
410                                  default:          strcat( result, "W=" );
411                                          break;          strcat( result, strW );
412                          }          strcat( result, "." );
413            
414                  if ( buffer != NULL)          buffer = result;
415                  {          
416                          WriteToChannel(buffer);          break;
417                  }          case HCBT_SETFOCUS:
418          }          //buffer = "HCBT_SETFOCUS";
419                    //not needed yet
420          return CallNextHookEx(hhook, nCode, wParam, lParam);          break;
421  }          default:
422            break;
423  DLL_EXPORT void SetCbtHook(void)      }
424  {      
425          if (!bHooked)      if ( ChannelIsOpen() ) {
426          {          if ( buffer != NULL ) {
427                  hhook           = SetWindowsHookEx(WH_CBT, (HOOKPROC)CbtProc, hInst, (DWORD)NULL);              WriteToChannel( buffer );
428                  bHooked         = true;          }
429          }      }
430        
431          if (!bHooked2)      return CallNextHookEx( hhook, nCode, wParam, lParam );
432          {  }
433                  hhook2          = SetWindowsHookEx(WH_SHELL, (HOOKPROC)ShellProc, hInst, (DWORD)NULL);    
434                  bHooked2                = true;  
435          }  LRESULT CALLBACK ShellProc( int nCode, WPARAM wParam, LPARAM lParam )
436    {
437          if (!bHooked3)      if ( nCode < 0 ) {
438          {          return CallNextHookEx( hhook, nCode, wParam, lParam );
439                  hhook3          = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)CallWndProc, hInst, (DWORD)NULL);        }
440                  bHooked3                = true;      
441          }      if ( ChannelIsOpen() ) {
442  }          PCHAR buffer = NULL;
443            
444  DLL_EXPORT      void RemoveCbtHook(void)          char windowTitle[ 150 ] = { ""
445  {                                    };
446          if(bHooked)          HWND windowHandle = NULL;
447          {          char result[ 255 ] = { ""
448                  UnhookWindowsHookEx(hhook);                                   };
449                  bHooked = false;          char strWindowId[ 25 ];
450          }          LONG b, t, l, r;
451            char strW[ 5 ];
452          if(bHooked2)          char strY[ 5 ];
453          {          char strX[ 5 ];
454                  UnhookWindowsHookEx(hhook2);              char strH[ 5 ];
455                  bHooked2 = false;          RECT rect;
456          }          
457            switch ( nCode ) {
458          if(bHooked3)              case HSHELL_WINDOWCREATED:
459          {              
460                  UnhookWindowsHookEx(hhook3);                  //get window id
461                  bHooked3 = false;              windowHandle = ( HWND ) wParam;
462          }              itoa( ( int ) windowHandle, strWindowId, 10 );
463  }              
464                //get coords
465  DLL_EXPORT int GetInstanceCount()              GetWindowRect( windowHandle, &rect );
466  {              b = rect.bottom;
467          return iInstanceCount;              t = rect.top;
468  }              l = rect.left;
469                r = rect.right;
470  int OpenVirtualChannel()              ltoa( b - t, strH, 10 );
471  {              ltoa( t, strY, 10 );
472                  m_vcHandle = WTSVirtualChannelOpen(WTS_CURRENT_SERVER_HANDLE,WTS_CURRENT_SESSION,CHANNELNAME);              ltoa( r - l, strW, 10 );
473                                ltoa( l, strX, 10 );
474                  if (m_vcHandle==NULL)              
475                  {              //get name
476                          return 0;              GetWindowText( windowHandle, windowTitle, 150 );
477                  }              
478                  else              ////setup return string
479                  {              strcat( result, "MSG=HSHELL_WINDOWCREATED;OP=0;" );
480                          return 1;              strcat( result, "ID=" );
481                  }              strcat( result, strWindowId );
482  }              strcat( result, ";" );
483                strcat( result, "TITLE=" );
484  int CloseVirtualChannel()              strcat( result, windowTitle );
485  {              strcat( result, ";" );
486          BOOL result = WTSVirtualChannelClose(m_vcHandle);              strcat( result, "X=" );
487                  strcat( result, strX );
488           m_vcHandle = NULL;              strcat( result, ";" );
489                        strcat( result, "Y=" );
490          if (result)              strcat( result, strY );
491          {              strcat( result, ";" );
492                  return 1;              strcat( result, "H=" );
493          }              strcat( result, strH );
494          else              strcat( result, ";" );
495          {              strcat( result, "W=" );
496                  return 0;              strcat( result, strW );
497          }              strcat( result, "." );
498  }              
499                buffer = result;
500  int ChannelIsOpen()              
501  {              break;
502          if (m_vcHandle==NULL)              
503          {              case HSHELL_WINDOWDESTROYED:
504                  return 0;              
505          }              //get window id
506          else              windowHandle = ( HWND ) wParam;
507          {              itoa( ( int ) windowHandle, strWindowId, 10 );
508                  return 1;              
509          }              //get coords
510  }              GetWindowRect( windowHandle, &rect );
511                b = rect.bottom;
512  int WriteToChannel(PCHAR buffer)              t = rect.top;
513  {              l = rect.left;
514      PULONG bytesRead      = 0;              r = rect.right;
515          PULONG pBytesWritten  = 0;              ltoa( b - t, strH, 10 );
516                ltoa( t, strY, 10 );
517          BOOL result = WTSVirtualChannelWrite(m_vcHandle,buffer,(ULONG) strlen(buffer),pBytesWritten);              ltoa( r - l, strW, 10 );
518                        ltoa( l, strX, 10 );
519          if (result)              
520          {              //get name
521                  return 1;              GetWindowText( windowHandle, windowTitle, 150 );
522          }              
523          else              ////setup return string
524          {              strcat( result, "MSG=HSHELL_WINDOWDESTROYED;OP=1;" );
525                  return 0;              strcat( result, "ID=" );
526          }              strcat( result, strWindowId );
527  }              strcat( result, ";" );
528                strcat( result, "TITLE=" );
529                strcat( result, windowTitle );
530                strcat( result, ";" );
531                strcat( result, "X=" );
532                strcat( result, strX );
533                strcat( result, ";" );
534                strcat( result, "Y=" );
535                strcat( result, strY );
536                strcat( result, ";" );
537                strcat( result, "H=" );
538                strcat( result, strH );
539                strcat( result, ";" );
540                strcat( result, "W=" );
541                strcat( result, strW );
542                strcat( result, "." );
543                
544                buffer = result;
545                
546                break;
547                default:
548                break;
549            }
550            
551            if ( buffer != NULL ) {
552                WriteToChannel( buffer );
553            }
554        }
555        
556        return CallNextHookEx( hhook, nCode, wParam, lParam );
557    }
558    
559    DLL_EXPORT void SetCbtHook( void )
560    {
561        if ( !bHooked ) {
562            hhook = SetWindowsHookEx( WH_CBT, ( HOOKPROC ) CbtProc, hInst, ( DWORD ) NULL );
563            bHooked = true;
564        }
565        
566        if ( !bHooked2 ) {
567            hhook2 = SetWindowsHookEx( WH_SHELL, ( HOOKPROC ) ShellProc, hInst, ( DWORD ) NULL );
568            bHooked2 = true;
569        }
570        
571        if ( !bHooked3 ) {
572            hhook3 = SetWindowsHookEx( WH_CALLWNDPROC, ( HOOKPROC ) CallWndProc, hInst, ( DWORD ) NULL );
573            bHooked3 = true;
574        }
575    }
576    
577    DLL_EXPORT void RemoveCbtHook( void )
578    {
579        if ( bHooked ) {
580            UnhookWindowsHookEx( hhook );
581            bHooked = false;
582        }
583        
584        if ( bHooked2 ) {
585            UnhookWindowsHookEx( hhook2 );
586            bHooked2 = false;
587        }
588        
589        if ( bHooked3 ) {
590            UnhookWindowsHookEx( hhook3 );
591            bHooked3 = false;
592        }
593    }
594    
595    DLL_EXPORT int GetInstanceCount()
596    {
597        return iInstanceCount;
598    }
599    
600    int OpenVirtualChannel()
601    {
602        m_vcHandle = WTSVirtualChannelOpen( WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, CHANNELNAME );
603        
604        if ( m_vcHandle == NULL ) {
605            return 0;
606        } else {
607            return 1;
608        }
609    }
610    
611    int CloseVirtualChannel()
612    {
613        BOOL result = WTSVirtualChannelClose( m_vcHandle );
614        
615        m_vcHandle = NULL;
616        
617        if ( result ) {
618            return 1;
619        } else {
620            return 0;
621        }
622    }
623    
624    int ChannelIsOpen()
625    {
626        if ( m_vcHandle == NULL ) {
627            return 0;
628        } else {
629            return 1;
630        }
631    }
632    
633    int WriteToChannel( PCHAR buffer )
634    {
635        PULONG bytesRead = 0;
636        PULONG pBytesWritten = 0;
637        
638        BOOL result = WTSVirtualChannelWrite( m_vcHandle, buffer, ( ULONG ) strlen( buffer ), pBytesWritten );
639        
640        if ( result ) {
641            return 1;
642        } else {
643            return 0;
644        }
645    }

Legend:
Removed from v.918  
changed lines
  Added in v.996

  ViewVC Help
Powered by ViewVC 1.1.26