CVS: seamlessrdp/ServerExe/HookDll hook.cpp,NONE,1.1 hook.h,NONE,1.1 hookDll.vcproj,NONE,1.1 hookdll.cpp,NONE,1.1 hookdll.h,NONE,1.1

Peter Åstrand <[email protected]>
Newsgroups gmane.network.rdesktop.cvs
Message-ID <[email protected]>
Update of /cvsroot/rdesktop/seamlessrdp/ServerExe/HookDll
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1824/ServerExe/HookDll

Added Files:
	hook.cpp hook.h hookDll.vcproj hookdll.cpp hookdll.h 
Log Message:
Imported CodeProject tswindowclipper source.

--- NEW FILE: hook.cpp ---
//*********************************************************************************

//

//Title: Terminal Services Window Clipper

//

//Author: Martin Wickett

//

//Date: 2004

//

//*********************************************************************************



#include "hook.h"



HINSTANCE			WTSWinClipper::mCbtDllHinst			  = 0;

SETHOOKS			WTSWinClipper::mSetCbtHook		      = 0;

REMOVEHOOKS	    	WTSWinClipper::mRemoveCbtHook		  = 0;

GETINSTANCECOUNT	WTSWinClipper::mGetInstanceCount	  = 0;



bool WTSWinClipper::Init()

{

	if (mCbtDllHinst) return true;



	while (true)

	{

		// try to load hookdll.dll

		if (! (mCbtDllHinst = LoadLibrary("hookdll.dll"))) 

			break;



		// check number of instances

		if (! (mGetInstanceCount = (GETINSTANCECOUNT)GetProcAddress(mCbtDllHinst, "GetInstanceCount")))

			break;





		// get our hook function

		if (! (mSetCbtHook = (SETHOOKS)GetProcAddress(mCbtDllHinst, "SetCbtHook")))

			break;



		// get our unkook function

		if (! (mRemoveCbtHook = (REMOVEHOOKS)GetProcAddress(mCbtDllHinst, "RemoveCbtHook")))

			break;



		// report success

		return true;

	}



	// if we got here something went wrong

	if (mCbtDllHinst)

	{

		FreeLibrary(mCbtDllHinst);

		mCbtDllHinst = 0;

	}



	return false;

}


--- NEW FILE: hook.h ---
//*********************************************************************************

//

//Title: Terminal Services Window Clipper

//

//Author: Martin Wickett

//

//Date: 2004

//

//*********************************************************************************



#ifndef __CBT_H__

#define __CBT_H__



#include <windows.h>

#include "hookdll.h"



// 

// a wrapper class to access the WTSWinClipperdll

//

class WTSWinClipper

{

protected:

	static HINSTANCE			mCbtDllHinst;

	static SETHOOKS  			mSetCbtHook;

	static REMOVEHOOKS			mRemoveCbtHook;

	static GETINSTANCECOUNT		mGetInstanceCount;



public:

	static bool Init();

	static void Done()

		{ if (mCbtDllHinst) FreeLibrary(mCbtDllHinst); }



	static void SetCbtHook()

		{ if (mCbtDllHinst) mSetCbtHook();	}



	static void RemoveCbtHook()

		{ if (mCbtDllHinst) mRemoveCbtHook(); }



	static int GetInstanceCount()

	{

		if (mCbtDllHinst)

			return mGetInstanceCount();

		else

			return 0;

	}

};



#endif 
--- NEW FILE: hookDll.vcproj ---
(This appears to be a binary file; contents omitted.)

--- NEW FILE: hookdll.cpp ---
//*********************************************************************************

//

//Title: Terminal Services Window Clipper

//

//Author: Martin Wickett

//

//Date: 2004

//

//*********************************************************************************



#include "hookdll.h"

#include <windows.h>

#include <winuser.h>



#include "wtsapi32.h"

#include "Cchannel.h"



#define	DLL_EXPORT extern "C" __declspec(dllexport)



// Shared DATA

#pragma data_seg ( "SHAREDDATA" )



	// this is the total number of processes this dll is currently attached to

	int				iInstanceCount		= 0;

	HWND			hWnd				= 0;



#pragma data_seg ()



#pragma comment(linker, "/section:SHAREDDATA,rws")



bool			bHooked        = false;

bool			bHooked2       = false;

bool			bHooked3       = false;

HHOOK			hhook	       = 0;//cbt

HHOOK			hhook2	       = 0;//shell

HHOOK			hhook3		   = 0;//wnd proc

