⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hook.c

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 C
字号:
/*__________________________________________________________________________
 Copyright (C) 2002 PGP Corporation
 All rights reserved.
 
 $Id: hook.c,v 1.12 2002/08/06 20:09:42 dallen Exp $
__________________________________________________________________________*/

#include "windows.h"
#include "resource.h"
#include "PGPhk.h"
//#include "conerror.h"

HINSTANCE g_hinst;

#define OE_REG_KEY	"Software\\PGP Corporation\\PGP\\InstalledComponents"

static char szModule[MAX_PATH];
/*
#pragma data_seg(".SHARDAT")
static HHOOK  g_hCBTHook   = NULL ;
static DWORD  TrayProcessID = 0   ;
static HWND   hwndShell_TrayWnd = 0   ;
static HWND   hwndReBarWindow32 = 0   ;
static HWND   hwndMSTaskSwWClass = 0   ;
static HWND   hwndSysTabControl32 = 0   ;
static HWND   CurrentFocus = NULL ; // SetFocus window
#pragma data_seg()
*/
void (*AttachOutlookExpressPlugin)(HWND);
void (*AttachICQPlugin)(HWND);

typedef struct
{
	HHOOK  g_hCBTHook;
	DWORD  TrayProcessID;
	HWND   hwndShell_TrayWnd;
	HWND   hwndReBarWindow32;
	HWND   hwndMSTaskSwWClass;
	HWND   hwndSysTabControl32;
	HWND   CurrentFocus;
} HKSHARED;

HKSHARED *HKGetSharedMem(HANDLE *hMapObject)
{
	void *mem;

	*hMapObject=CreateFileMapping
		((HANDLE)0xFFFFFFFF,
		NULL,
		PAGE_READWRITE,
		0,
		sizeof(HKSHARED),
		"PGPhkSharedMemory");

	if(*hMapObject==NULL)
		return NULL;

	mem=MapViewOfFile(*hMapObject,FILE_MAP_WRITE,0,0,0);

	return mem;
}

void HKReleaseSharedMem(HKSHARED *hkShared,HANDLE hMapObject)
{
	UnmapViewOfFile(hkShared);
	
	CloseHandle(hMapObject);
}

int mystrlen(char *string)
{
	int len;

	len=0;

	while(string[len]!=0)
	{
		len++;
	}

	return len;
}

int mystrcmp(char *string1,char *string2)
{
	int index,len1,len2;

	len1=mystrlen(string1);
	len2=mystrlen(string2);

	if(len1!=len2)
		return 1;

	for(index=0;index<len1;index++)
	{
		if(string1[index]!=string2[index])
		{
			return 1;
		}
	}

	return 0; // They are equal
}

LRESULT WINAPI CBTProc (int nCode, WPARAM wParam, LPARAM lParam)
{
	HKSHARED *hkShared;
	HHOOK hCBTHook;
	HANDLE hMapObject;
	
	// Lots of overhead here but the only way to do it for
	// terminal services and XP. .
	hkShared=HKGetSharedMem(&hMapObject);
	hCBTHook=hkShared->g_hCBTHook;

	if (nCode >= 0)
	{
		switch (nCode)
        {
			case HCBT_SETFOCUS:
			{
				HWND hwndCurrent;
				DWORD ThreadID;
				DWORD ProcessID;

				hwndCurrent=(HWND)wParam;

				ThreadID=GetWindowThreadProcessId(hwndCurrent,&ProcessID);
				if((ProcessID!=hkShared->TrayProcessID)&&(hkShared->CurrentFocus!=hwndCurrent)&&
					(hwndCurrent!=NULL)&&
					// These have to be checked for because of brain
					// dead Windows2000 (tm)
					(hwndCurrent!=hkShared->hwndSysTabControl32)&&
					(hwndCurrent!=hkShared->hwndMSTaskSwWClass)&&
					(hwndCurrent!=hkShared->hwndReBarWindow32)&&
					(hwndCurrent!=hkShared->hwndShell_TrayWnd))
				{
					hkShared->CurrentFocus=hwndCurrent;
					FlushViewOfFile(hkShared,sizeof(HKSHARED));
				}
				break;
			}	
			
			case HCBT_CREATEWND:
			{
				// For ICQ
				CREATESTRUCT *lpcs;
				char szWndClass[32];
				int iLen,i,iSlash;

				GetClassName((HWND) wParam, szWndClass, sizeof(szWndClass)-1);

				// RichEdit20A is new for ICQ 2001b
				if((!mystrcmp(szWndClass,"RICHEDIT"))||(!mystrcmp(szWndClass,"RichEdit20A")))
				{
					lpcs=((CBT_CREATEWND *)lParam)->lpcs;

					GetModuleFileName(lpcs->hInstance,szModule,sizeof(szModule));

					// Fancy code needed to get rid of libc library
					// because of VC6 bugs with 16 bit CBT hooks
					iLen=mystrlen(szModule);

					iSlash=0;

					for(i=0;i<iLen;i++)
					{
						// Find slash
						if(szModule[i]=='\\')
							iSlash=i+1;

						// Make upper case
						szModule[i]=szModule[i]|32;
					}

					if(!mystrcmp(&(szModule[iSlash]),"icq.exe"))
					{
						HANDLE hPGPicq;
						DLGPROC lpOldProc;

						lpOldProc = (DLGPROC)GetProp((HWND) wParam, "oldproc");
				
						if (lpOldProc != NULL)
							break;

						hPGPicq = LoadLibrary("PGPicq.dll");
				
						if (hPGPicq != NULL)
						{
							AttachICQPlugin = (void (*)(HWND)) 
							GetProcAddress(hPGPicq, 
								"AttachICQPlugin");

							AttachICQPlugin((HWND) wParam);
						}
					}
				}
				// Drop through for outlook
			}

			case HCBT_ACTIVATE:
			{
				char szWndClass[32];
				HANDLE hPGPoe;
				DLGPROC lpOldProc;
				HKEY hkey;

				/* First check to see if this is an Outlook Express window */
				
				GetClassName((HWND) wParam, szWndClass, sizeof(szWndClass)-1);

				if (mystrcmp(szWndClass, "ThorBrowserWndClass") && 
					mystrcmp(szWndClass, "ATH_Note") &&
					mystrcmp(szWndClass, "Outlook Express Browser Class"))
				{
					break;
				}

				/* Check to make sure we haven't already attached */
				
				lpOldProc = (DLGPROC)GetProp((HWND) wParam, "oldproc");
				if (lpOldProc != NULL)
					break;
				
				/* Don't try loading Outlook Express plug-in if
					it wasn't installed */

				if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, OE_REG_KEY, 0, 
						KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS)
				{
					BYTE lpValue[4] = {0,0,0,0};
					DWORD dwSize = sizeof(lpValue);
					DWORD dwType;

					RegQueryValueEx(hkey, "OUTLOOKEXPRESS", NULL, &dwType,
						lpValue, &dwSize);
					RegCloseKey(hkey);

					if (*((DWORD *) lpValue) != 1)
						break;
				}
				else
					break;

				/* Attempt to load the Outlook Express plug-in DLL */
				
				hPGPoe = LoadLibrary("PGPoe.dll");
				if (hPGPoe == NULL)
				{
					char szMsg[255];
					char szTitle[255];
					
					LoadString(g_hinst, IDS_NOPLUGIN, szMsg, 254);
					LoadString(g_hinst, IDS_TITLE, szTitle, 254);
					MessageBox(NULL, szMsg, szTitle, MB_ICONEXCLAMATION);
				}
				else
				{
					AttachOutlookExpressPlugin = (void (*)(HWND)) 
						GetProcAddress(hPGPoe, "AttachOutlookExpressPlugin");
					
					AttachOutlookExpressPlugin((HWND) wParam);
				}
				break;
			}	
		}			
	}

	HKReleaseSharedMem(hkShared,hMapObject);

	return CallNextHookEx (hCBTHook, nCode, wParam, lParam);
}


