15,472
社区成员
发帖
与我相关
我的任务
分享
// dll.cpp : 定义 DLL 应用程序的入口点。
//
#include "stdafx.h"
#ifdef _MANAGED
#pragma managed(push, off)
#endif
//常量声明:
bool gRegisteredSubClass = false;//是否已经注册了子窗口过程
HINSTANCE gDllInstance;//当前DLL的实例
static HHOOK gHook = 0;//HOOK
//函数声明:
HWND GetClockWindow ();
static LRESULT CALLBACK HookProc (int nCode, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK ClockProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call){
case DLL_PROCESS_ATTACH:
gDllInstance = hModule;
DisableThreadLibraryCalls (gDllInstance);
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
extern "C" _declspec(dllexport) BOOL registerTrayClockSubClass(){
//获得时钟所在的线程
DWORD explorerProcessId;
DWORD clockThreadId = GetWindowThreadProcessId (GetClockWindow (),
&explorerProcessId);
if (!explorerProcessId){
return false;
}
//将dll注册到线程中去
SetWindowsHookEx (WH_GETMESSAGE, HookProc, gDllInstance, clockThreadId);
return true;
}
//获得到TrayClock的Hwnd
HWND
GetClockWindow ()
{
HWND hShellTrayWnd = FindWindow (_T ("Shell_TrayWnd"), 0);
HWND hTrayNotifyWnd = FindWindowEx (hShellTrayWnd, 0, _T ("TrayNotifyWnd"), 0);
HWND hTrayClockWClass = FindWindowEx (hTrayNotifyWnd, 0,_T ("TrayClockWClass"), 0);
return hTrayClockWClass;
}
//HOOK的回调函数
static LRESULT CALLBACK
HookProc (int nCode, WPARAM wParam, LPARAM lParam)
{
//如果没有注册SubClass,就注册
if (!gRegisteredSubClass)
{
//::SetWindowSubclass(GetClockWindow (), ClockProc, 0, 0);
::SetWindowLong(GetClockWindow (), GWL_WNDPROC, (LONG)ClockProc);
gRegisteredSubClass = true;
}
return CallNextHookEx(gHook, nCode, wParam, lParam);
}
//时钟的回调函数
static LRESULT CALLBACK
ClockProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
LRESULT ret_val = 0;
if(WM_LBUTTONDOWN==msg)
::MessageBox(NULL,NULL,NULL,MB_OK);
return ret_val;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
// dll.cpp : 定义 DLL 应用程序的入口点。
//
#include "stdafx.h"
#ifdef _MANAGED
#pragma managed(push, off)
#endif
//全局变量声明:
bool gRegisteredSubClass = false;//是否已经注册了子窗口过程
HINSTANCE gDllInstance;//当前DLL的实例
static HHOOK gHook = 0;//HOOK
HWND gHwndShellTrayWindow = 0;
//WNDPROC fOldShellTrayProc;//任务栏旧有的窗口函数
//函数声明:
HWND GetShellTrayWindow ();
HWND GetClockWindow ();
static LRESULT CALLBACK HookProc (int nCode, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK ShellTrayWindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
//static LRESULT CALLBACK ShellTrayWindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
//----------------------------------------DLL入库函数-------------------------------------------------
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call){
case DLL_PROCESS_ATTACH:
gDllInstance = hModule;
DisableThreadLibraryCalls (gDllInstance);
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
//-----------------------------------------工具函数-----------------------------------------------
//获得到TrayClock的Hwnd
HWND
GetClockWindow ()
{
HWND hShellTrayWnd = GetShellTrayWindow ();
HWND hTrayNotifyWnd = FindWindowEx (hShellTrayWnd, 0, _T ("TrayNotifyWnd"), 0);
HWND hTrayClockWClass = FindWindowEx (hTrayNotifyWnd, 0,_T ("TrayClockWClass"), 0);
return hTrayClockWClass;
}
//获得到任务栏的句柄
HWND
GetShellTrayWindow ()
{
HWND hShellTrayWnd = FindWindow (_T ("Shell_TrayWnd"), 0);
return hShellTrayWnd;
}
//----------------------------------------外部接口-------------------------------------------------
//注册函数
extern "C" _declspec(dllexport) BOOL registerShellTraySubClass(){
//获得任务栏所在的线程
::MessageBox(NULL,_T("开始申请"),NULL,MB_OK);
gHwndShellTrayWindow = GetShellTrayWindow ();
DWORD explorerProcessId;
DWORD clockThreadId = GetWindowThreadProcessId (GetShellTrayWindow (),
&explorerProcessId);
if (!explorerProcessId){
return false;
}
//将dll注册到线程中去
gHook = SetWindowsHookEx (WH_GETMESSAGE, HookProc, gDllInstance, clockThreadId);
::MessageBox(NULL,_T("结束申请"),NULL,MB_OK);
return true;
}
//结束注册函数
extern "C" _declspec(dllexport) BOOL unRegisterShellTraySubClass(){
::MessageBox(NULL,_T("开始注销"),NULL,MB_OK);
//移除子窗口过程
BOOL res = ::RemoveWindowSubclass(GetShellTrayWindow (), ShellTrayWindowProc, 0);
if(res){
::MessageBox(NULL,_T("注销了SubClass"),NULL,MB_OK);
}else{
::MessageBox(NULL,_T("没有注销SubClass"),NULL,MB_OK);
}
//取消HOOK
BOOL hookRes = UnhookWindowsHookEx (gHook);
if(hookRes){
::MessageBox(NULL,_T("注销了HOOK"),NULL,MB_OK);
}else{
::MessageBox(NULL,_T("没有注销HOOK"),NULL,MB_OK);
}
::MessageBox(NULL,_T("结束注销"),NULL,MB_OK);
return true;
}
//----------------------------------------回调函数函数-------------------------------------------------
//HOOK的回调函数
static LRESULT CALLBACK
HookProc (int nCode, WPARAM wParam, LPARAM lParam)
{
//什么也不做,只为将DLL注入到目标进程
if(!gRegisteredSubClass){
gRegisteredSubClass = true;//这句要写在前面,否则执行下一句函数的间隙,系统会多次调用这个函数。
::SetWindowSubclass(GetShellTrayWindow (), ShellTrayWindowProc, 0, 0);
}
return CallNextHookEx(gHook, nCode, wParam, lParam);
}
//任务栏的回调函数
static LRESULT CALLBACK
ShellTrayWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
//ShellTrayWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT ret_val = 0;
if(uMsg == WM_NCLBUTTONDBLCLK) {
::MessageBox(NULL,NULL,NULL,MB_OK);
}
//return CallWindowProc((WNDPROC)fOldShellTrayProc,hwnd,uMsg,wParam,lParam);
return ::DefSubclassProc(hwnd, uMsg, wParam, lParam);
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
BCB_FANS 回复于:2002-12-31 00:59:21
“偶原来想的是替换ClockHandle的窗体,你为什么替换的是Shell_Tray的?”
因为那个时钟窗口变态,它不接受任何鼠标消息,就算替换了,你也截获不到任何有用的消息!!!
因此只好对它的爷爷下手(它的老爸TrayNotifyWnd也同样不接受鼠标消息)^_^