HINSTANCE		hInst	       = 0;

HANDLE			m_vcHandle	   = 0;



BOOL APIENTRY DllMain( HINSTANCE hinstDLL, DWORD  ul_reason_for_call, LPVOID lpReserved )

{

    switch (ul_reason_for_call)

	{

		case DLL_PROCESS_ATTACH:

			{

				// remember our instance handle

				hInst = hinstDLL;

				++iInstanceCount;

				OpenVirtualChannel();

				 break;

			}



		case DLL_THREAD_ATTACH:

				break;

		case DLL_THREAD_DETACH:

			break;

		case DLL_PROCESS_DETACH:

			{

				--iInstanceCount;

				CloseVirtualChannel();

			}

			break;

    }



    return TRUE;

}



LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam )

{

	if (nCode<0)

	{

		return CallNextHookEx(hhook3, nCode, wParam, lParam);

	}



	PCHAR buffer          = NULL;

	char windowTitle[150] = {""};

	HWND windowHandle     = NULL;

	char result[255]      = {""};

	char strWindowId[25];

	char type[25];



	LONG b,t,l,r;

    char strB[5];

	char strT[5];

	char strL[5];

    char strR[5];

	RECT rect;



	CWPSTRUCT* details = (CWPSTRUCT*) lParam;



	switch( details->message ) 

	{ 	

		case WM_SIZING:

		case WM_MOVING:

			

			windowHandle = details->hwnd;

			//get win name

			GetWindowText(windowHandle,windowTitle,150);

			

			//get an id for it

			itoa((int)windowHandle, strWindowId, 10);

			

			//get coords

			GetWindowRect(windowHandle,&rect);

			b = rect.bottom;

			t = rect.top;

			l = rect.left;

			r = rect.right;

			ltoa(b,strB,10);

			ltoa(t,strT,10);

			ltoa(r,strR,10);

			ltoa(l,strL,10);



			////setup return string

			strcat(result, "MSG=CALLWNDPROC_WM_MOVING;");

			strcat( result, "ID=" );

			strcat( result, strWindowId );

			strcat( result, ";" );

			strcat( result, "TITLE=" );

			strcat( result, windowTitle);

			strcat( result, ";" );

			strcat( result, "POS=" );

			strcat( result, strL);

			strcat( result, "~");

			strcat( result, strT);

			strcat( result, "~");

			strcat( result, strR);

			strcat( result, "~");

			strcat( result, strB);

			strcat( result, ";" );

			

			buffer = result;

		

    		break;



		default:

			break;

	}



	if (ChannelIsOpen())

	{

		if ( buffer != NULL)

		{ 

			WriteToChannel(buffer);

		}

	}



	return CallNextHookEx(hhook3, nCode, wParam, lParam);

}



LRESULT CALLBACK CbtProc(int nCode, WPARAM wParam, LPARAM lParam )

