__declspec(dllexport)
& __declspec(dllimport)
1.使用 __declspec(dllexport) 從 DLL 導出
Microsoft 在 Visual C++ 的 16 位編譯器版本中引入了 __export,使編譯器得以自動生成導出名並將它們放到一個 .lib 文件中。然後,此 .lib 文件就可以像靜態 .lib 那樣用於與 DLL 鏈接.
2.聲明一個導入函數,是說這個函數是從別的DLL導入。我要用。一般用於使用某個dll的exe中
3.以下摘自<window核心編成>:
下面的頭文件被DLL和EXE文件同時包括:
/***************************************************************************
Module: MyLib.h
***************************************************************************/
#ifdef MYLIBAPI
// MYLIBAPI 應該在要導出的DLL文件中定義
// code modules before this header file is included.
// All functions/variables are being exported.
#else
// 當頭文件被EXE文件引用此處定義EXE文件要導入的函數和變量
#define MYLIBAPI extern "C" __declspec(dllimport)
#endif
////////////////////////////////////////////////////////////////////////////
// Define any data structures and symbols here.
////////////////////////////////////////////////////////////////////////////
// 定義導出變量
MYLIBAPI int g_nResult;
////////////////////////////////////////////////////////////////////////////
// 此處定義導出函數原形
MYLIBAPI int Add(int nLeft, int nRight);
////////////////////////////// End of File /////////////////////////////////
/***************************************************************************
Module: MyLibFile1.cpp
***************************************************************************/
#include <windows.h>
// This DLL source code file exports functions and variables.
#define MYLIBAPI extern "C" __declspec(dllexport)
// Include the exported data structures, symbols, functions, and variables.
#include "MyLib.h"
// Place the code for this DLL source code file here.
int g_nResult;
int Add(int nLeft, int nRight) {
g_nResult = nLeft + nRight;
return(g_nResult);
}
MyLibFile1.cpp文件中的導出函數和變量前沒有MYLIBAPI修飾原因是編輯器在解析頭文件的時候會記住應該導出的函數和變量.
4.何爲導出:
window編輯器在看到__declspec(dllexport)修飾的變量或者函數或類時候,會在生成的。OBJ 文件中嵌入一些額外的信息,當連接器在鏈接DLL所有OBJ文件時,會解析這些信息,並聲稱一個LIB文件,該LIB文件列出所有該DLL文件到處的符號(symbol),同時連接器還會在生成DLL文件中嵌入一個導出符號表(export section)
可以使用dumpbin.exe運行 查看DLL文件中的導出段