__declspec(dllexport)
在应用程序中要想使用这些导出函数,也需要使用关键字来声明要使用的DLL导出函数:
__declspec(dllimport)
例如:
//dll_1.cpp
#include "stdafx.h"
#include
#include "dll_1.h"
#define EXPORTING_DLL
int printmyname(bool a)
{
if(a)
printf("hi jack!");
else
printf("printfmyname need TRUE");
return 0;
}
//dll_1.h
#ifndef DLL_1_H
#define DLL_1_H
#ifdef EXPORTING_DLL
extern __declspec(dllexport) int printmyname(bool a);
#else
extern __declspec(dllimport) int printmyname(bool a);
#endif
#endif
2. 我们还可以使用模块定义文件来声明DLL导出函数,这样,我们就不需要向DLL导出函数中添加关键字了。如:
// myDLL.def
//
LIBRARY "myDLL"
EXPORTS
HelloWorld
示例DLL和应用程序
这是一个在Visual C++中用“Win32动态链接库”项目类型创建的示例程序。
// myDLL.cpp
//
#include "stdafx.h"
#define EXPORTING_DLL
#include "sampleDLL.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
void HelloWorld()
{
MessageBox( NULL, TEXT("Hello World"), TEXT("In a DLL"), MB_OK);
}
// File: myDLL.h
//
#ifndef INDLL_H
#define INDLL_H
#ifdef EXPORTING_DLL
extern __declspec(dllexport) void HelloWorld() ;
#else
extern __declspec(dllimport) void HelloWorld() ;
#endif
#endif
下面的程序是一个win32应用程序,该程序调用myDLL中的导出函数HelloWorld。
// myApp.cpp
//
#include "stdafx.h"
#include "myDLL.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HelloWorld();
return 0;
}
注意:在加载时动态链接中,必须链接在生成myDLL项目时创建的myDLL.lib引入库。
在运行时动态链接中,应该这样调用myDLL.dll中的导出函数:
...
typedef VOID (*DLLPROC) (LPTSTR);
...
HINSTANCE hinstDLL;
DLLPROC HelloWorld;
BOOL fFreeDLL;
hinstDLL = LoadLibrary("myDLL.dll");
if (hinstDLL != NULL)
{
HelloWorld = (DLLPROC) GetProcAddress(hinstDLL, "HelloWorld");
if (HelloWorld != NULL)
(HelloWorld);
fFreeDLL = FreeLibrary(hinstDLL);
}
...
这只是个简单的例子,但大致的使用方法就是这样。
<p _extended="true"><strong _extended="true">visual studio 2005有点旧了...2010都出了! 建议用vs2008</strong></p> <p _extended="true"><strong _extended="true">微软官方msdn下载!</strong></p> <p _extended="true"><a