今天做了一个下午的HOOK,要做一个有记忆键盘功能的全局钩子,没想到搞半天做了一个乱七八糟的东西出来,哎..有点遗憾.
CFile file("1.txt",CFile::modeRead);
fileLength = file.GetLength();
pBuf = new char[fileLength+2];
pBuf[fileLength] = 0;
file.Read(pBuf,fileLength);
file.Close();
switch (wParam)
{
case 'A':
{
// strcpy(ch,"a");
strcat(pBuf,"a");
// strcpy(pBuf,"a");
CFile file("1.txt",CFile::modeWrite);
file.Write(pBuf,fileLength+2);
// file.Write(ch,1);
file.Close();
break;
}
case 'B':
{
ch[0] = 'b';
ch[1] = 0;
strcat(pBuf,ch);
CFile file("1.txt",CFile::modeWrite);
file.Write(pBuf,fileLength+2);
file.Close();
break;
}
case 'C':
{
ch[0] = 'c';
ch[1] = 0;
strcat(pBuf,ch);
CFile file("1.txt",CFile::modeWrite);
file.Write(pBuf,fileLength+2);
file.Close();
break;
}
case 'D':
{
ch[0] = 'd';
ch[1] = 0;
strcat(pBuf,ch);
CFile file("1.txt",CFile::modeWrite);
file.Write(pBuf,fileLength+2);
file.Close();
break;
}
case 'E':
{
ch[0] = 'e';
ch[1] = 0;
strcat(pBuf,ch);
CFile file("1.txt",CFile::modeWrite);
file.Write(pBuf,fileLength+2);
file.Close();
break;
}
case 'F':
{
ch[0] = 'f';
ch[1] = 0;
strcat(pBuf,ch);
CFile file("1.txt",CFile::modeWrite);
file.Write(pBuf,fileLength+2);
file.Close();
break;
}
}
delete pBuf;
if(VK_F2==wParam)
{
// ::SendMessage(g_hWnd,WM_CLOSE,0,0);
::UnhookWindowsHookEx(g_hMouse);
::UnhookWindowsHookEx(g_hKeyboard);
}
return CallNextHookEx(g_hKeyboard,code,wParam,lParam);
}
这是动态链接库中,对于键盘操作的代码.
非常奇怪的是,使用strcat函数,最后竟然一次向文本输入两个同样的字符.例如,要在键盘上输入a,则最后打开文本一看,那上面竟然是aa.就这个问题我找了一个下午,但还不知道为什么.太神奇了.不管他了.
还有一个是我给键盘安装的是一个全局钩子,但是这个钩子在其他地方不管用了.真的很奇怪.比如在记事本上输入字母,则完全无法记忆.什么东西这是.
看来还得看看其他资料.
有高兴一点的事情是,昨天晚上有FindWindow找到了domodal出来的窗口句柄,实现通讯软件中的组播与多播.
SOCKADDR_IN addFrom;
char recvBuf[200];
char tempBuf[300];
int len = sizeof(SOCKADDR);
int teval;
while(TRUE)
{
teval = recvfrom(sock,recvBuf,200,0,(SOCKADDR*)&addFrom,&len);
if (teval == SOCKET_ERROR)
{
break;
}
sprintf(tempBuf,"%s say: %s",inet_ntoa(addFrom.sin_addr),recvBuf);
// AfxMessageBox(tempBuf);
HWND chwnd = ::FindWindow(NULL,"Dialog");
::PostMessage(chwnd,WM_RECVDATA,0,(LPARAM)tempBuf);
}
return 0;
}
继续学习,继续努力!