打开VC 新建 工程 Win32 Dynamic~Link Library 名字“MakeDll”
新建class MakeDll
MakeDll.h
// MakeDLL.h: interface for the MakeDLL class.
#if !defined(AFX_MAKEDLL_H__AEA598EA_1138_4F09_A882_F655B1DEA725__INCLUDED_)
#define AFX_MAKEDLL_H__AEA598EA_1138_4F09_A882_F655B1DEA725__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
extern "C" _declspec(dllexport) int sum(int a,int b);//
class MakeDLL
{
public:
MakeDLL();
virtual ~MakeDLL();
};
#endif // !defined(AFX_MAKEDLL_H__AEA598EA_1138_4F09_A882_F655B1DEA725__INCLUDED_)
MakeDll.cpp
#include "MakeDLL.h"
//
#include "windows.h"
BOOL APIENTRY DllMain(
HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
extern "C" _declspec(dllexport)int sum(int a, int b)
{
return a+b;
}
//
MakeDLL::MakeDLL()
{
}
MakeDLL::~MakeDLL()
{
}
编译生成MakeDll.dll文件
调用DLL :
新建工程 Wind32 Console Application
DllTest
DllTest.h
#if !defined(AFX_DLLTEST_H__E15FF510_5A00_4C68_9A3F_E34173E64BBC__INCLUDED_)
#define AFX_DLLTEST_H__E15FF510_5A00_4C68_9A3F_E34173E64BBC__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class DllTest
{
public:
DllTest();
virtual ~DllTest();
};
#endif // !defined(AFX_DLLTEST_H__E15FF510_5A00_4C68_9A3F_E34173E64BBC__INCLUDED_)
DllTest.cpp#include "DllTest.h"
#include "stdio.h"
#include<windows.h>
//
void main(void)
{
HINSTANCE hDLL;
hDLL=LoadLibrary("MakeDll.dll");//加载动态链接库MyDll.dll文件;
typedef int( *pSum)(int a,int b);
pSum Sum ;
//=
Sum=(pSum)GetProcAddress(hDLL,"sum");
int a = Sum(5,8);
printf("%d",a);
}
//
DllTest::DllTest()
{
}
DllTest::~DllTest()
{
}
把 MakeDll.dll 拷贝到 DllTest 文件夹下
编译运行
结果 13 over
VC生成DLL并且调用DLL文件
最新推荐文章于 2022-02-25 19:38:17 发布