AfxLoadLibrary
使用AfxLoadLibrary ,將 DLL 模組對應。
HINSTANCE AFXAPI AfxLoadLibrary(
LPCTSTR lpszModuleName
);
參數
lpszModuleName
指到 null 結尾字串,包含該模組的名稱 (不論是哪一。DLL 或。EXE 檔)。 指定的名稱是模組的檔名。如果字串是指定的路徑,但檔案不存在於指定的目錄,則功能將會失敗。
如果未指定路徑,並省略副檔名時,預設副檔名。會加入 DLL。 不過,此檔案名稱字串可包含後面加上點字元 (.),表示模組名稱沒有副檔名。 沒有指定路徑,該函式會搜尋下列程序中的檔案:
載入應用程式目錄。
目前的目錄。
Windows 95/98: 的 Windows 系統目錄。 Windows NT: 32 位元的 Windows 系統目錄。 這個目錄的名稱是 SYSTEM32。
只有 Windows NT: 的 16 位元的 Windows 系統目錄。 取得這個目錄中,路徑的 Win32 函式,但它會搜尋。 這個目錄的名稱是系統。
Windows 目錄。
PATH 環境變數中所列的目錄。
傳回值
如果函式成功,傳回的值會是模組控制代碼。 如果函式失敗,則傳回值為 NULL。
備註
它會傳回的控制代碼,可以用於 GetProcAddress 以取得 DLL 函式的位址。 AfxLoadLibrary也可以用來對應其他可執行檔的模組。
每個處理序會維護每個載入的程式庫模組的參考計數。 此參考次數就遞增一次AfxLoadLibrary稱為,也會減少每次AfxFreeLibrary呼叫。 當參考計數到達零時,模組不相符的呼叫程序的位址空間,並控制代碼不再有效。
務必使用AfxLoadLibrary和AfxFreeLibrary (而非 Win32 函式 LoadLibrary 和 FreeLibrary) 應用程式使用多個執行緒,並以動態方式載入擴充 DLL。 使用AfxLoadLibrary和AfxFreeLibrary確保擴充 DLL 會載入和卸載時所執行的啟動和關閉程式碼而損毀的全域 MFC 狀態。
使用AfxLoadLibrary在應用程式會要求您以動態方式連結的 MFC DLL 版本。 標頭檔AfxLoadLibrary,Afxdll_.h,只會包含如果 MFC 連結至為 DLL 的應用程式。 這是原本設計,因為您必須連結到的 MFC DLL 版本來使用,或建立的擴充 Dll。
範例
// The following shows how to create a MDI based application
// using a generic CView derived class that is implemented in
// a dynamically loaded MFC Extension DLL.
typedef CRuntimeClass * (*GETDLLVIEW)();
BOOL CUserApp::InitInstance()
{
// Standard Application Wizard generated initialization excluded.
...
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views
//Load MFC Extension DLL based view class.
m_hViewDll = AfxLoadLibrary(szMyViewDllPath);
if (!m_hViewDll)
{
CString str;
str.Format(_T("Error: Cannot find component %s"), szMyViewDllPath);
AfxMessageBox(str);
return FALSE;
}
GETDLLVIEW GetMyView = (GETDLLVIEW)GetProcAddress(m_hViewDll, "GetMyView");
ASSERT(GetMyView != NULL);
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(IDR_NVC_MFC_DLLUserTYPE,
RUNTIME_CLASS(CUserDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
GetMyView());
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
// Standard Application Wizard generated initalization excluded.
...
return TRUE;
}
int CUserApp::ExitInstance()
{
if (NULL != m_hViewDll)
{
AfxFreeLibrary(m_hViewDll);
m_hViewDll = NULL;
}
return CWinApp::ExitInstance();
}
需求
標頭: afxdll_.h