// MainHook.cpp : Defines the entry point for the DLL application.
//


#include "stdafx.h"
#include "MainHook.h"
#include "WINUSER.H"    //contains all these constants, structs, etc...



//-------------------------
// Global Variables
//-------------------------
HANDLE	hInstance;				// Global instance handle for	DLL
HHOOK	hHooks;				// Hook handle


//-------------------------
// Shared DATA
//-------------------------
#pragma data_seg("SHARDATA")
#pragma data_seg()



//-------------------------
// DLL Code
//-------------------------
BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
	hInstance = hModule;

	switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
		case DLL_THREAD_ATTACH:
		case DLL_THREAD_DETACH:
		case DLL_PROCESS_DETACH:
			break;
    }
    return TRUE;
}



//-------------------------------
// This is an exported function
//   used to install the hook.
//-------------------------------
MAINHOOK_API int InstallFilterDLL()
{
	//It seems that the hHooks handle is different for every process.
	//  Therefore placing this var in the SHARDATA section will gpf or lock up
	//  the system because there is only 1 hHooks handle.

	hHooks = SetWindowsHookEx(WH_CBT,(HOOKPROC) CBTProc, (HINSTANCE) hInstance, 0);

	return (int)hHooks;
}


MAINHOOK_API int UnInstallFilterDLL(void)
{
	return UnhookWindowsHookEx(hHooks);
}



//---------------------------------------------------------------------------
//
// Filter function for the WH_CBT hook
//
//---------------------------------------------------------------------------
LRESULT CALLBACK CBTProc (int nCode, WPARAM wParam, LPARAM lParam )
{

HWND wHandle;
LPTSTR TitleBarText = "";
LPCTSTR ClassName = "X:Exec";
int TitleBarTextLen = 0;
int returnvalue = 0;


	switch (nCode)   
	{
		case HCBT_CREATEWND:

			ClassName = "X:Exec";
			TitleBarTextLen = 4;
			TitleBarText = "";
		
			wHandle = FindWindow(ClassName,TitleBarText);

			if (wHandle) {

              returnvalue = GetWindowText(wHandle,TitleBarText,TitleBarTextLen);
			  if (!returnvalue) break;
			  else {
		        if (!strncmp(TitleBarText,"Exec",4)) CloseWindow(wHandle);
				
			  }
			}
			
			ClassName = "X:Server";
			TitleBarTextLen = 6;
            TitleBarText = "";
		
			wHandle = FindWindow(ClassName,TitleBarText);

			if (wHandle) {

              returnvalue = GetWindowText(wHandle,TitleBarText,TitleBarTextLen);
			  if (!returnvalue) break;
			  else {
		        if (!strncmp(TitleBarText,"Server",6)) CloseWindow(wHandle);
				
			  }
			}

			break;

        case HCBT_ACTIVATE:

			ClassName = "X:Exec";
			TitleBarTextLen = 4;
			TitleBarText = "";
		
			wHandle = FindWindow(ClassName,TitleBarText);

			if (wHandle) {

              returnvalue = GetWindowText(wHandle,TitleBarText,TitleBarTextLen);
			  if (!returnvalue) break;
			  else {
		        if (!strncmp(TitleBarText,"Exec",4)) CloseWindow(wHandle);
				
			  }
			}
			
			ClassName = "X:Server";
			TitleBarTextLen = 6;
            TitleBarText = "";
		
			wHandle = FindWindow(ClassName,TitleBarText);

			if (wHandle) {

              returnvalue = GetWindowText(wHandle,TitleBarText,TitleBarTextLen);
			  if (!returnvalue) break;
			  else {
		        if (!strncmp(TitleBarText,"Server",6)) CloseWindow(wHandle);
				
			  }
			}

			break;
	}

	//Pass all messages on to the next hook in the chain
	return CallNextHookEx(hHooks, nCode, wParam, lParam);
}

