---math.h文件
#include <string>
extern "C" __declspec(dllexport) int pascal Test(int i,int j);
//extern "C" class CMath
//{
//public:
// CMath(void);
//  __declspec(dllexport) int pascal Add(int i,int j);
// ~CMath(void);
//};
---math.cpp文件
#include "StdAfx.h"
#include "Math.h"
int pascal Test(int i,int j)
{
return i+j;
}VS2010,编译生成了firt.dll文件-----------------------client----------------------
-------------test.cpp--------------
#include<iostream>
#include<windows.h>
typedef int  ( _stdcall *FUN)(int,int);
int main(void)
{
HINSTANCE  hinst=::LoadLibraryA("first.dll");
if(NULL != hinst)
{
std::cout<<"DLL is Load"<<std::endl;
FUN f=(FUN)GetProcAddress(hinst,"1");
if(f==NULL)
{
std::cout<<"Error"<<std::endl;

}
else
{
int k=f(2,3);
std::cout<<k<<std::endl;
}
}
else
{
std::cout<<"Dll isn't Load"<<std::endl;
}
std::cin.get();
}
loadlibrary能成功,但是GetProcAddress总是返回NULL。请问为什么?

解决方案 »

  1.   

    GetProcAddress要填导出函数的名字
      

  2.   

    FUN f=(FUN)GetProcAddress(hinst,"1");  发错,
    FUN f=(FUN)GetProcAddress(hinst,"Test");  也返回NULL。我在网上看到别人说用导出函数的序号,所以我把Test换成了1  还是不行。
    初学
      

  3.   

    通过Dependency工具显示函数名为_Test@8
      

  4.   

    我刚刚用_Test@8替换Test正常了。有什么办法让直接用Test就能返回函数的指针。
      

  5.   

    不要用__declspec(dllexport)
    用def文件或者link的/export参数
      

  6.   


    改了之后还是_Test@8,若是用DEF文件来定义导出函数,则为Test。觉得以后还是用DEF文件来处理导出函数名称。问题虽然解决了,但是我还是不知道为什么我加了extern "C"修饰符之后,编译器编译出来的导出函数还是_Test@8   求大牛解释。
      

  7.   

    _Test@8 就是vc的c风格的修饰名
    你不加extern "C"更乱