动态加载DLL问题 DLL加载成功,可是为什么找不报error C3861: “Add”: 找不到标识符
/////////////////MyDll1.h////////////////////////////////////////////////#ifdefMYLIBA...
///
////////////// MyDll1.h ////////////////////////////////////////////////
#ifdef MYLIBAPP
#else
#define MYLIBAPP extern "C" __declspec(dllimport)
#endif
MYLIBAPP int g_nResult;
MYLIBAPP int Add(int nLeft,int nRight);
MYLIBAPP int Sub(int nLeft,int nRight);
///MyDll1.CPP///////////////////////////////////////
#include <Windows.h>
#define MYLIBAPP extern "C" __declspec(dllexport)
#include "MyDll1.h"
int g_nResult;
int Add(int nLeft,int nRight)
{
g_nResult = nLeft+nRight;
return g_nResult;
}
int Sub(int nLeft,int nRight)
{
g_nResult = nLeft -nRight;
return g_nResult;
}
/////////可执行文件
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
BOOL LibFlag;
int nLeft = 10,nRight = 20,nResult = 0;
HINSTANCE hInst = LoadLibrary(L"MyDll1.dll");
if(hInst == NULL)
{
GetProcAddress(hInst,"MyDll1.dll");
}
else
{
MessageBox(NULL,L"LoadLaibrary Succeed",L"OK",MB_OK);
}
nResult = Add(nLeft,nRight);
cout<<"The Result is :"<<nResult<<endl;
LibFlag = FreeLibrary(hInst);
if(LibFlag == TRUE)
{
MessageBox(NULL,L"FreeLibrary Succeed",L"OK",MB_OK);
}
else
{
MessageBox(NULL,L"FreeLibrary Failed",L"Error",MB_OK);
}
return 0;
}
PS:MyDll1.dlllk加载成功,亲并放在当前可执行文件目录下 展开
////////////// MyDll1.h ////////////////////////////////////////////////
#ifdef MYLIBAPP
#else
#define MYLIBAPP extern "C" __declspec(dllimport)
#endif
MYLIBAPP int g_nResult;
MYLIBAPP int Add(int nLeft,int nRight);
MYLIBAPP int Sub(int nLeft,int nRight);
///MyDll1.CPP///////////////////////////////////////
#include <Windows.h>
#define MYLIBAPP extern "C" __declspec(dllexport)
#include "MyDll1.h"
int g_nResult;
int Add(int nLeft,int nRight)
{
g_nResult = nLeft+nRight;
return g_nResult;
}
int Sub(int nLeft,int nRight)
{
g_nResult = nLeft -nRight;
return g_nResult;
}
/////////可执行文件
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
BOOL LibFlag;
int nLeft = 10,nRight = 20,nResult = 0;
HINSTANCE hInst = LoadLibrary(L"MyDll1.dll");
if(hInst == NULL)
{
GetProcAddress(hInst,"MyDll1.dll");
}
else
{
MessageBox(NULL,L"LoadLaibrary Succeed",L"OK",MB_OK);
}
nResult = Add(nLeft,nRight);
cout<<"The Result is :"<<nResult<<endl;
LibFlag = FreeLibrary(hInst);
if(LibFlag == TRUE)
{
MessageBox(NULL,L"FreeLibrary Succeed",L"OK",MB_OK);
}
else
{
MessageBox(NULL,L"FreeLibrary Failed",L"Error",MB_OK);
}
return 0;
}
PS:MyDll1.dlllk加载成功,亲并放在当前可执行文件目录下 展开
展开全部
可执行文件:
typedef int (WINAPIV *pfn_Add)( int, int );
int main()
{
....
pfn_Add Add;
....
Add = (pfn_Add)GetProcAddress(hInst,"Add");
....
nResult = (*Add)(nLeft,nRight);
}
typedef int (WINAPIV *pfn_Add)( int, int );
int main()
{
....
pfn_Add Add;
....
Add = (pfn_Add)GetProcAddress(hInst,"Add");
....
nResult = (*Add)(nLeft,nRight);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
GetProcAddress(hInst,"MyDll1.dll");第二个参数是你要调用的函数名称,而不是动态库的名称,应该改成GetProcAddress(hInst,"Add")就对了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在exe程序里面加上#include "MyDll1.h"试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询