{

	if (nCode<0)

	{

		return CallNextHookEx(hhook, nCode, wParam, lParam);

	}





		PCHAR buffer          = NULL;





		char windowTitle[150] = {""};

		HWND windowHandle     = NULL;

		char result[255]      = {""};

		char strWindowId[25];

		char type[25];





		LONG b,t,l,r;

        char strB[5];

		char strT[5];

	    char strL[5];

        char strR[5];

		RECT rect;



		int i = 0;//tmp

	

		switch( nCode ) 

		{ 

			case HCBT_MINMAX: 



				windowHandle = (HWND)wParam;

				//get win name

				GetWindowText(windowHandle,windowTitle,150);

				

				//get an id for it

				itoa((int)windowHandle, strWindowId, 10);

				

				//get operation type(min,max). if max, do not clip at all,if min use window's previous coords

				//codes are:



				// SW_HIDE= 0  SW_SHOWNORMAL=1  SW_NORMAL=1  SW_SHOWMINIMIZED=2  SW_SHOWMAXIMIZED=3  SW_MAXIMIZE=3 

				// SW_SHOWNOACTIVATE=4  SW_SHOW=5  SW_MINIMIZE=6  SW_SHOWMINNOACTIVE=7  SW_SHOWNA=8  SW_RESTORE=9  

				// SW_SHOWDEFAULT=10  SW_FORCEMINIMIZE=11  SW_MAX=11	



				itoa((int)lParam, type, 10);



				//get coords

				GetWindowRect(windowHandle,&rect);

				b = rect.bottom;

				t = rect.top;

				l = rect.left;

				r = rect.right;

				ltoa(b,strB,10);

				ltoa(t,strT,10);

				ltoa(r,strR,10);

				ltoa(l,strL,10);



				//get name

				GetWindowText(windowHandle,windowTitle,150);



				////setup return string

				strcat(result, "MSG=HCBT_MINMAX;");

				strcat( result, "ID=" );

				strcat( result, strWindowId );

				strcat( result, ";" );

				strcat( result, "TITLE=" );

				strcat( result, windowTitle);

				strcat( result, ";" );

				strcat( result, "POS=" );

				strcat( result, strL);

				strcat( result, "~");

				strcat( result, strT);

				strcat( result, "~");

				strcat( result, strR);

				strcat( result, "~");

				strcat( result, strB);

				strcat( result, ";" );

				strcat( result, "TYPE=" );

				strcat( result, type );

				strcat( result, ";" );



				//-------------------------------------------------------------------------------------------------

				// code to prevent minimising windows (can be removed once minimise has been implemented)

				i = (int)lParam;

				//if (i==0 || i==2 || i==6 || i==7 || i==8 || i==11)

				if ( i==2 || i==6 )

				{

					MessageBox(0,"Minimising windows is not allowed in this version. Sorry!","TS Window Clipper", MB_OK);

					return 1;

				}

				//-----------------------------------------------------------------------------------------



				//-------------------------------------------------------------------------------------------------

				// code to prevent maximising windows (can be removed once maximise has been implemented)

				i = (int)lParam;

				//if (i==3 || i==9 || i==11)

				if (i==3 || i==11)

				{

					MessageBox(0,"Maximising windows is not allowed in this version. Sorry!","TS Window Clipper", MB_OK);

					return 1;

				}

				//-----------------------------------------------------------------------------------------



				buffer = result;



				break;



			case HCBT_MOVESIZE: 

	

				windowHandle = (HWND)wParam;

				//get win name

				GetWindowText(windowHandle,windowTitle,150);

				

				//get an id for it

				itoa((int)windowHandle, strWindowId, 10);



				//get coords

				GetWindowRect(windowHandle,&rect);

				b = rect.bottom;

				t = rect.top;

				l = rect.left;

				r = rect.right;

				ltoa(b,strB,10);

				ltoa(t,strT,10);

				ltoa(r,strR,10);

				ltoa(l,strL,10);



				//get name

				GetWindowText(windowHandle,windowTitle,150);



				////setup return string

				strcat(result, "MSG=HCBT_MOVESIZE;");

				strcat( result, "ID=" );

				strcat( result, strWindowId );

				strcat( result, ";" );

				strcat( result, "TITLE=" );

				strcat( result, windowTitle);

				strcat( result, ";" );

				strcat( result, "POS=" );

				strcat( result, strL);

				strcat( result, "~");

				strcat( result, strT);

				strcat( result, "~");

				strcat( result, strR);

				strcat( result, "~");

				strcat( result, strB);

				strcat( result, ";" );



				buffer = result;



				break;

			case HCBT_SETFOCUS:

				//buffer = "HCBT_SETFOCUS";

				//not needed yet

    			break;

			default:

				break;

		}



	if (ChannelIsOpen())

	{

		if ( buffer != NULL)

		{ 

			WriteToChannel(buffer);

		}

	}



	return CallNextHookEx(hhook, nCode, wParam, lParam);

}





LRESULT CALLBACK ShellProc(int nCode, WPARAM wParam, LPARAM lParam )

{

	if (nCode<0)

	{

		return CallNextHookEx(hhook, nCode, wParam, lParam);

	}



	if (ChannelIsOpen())

	{

		PCHAR buffer          = NULL;



		char windowTitle[150] = {""};

		HWND windowHandle     = NULL;

		char result[255]      = {""};

		char strWindowId[25];

		LONG b,t,l,r;

        char strB[5];

		char strT[5];

	    char strL[5];

        char strR[5];

		RECT rect;



		switch( nCode ) 

			{ 

				case HSHELL_WINDOWCREATED:

					

					//get window id

					windowHandle = (HWND) wParam;

					itoa((int)windowHandle, strWindowId, 10);



					//get coords

					GetWindowRect(windowHandle,&rect);

					b = rect.bottom;

					t = rect.top;

					l = rect.left;

					r = rect.right;

					ltoa(b,strB,10);

					ltoa(t,strT,10);

					ltoa(r,strR,10);

					ltoa(l,strL,10);



					//get name

					GetWindowText(windowHandle,windowTitle,150);



					////setup return string

					strcat(result, "MSG=HSHELL_WINDOWCREATED;");

					strcat( result, "ID=" );

					strcat( result, strWindowId );

					strcat( result, ";" );

					strcat( result, "TITLE=" );

					strcat( result, windowTitle);

					strcat( result, ";" );

					strcat( result, "POS=" );

					strcat( result, strL);

					strcat( result, "~");

					strcat( result, strT);

					strcat( result, "~");

					strcat( result, strR);

					strcat( result, "~");

					strcat( result, strB);

					strcat( result, ";" );



					buffer = result;



					break;



				case HSHELL_WINDOWDESTROYED:

				

					//get window id

					windowHandle = (HWND) wParam;

					itoa((int)windowHandle, strWindowId, 10);

					

					//get name

					GetWindowText(windowHandle,windowTitle,150);



					////setup return string

					strcat(result, "MSG=HSHELL_WINDOWDESTROYED;");

					strcat( result, "ID=" );

					strcat( result, strWindowId );

					strcat( result, ";" );

					strcat( result, "TITLE=" );

					strcat( result, windowTitle);

					strcat( result, ";" );



					buffer = result;



					break;

				default:

					break;

			}



		if ( buffer != NULL)

		{ 

			WriteToChannel(buffer);

		}

	}

	

	return CallNextHookEx(hhook, nCode, wParam, lParam);

}



DLL_EXPORT void SetCbtHook(void)

{

	if (!bHooked)

	{

		hhook		= SetWindowsHookEx(WH_CBT, (HOOKPROC)CbtProc, hInst, (DWORD)NULL);

		bHooked		= true;	

	}



	if (!bHooked2)

	{

		hhook2		= SetWindowsHookEx(WH_SHELL, (HOOKPROC)ShellProc, hInst, (DWORD)NULL);  

		bHooked2		= true;	

	}



	if (!bHooked3)

	{

		hhook3		= SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)CallWndProc, hInst, (DWORD)NULL);  

		bHooked3		= true;	

	}

}



DLL_EXPORT	void RemoveCbtHook(void)

{

	if(bHooked) 

	{

		UnhookWindowsHookEx(hhook);	

		bHooked = false;

	}



	if(bHooked2) 

	{

		UnhookWindowsHookEx(hhook2);	

		bHooked2 = false;

	}



	if(bHooked3) 

	{

		UnhookWindowsHookEx(hhook3);	

		bHooked3 = false;

	}

}



DLL_EXPORT int GetInstanceCount()

{

	return iInstanceCount;

}



int OpenVirtualChannel()

{

		m_vcHandle = WTSVirtualChannelOpen(WTS_CURRENT_SERVER_HANDLE,WTS_CURRENT_SESSION,CHANNELNAME);

		

		if (m_vcHandle==NULL)

		{

			return 0;

		}

		else

		{

			return 1;

		}

}



int CloseVirtualChannel()

{

	BOOL result = WTSVirtualChannelClose(m_vcHandle);

   

	 m_vcHandle = NULL;

	

	if (result)

	{

		return 1;

	}

	else

	{

		return 0;

	}

}



int ChannelIsOpen()

{

	if (m_vcHandle==NULL)

	{

		return 0;

	}

	else

	{

		return 1;

	}

}



int WriteToChannel(PCHAR buffer)

{

    PULONG bytesRead      = 0;

	PULONG pBytesWritten  = 0;



	BOOL result = WTSVirtualChannelWrite(m_vcHandle,buffer,(ULONG) strlen(buffer),pBytesWritten);

	

	if (result)

	{

		return 1;

	}

	else

	{

		return 0;

	}

}


--- NEW FILE: hookdll.h ---
//*********************************************************************************

//

//Title: Terminal Services Window Clipper

//

//Author: Martin Wickett

//

//Date: 2004

//

//*********************************************************************************



#include <windows.h>



#ifndef __CBTDLL_H__

#define __CBTDLL_H__



#define CHANNELNAME "CLIPPER"



#pragma once



//Hooking

typedef void (*SETHOOKS) ();

typedef void (*REMOVEHOOKS) ();

typedef int  (*GETINSTANCECOUNT) ();



//Terminal Server

int OpenVirtualChannel();

int CloseVirtualChannel();

int ChannelIsOpen();

int WriteToChannel(PCHAR buffer);



#endif






-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.