HWND PGPhkReadCurrentFocus(void)
{
	HKSHARED *hkShared;
	HWND hwndCurrent;
	HANDLE hMapObject;
	
	hkShared=HKGetSharedMem(&hMapObject);
	hwndCurrent=hkShared->CurrentFocus;
	HKReleaseSharedMem(hkShared,hMapObject);

	return hwndCurrent;
}

// Global just for the tray process when it sets and deletes it
HKSHARED *hkSharedInit;
HANDLE hMapObject;

void PGPhkSetHook(void)
{
	hkSharedInit=HKGetSharedMem(&hMapObject);
	
	memset(hkSharedInit,0x00,sizeof(HKSHARED));

	hkSharedInit->TrayProcessID=GetCurrentProcessId();

	// These have to be checked for because of brain
	// dead Windows2000 (tm)
	hkSharedInit->hwndShell_TrayWnd=FindWindowEx(NULL,
		NULL,"Shell_TrayWnd",NULL);
	hkSharedInit->hwndReBarWindow32=FindWindowEx(hkSharedInit->hwndShell_TrayWnd,
		NULL,"ReBarWindow32",NULL);
	hkSharedInit->hwndMSTaskSwWClass=FindWindowEx(hkSharedInit->hwndReBarWindow32,
		NULL,"MSTaskSwWClass",NULL);
	hkSharedInit->hwndSysTabControl32=FindWindowEx(hkSharedInit->hwndMSTaskSwWClass,
		NULL,"SysTabControl32",NULL);

	hkSharedInit->g_hCBTHook=SetWindowsHookEx(WH_CBT, 
		(HOOKPROC)CBTProc,
		g_hinst,
		0);

	FlushViewOfFile(hkSharedInit,sizeof(HKSHARED));
}

void PGPhkDeleteHook(void)
{
	UnhookWindowsHookEx(hkSharedInit->g_hCBTHook);
	hkSharedInit->hwndShell_TrayWnd=0;
	hkSharedInit->hwndMSTaskSwWClass=0;
	hkSharedInit->hwndSysTabControl32=0;
	hkSharedInit->hwndReBarWindow32=0;

	FlushViewOfFile(hkSharedInit,sizeof(HKSHARED));

	HKReleaseSharedMem(hkSharedInit,hMapObject);
}
		
BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason,LPVOID lpvReserved)
{
   switch (fdwReason) {
      case DLL_PROCESS_ATTACH:
         g_hinst = hinstDll;
		 //vErrorOut(fg_white,"DLL_PROCESS_ATTACH\n");
         break;
      case DLL_THREAD_ATTACH:
		  //vErrorOut(fg_white,"DLL_THREAD_ATTACH\n");
         break;
      case DLL_THREAD_DETACH:
		  //vErrorOut(fg_red,"DLL_THREAD_DETACH\n");
		break;
      case DLL_PROCESS_DETACH:
		  //vErrorOut(fg_red,"DLL_PROCESS_DETACH\n");
         break;
   }
   return(TRUE);
}

/*__Editor_settings____

	Local Variables:
	tab-width: 4
	End:
	vi: ts=4 sw=4
	vim: si
_____________________*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -