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

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

  ViewVC Help
Powered by ViewVC 1.1.26