使用 __declspec(dllimport) 匯入資料

在資料情況下,使用 __declspec(dllimport) 是移除間接傳遞層的便利項目。您仍然需要在從 DLL 匯入資料時檢視匯入位址表。在 __declspec(dllimport) 之前,這是指在存取從 DLL 匯出的資料時,您必須記得要執行其他的間接傳遞層級:

// project.h
#ifdef _DLL   // If accessing the data from inside the DLL
   ULONG ulDataInDll;

#else         // If accessing the data from outside the DLL
   ULONG *ulDataInDll;
#endif

接著您可以在您的.DEF 檔裡匯出資料:

// project.def
LIBRARY project
EXPORTS
   ulDataInDll   CONSTANT

並且由 DLL 的外部存取:

if (*ulDataInDll == 0L) 
{
   // Do stuff here
}

編譯器會在您將資料標記成 __declspec(dllimport) 自動為您產生間接傳遞程式碼。您無須擔心上述步驟。如同前述,建置 DLL 時請勿在資料上使用 __declspec(dllimport) 宣告。DLL 內的函式不會使用匯入位址表來存取資料物件;因此,不會有其他的間接傳遞層級存在。

若要從 DLL 自動匯出資料,請使用這個宣告:

__declspec(dllexport) ULONG ulDataInDLL;

請參閱

概念

匯入至應用程式