65,211
社区成员
发帖
与我相关
我的任务
分享// DLL下的.h
#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllexport)
#endif
MYLIBAPI int g_nResult;
MYLIBAPI int Add(int nLeft,int nRight);
//DLL下的CPP文件
#include <windows.h>
#define MYLIBAPI extern "C" __declspec(dllexport)
#include "MyLib.h"
int g_nResult;
int Add(int nLeft,int nRight)
{
return g_nResult=nLeft+nRight;
}
//exe端的cpp文件
#include <windows.h>
//#include <strsafe.h>
#include <stdio.h>
#include <stdlib.h>
#include "D:\Test\TEST\Dll\MyLib.h"
void main()
{
int nLeft=10,nRight=25;
printf("The 10+25=%d\n and the g_nResult=%d",Add(nLeft,nRight),g_nResult);
}
还要把那个.dll文件拷贝到.exe工程目录下!!!!
#pragma comment( lib, "D:\\Test\\TEST\\Dll\\debug\\MyLib.lib" )
没有这句,你只包含个头文件咋弄??
// DLL下的.h
#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllexport)
#endif
MYLIBAPI int g_nResult;
MYLIBAPI int Add(int nLeft,int nRight);
//DLL下的CPP文件
#include <windows.h>
#define MYLIBAPI extern "C" __declspec(dllexport)
#include "MyLib.h"
int g_nResult;
int Add(int nLeft,int nRight)
{
return g_nResult=nLeft+nRight;
}
//exe端的cpp文件
#include <windows.h>
//#include <strsafe.h>
#include <stdio.h>
#include <stdlib.h>
#include "D:\Test\TEST\Dll\MyLib.h"
#pragma comment( lib, "D:\\Test\\TEST\\Dll\\debug\\MyLib.lib" )
void main()
{
int nLeft=10,nRight=25;
printf("The 10+25=%d\n and the g_nResult=%d",Add(nLeft,nRight),g_nResult);
}
呵呵,不好意思咯!
// DLL下的.h
#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllimport)
#endif
MYLIBAPI int g_nResult;
MYLIBAPI int Add(int nLeft,int nRight);
//DLL下的CPP文件
#include <windows.h>
#define MYLIBAPI extern "C" __declspec(dllimport)
#include "MyLib.h"
int g_nResult;
int Add(int nLeft,int nRight)
{
return g_nResult=nLeft+nRight;
}
//exe端的cpp文件
#include <windows.h>
//#include <strsafe.h>
#include <stdio.h>
#include <stdlib.h>
#include "D:\Test\TEST\Dll\MyLib.h"
void main()
{
int nLeft=10,nRight=25;
printf("The 10+25=%d\n and the g_nResult=%d",Add(nLeft,nRight),g_nResult);
}
既然是隐式链接,那么:
#define MYLIBAPI extern "C" __declspec(dllexport)
改为:
#define MYLIBAPI extern "C" __declspec(dllimport)
两处都得改~~
改了就好使!
#include <windows.h>
//#include <strsafe.h>
#include <stdio.h>
#include <stdlib.h>
//#include "C:\Documents and Settings\Administrator\桌面\test\dll\dll.h"
typedef int (*padd)(int,int);
void main()
{
int nLeft=10,nRight=25;
HMODULE module = ::LoadLibrary("C:\\Documents and Settings\\Administrator\\桌面\\test\\dll\\Debug\\dll.dll");
padd pa = (padd)GetProcAddress(module,"Add");
printf("The 10+25=%d\n ",pa(nLeft,nRight));
}