//////开始同步
void CMyDlg::OnDLL()
{
// TODO: Add your control notification handler code here
CEdit *textbox1=(CEdit*)GetDlgItem(IDC_EDIT1);
CListBox *listbox1=(CListBox*)GetDlgItem(LISTBOX1);
CListBox *listbox2=(CListBox*)GetDlgItem(LISTBOX2);
CButton *ADll=(CButton*)GetDlgItem(IDC_BUTTON5);
CButton *Check=(CButton*)GetDlgItem(IDC_BUTTON3);
CButton *SetMaster=(CButton*)GetDlgItem(IDC_BUTTON4);
CButton *Remove=(CButton*)GetDlgItem(IDC_BUTTON6);
CButton *KillPr=(CButton*)GetDlgItem(IDC_BUTTON7);
CString text;
CString temp;
/////////////////////////检查数据完整性////////////////////////////
textbox1->GetWindowText(text);
if(text=="")return;
///////////////////////////////////////////////////////////////////
ADll->GetWindowText(text);
if(text=="开始同步")
{
HWND hwnd[32];
textbox1->GetWindowText(temp);
hwnd[0]=GetHwnd(temp);
for(int i=0;i<listbox2->GetCount();i++)
{
listbox2->GetText(i,temp);
hwnd[i+1]=GetHwnd(temp);
}
///////////////////////////////
//读取DLL
instkbhook(hwnd); //////////////////////////////
Check->EnableWindow(FALSE);
SetMaster->EnableWindow(FALSE);
Remove->EnableWindow(FALSE);
KillPr->EnableWindow(FALSE);
text="停止同步";
ADll->SetWindowText(text);
}
else
{
///////////////////////////////
//读取DLL
unkbhook();
//////////////////////////////
Check->EnableWindow(TRUE);
SetMaster->EnableWindow(TRUE);
Remove->EnableWindow(TRUE);
KillPr->EnableWindow(TRUE);
text="开始同步";
ADll->SetWindowText(text);
}
//listbox2->AddString(text);
//Check->GetWindowText(text);
//listbox2->AddString(text);
}///获取句柄<非窗口事件,自添加函数>
HWND CMyDlg::GetHwnd(CString str)
{
int i;CString h="";
for(i=0;i<str.GetLength();i++)
{
if(str.GetAt(i)==',')break;
}
i++;
for(;i<str.GetLength();i++)
{
if(str.GetAt(i)==',')break;
h+=str.GetAt(i);
}
i=_ttoi((LPCTSTR)h);
return (HWND)i;
}
part2 DLL文件
//键盘钩子#include "stdafx.h"
#include "hodll.h"
#include "mmsystem.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///共享数据段
#pragma data_seg(".SHARDAT")
static HHOOK hkb=NULL;
static HHOOK mkb=NULL;
static HWND FollowWin[32]={NULL};
#pragma data_seg()
HINSTANCE hins;//dll实例句柄
static HWND MainWin=NULL;
char szBuf[256];
char *p;
CString msg;
////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CHodllApp, CWinApp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
/////钩子函数
LRESULT __declspec(dllexport)__stdcall CALLBACK K_Hook(int nCode,WPARAM wParam,LPARAM lParam)
{
if(HC_ACTION==nCode)
{
if(((DWORD)lParam&0x40000000))//按下
{
for(int i=1;i<32;i++)
{
if(FollowWin[i]) ::PostMessage(FollowWin[i],WM_KEYDOWN,wParam,0);
else break;
}
}
else
{
for(int i=1;i<32;i++)
{
if(FollowWin[i]) ::PostMessage(FollowWin[i],WM_KEYUP,wParam,0);
else break;
}
}
} LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam );
return RetVal;
}LRESULT __declspec(dllexport)__stdcall CALLBACK M_Hook(int nCode,WPARAM wParam,LPARAM lParam)
{
LPMOUSEHOOKSTRUCT pMouseHook=(MOUSEHOOKSTRUCT FAR *) lParam;
if(nCode>=0)
{
if(wParam==WM_MOUSEMOVE||wParam==WM_NCMOUSEMOVE||wParam==WM_LBUTTONDOWN||wParam==WM_RBUTTONDOWN||wParam==WM_LBUTTONUP||wParam==WM_RBUTTONUP)//右键按下
{
for(int i=1;i<32;i++)
{
if(FollowWin[i]) ::PostMessage(FollowWin[i],wParam,0,MAKELPARAM(pMouseHook->pt.x,pMouseHook->pt.y));
else break;
}
}
}
return CallNextHookEx(mkb,nCode,wParam,lParam);
}
///安装钩子
BOOL __declspec(dllexport)__stdcall installhook(HWND *Wnd)
{
if(Wnd[0])MainWin=Wnd[0];
unsigned long mainwid=::GetWindowThreadProcessId(MainWin,NULL);
//hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)K_Hook,hins,0);
//mkb=SetWindowsHookEx(WH_MOUSE,(HOOKPROC)M_Hook,hins,0);
hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)K_Hook,hins,mainwid);
mkb=SetWindowsHookEx(WH_MOUSE,(HOOKPROC)M_Hook,hins,mainwid);
int i=1;
while(Wnd[i])//记录所有句柄,脚码0为主控窗体句柄
{
FollowWin[i-1]=Wnd[i];
if(i>=31)
{
break;
}i++;
}
if(hkb&&mkb) return TRUE;
return FALSE;
}
///卸载钩子
BOOL __declspec(dllexport) UnHook()
{
BOOL unkhooked = UnhookWindowsHookEx(hkb);
BOOL unmhooked = UnhookWindowsHookEx(mkb);
return unkhooked&&unmhooked;
}
///用于初始化de封装类
BOOL CHodllApp::InitInstance ()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
hins=AfxGetInstanceHandle(); GetModuleFileName(AfxGetInstanceHandle( ),szBuf,sizeof(szBuf));
p = szBuf;
while(strchr(p,'\\'))
{
p = strchr(p,'\\');
p++;
}
*p = '\0';
msg=szBuf;
return TRUE;
}
BOOL CHodllApp::ExitInstance ()
{
return TRUE;
}
CHodllApp::CHodllApp()
{
}
CHodllApp theApp;
void CMyDlg::OnDLL()
{
// TODO: Add your control notification handler code here
CEdit *textbox1=(CEdit*)GetDlgItem(IDC_EDIT1);
CListBox *listbox1=(CListBox*)GetDlgItem(LISTBOX1);
CListBox *listbox2=(CListBox*)GetDlgItem(LISTBOX2);
CButton *ADll=(CButton*)GetDlgItem(IDC_BUTTON5);
CButton *Check=(CButton*)GetDlgItem(IDC_BUTTON3);
CButton *SetMaster=(CButton*)GetDlgItem(IDC_BUTTON4);
CButton *Remove=(CButton*)GetDlgItem(IDC_BUTTON6);
CButton *KillPr=(CButton*)GetDlgItem(IDC_BUTTON7);
CString text;
CString temp;
/////////////////////////检查数据完整性////////////////////////////
textbox1->GetWindowText(text);
if(text=="")return;
///////////////////////////////////////////////////////////////////
ADll->GetWindowText(text);
if(text=="开始同步")
{
HWND hwnd[32];
textbox1->GetWindowText(temp);
hwnd[0]=GetHwnd(temp);
for(int i=0;i<listbox2->GetCount();i++)
{
listbox2->GetText(i,temp);
hwnd[i+1]=GetHwnd(temp);
}
///////////////////////////////
//读取DLL
instkbhook(hwnd); //////////////////////////////
Check->EnableWindow(FALSE);
SetMaster->EnableWindow(FALSE);
Remove->EnableWindow(FALSE);
KillPr->EnableWindow(FALSE);
text="停止同步";
ADll->SetWindowText(text);
}
else
{
///////////////////////////////
//读取DLL
unkbhook();
//////////////////////////////
Check->EnableWindow(TRUE);
SetMaster->EnableWindow(TRUE);
Remove->EnableWindow(TRUE);
KillPr->EnableWindow(TRUE);
text="开始同步";
ADll->SetWindowText(text);
}
//listbox2->AddString(text);
//Check->GetWindowText(text);
//listbox2->AddString(text);
}///获取句柄<非窗口事件,自添加函数>
HWND CMyDlg::GetHwnd(CString str)
{
int i;CString h="";
for(i=0;i<str.GetLength();i++)
{
if(str.GetAt(i)==',')break;
}
i++;
for(;i<str.GetLength();i++)
{
if(str.GetAt(i)==',')break;
h+=str.GetAt(i);
}
i=_ttoi((LPCTSTR)h);
return (HWND)i;
}
part2 DLL文件
//键盘钩子#include "stdafx.h"
#include "hodll.h"
#include "mmsystem.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///共享数据段
#pragma data_seg(".SHARDAT")
static HHOOK hkb=NULL;
static HHOOK mkb=NULL;
static HWND FollowWin[32]={NULL};
#pragma data_seg()
HINSTANCE hins;//dll实例句柄
static HWND MainWin=NULL;
char szBuf[256];
char *p;
CString msg;
////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CHodllApp, CWinApp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
/////钩子函数
LRESULT __declspec(dllexport)__stdcall CALLBACK K_Hook(int nCode,WPARAM wParam,LPARAM lParam)
{
if(HC_ACTION==nCode)
{
if(((DWORD)lParam&0x40000000))//按下
{
for(int i=1;i<32;i++)
{
if(FollowWin[i]) ::PostMessage(FollowWin[i],WM_KEYDOWN,wParam,0);
else break;
}
}
else
{
for(int i=1;i<32;i++)
{
if(FollowWin[i]) ::PostMessage(FollowWin[i],WM_KEYUP,wParam,0);
else break;
}
}
} LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam );
return RetVal;
}LRESULT __declspec(dllexport)__stdcall CALLBACK M_Hook(int nCode,WPARAM wParam,LPARAM lParam)
{
LPMOUSEHOOKSTRUCT pMouseHook=(MOUSEHOOKSTRUCT FAR *) lParam;
if(nCode>=0)
{
if(wParam==WM_MOUSEMOVE||wParam==WM_NCMOUSEMOVE||wParam==WM_LBUTTONDOWN||wParam==WM_RBUTTONDOWN||wParam==WM_LBUTTONUP||wParam==WM_RBUTTONUP)//右键按下
{
for(int i=1;i<32;i++)
{
if(FollowWin[i]) ::PostMessage(FollowWin[i],wParam,0,MAKELPARAM(pMouseHook->pt.x,pMouseHook->pt.y));
else break;
}
}
}
return CallNextHookEx(mkb,nCode,wParam,lParam);
}
///安装钩子
BOOL __declspec(dllexport)__stdcall installhook(HWND *Wnd)
{
if(Wnd[0])MainWin=Wnd[0];
unsigned long mainwid=::GetWindowThreadProcessId(MainWin,NULL);
//hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)K_Hook,hins,0);
//mkb=SetWindowsHookEx(WH_MOUSE,(HOOKPROC)M_Hook,hins,0);
hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)K_Hook,hins,mainwid);
mkb=SetWindowsHookEx(WH_MOUSE,(HOOKPROC)M_Hook,hins,mainwid);
int i=1;
while(Wnd[i])//记录所有句柄,脚码0为主控窗体句柄
{
FollowWin[i-1]=Wnd[i];
if(i>=31)
{
break;
}i++;
}
if(hkb&&mkb) return TRUE;
return FALSE;
}
///卸载钩子
BOOL __declspec(dllexport) UnHook()
{
BOOL unkhooked = UnhookWindowsHookEx(hkb);
BOOL unmhooked = UnhookWindowsHookEx(mkb);
return unkhooked&&unmhooked;
}
///用于初始化de封装类
BOOL CHodllApp::InitInstance ()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
hins=AfxGetInstanceHandle(); GetModuleFileName(AfxGetInstanceHandle( ),szBuf,sizeof(szBuf));
p = szBuf;
while(strchr(p,'\\'))
{
p = strchr(p,'\\');
p++;
}
*p = '\0';
msg=szBuf;
return TRUE;
}
BOOL CHodllApp::ExitInstance ()
{
return TRUE;
}
CHodllApp::CHodllApp()
{
}
CHodllApp theApp;
///共享数据段
#pragma data_seg(".SHARDAT")
static HHOOK hkb=NULL;
static HHOOK mkb=NULL;
static HWND FollowWin[32]={NULL};
#pragma data_seg()
HINSTANCE hins;//dll实例句柄
static HWND MainWin=NULL;
char szBuf[256];
char *p;
CString msg;
////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CHodllApp, CWinApp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
/////钩子函数
LRESULT __declspec(dllexport)__stdcall CALLBACK K_Hook(int nCode,WPARAM wParam,LPARAM lParam)
{
if(HC_ACTION==nCode)
{
if(((DWORD)lParam&0x40000000))//按下
{
for(int i=1;i<32;i++)
{
if(FollowWin[i]) ::PostMessage(FollowWin[i],WM_KEYDOWN,wParam,0);
else break;
}
}
else
{
for(int i=1;i<32;i++)
{
if(FollowWin[i]) ::PostMessage(FollowWin[i],WM_KEYUP,wParam,0);
else break;
}
}
}LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam );
return RetVal;
}LRESULT __declspec(dllexport)__stdcall CALLBACK M_Hook(int nCode,WPARAM wParam,LPARAM lParam)
{ LPMOUSEHOOKSTRUCT pMouseHook=(MOUSEHOOKSTRUCT FAR *) lParam;
if(nCode>=0)
{
if(wParam==WM_MOUSEMOVE||wParam==WM_NCMOUSEMOVE||wParam==WM_LBUTTONDOWN||wParam==WM_RBUTTONDOWN||wParam==WM_LBUTTONUP||wParam==WM_RBUTTONUP)//右键按下
{
for(int i=1;i<32;i++)
{
if(FollowWin[i]) ::PostMessage(FollowWin[i],wParam,0,MAKELPARAM(pMouseHook->pt.x,pMouseHook->pt.y));
else break;
}
}
}
return CallNextHookEx(mkb,nCode,wParam,lParam);
}
///安装钩子
BOOL __declspec(dllexport)__stdcall installhook(HWND *Wnd)
{
if(Wnd[0])MainWin=Wnd[0];
unsigned long mainwid=::GetWindowThreadProcessId(MainWin,NULL);
//hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)K_Hook,hins,0);
//mkb=SetWindowsHookEx(WH_MOUSE,(HOOKPROC)M_Hook,hins,0);
hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)K_Hook,hins,mainwid);
mkb=SetWindowsHookEx(WH_MOUSE,(HOOKPROC)M_Hook,hins,mainwid);
int i=1;while(Wnd[i])//记录所有句柄,脚码0为主控窗体句柄
{
FollowWin[i-1]=Wnd[i];
if(i>=31)
{
break;
}i++;
}
if(hkb&&mkb) return TRUE;
return FALSE;
}
///卸载钩子
BOOL __declspec(dllexport) UnHook()
{
BOOL unkhooked = UnhookWindowsHookEx(hkb);
BOOL unmhooked = UnhookWindowsHookEx(mkb);
return unkhooked&&unmhooked;
}
普通的程序直接加断点就可以调试了.这个貌似不行.
但求调试dll方法.
普通的程序直接加断点就可以调试了.这个貌似不行.
但求调试dll方法.attach到目标进程,也就是被你hook住的进程,然后在你的dll代码中下断点,这样就能断下来。