呼叫 DbgHelp 函式庫
雖然 DbgHelp.dll 隨附於所有版本的 Windows,但呼叫端應考慮使用較新的 DLL 版本,如 Windows 套件中的 偵錯工具所提供的版本。 如需 DbgHelp 發行的詳細資訊,請參閱 DbgHelp 版本。
使用 DbgHelp 時,最佳策略是在應用程式目錄中安裝來自 Windows 偵錯工具 套件的函式庫複本,使其在程式結構上與呼叫此庫的軟體相鄰。 如果需要符號伺服器和來源伺服器,則 SymSrv.dll 和 SrcSrv.dll 都必須安裝在與 DbgHelp.dll相同的目錄中,因為 DbgHelp 只有在與該 DLL 共用相同的目錄時,才會呼叫這些 DLL。 (請注意,DbgHelp 不會從標準搜尋路徑呼叫這兩個 DLL。這有助於防止使用不相符的 DLL;同樣地,它也會改善整體的安全性。
下列程式代碼是從 DbgHelp 來源擷取。 它示範 DbgHelp 如何只從 DbgHelp.dll 所在的相同目錄載入 SymSrv.dll 和 SrcSrv.dll 版本。
HINSTANCE ghinst;
// For calculating the size of arrays for safe string functions.
#ifndef cch
#define ccht(Array, EltType) (sizeof(Array) / sizeof(EltType))
#define cch(Array) ccht(Array, (Array)[0])
#endif
//
// LoadLibrary() a DLL, using the same directory as dbghelp.dll.
//
HMODULE
LoadDLL(
__in PCWSTR filename
)
{
WCHAR drive[10] = L"";
WCHAR dir[MAX_PATH + 1] = L"";
WCHAR file[MAX_PATH + 1] = L"";
WCHAR ext[MAX_PATH + 1] = L"";
WCHAR path[MAX_PATH + 1] = L"";
HMODULE hm;
// Chop up 'filename' into its elements.
_wsplitpath_s(filename, drive, cch(drive), dir, cch(dir), file, cch(file), ext, cch(ext));
// If 'filename' contains no path information, then get the path to our module and
// use it to create a fully qualified path to the module we are loading. Then load it.
if (!*drive && !*dir)
{
// ghinst is the HINSTANCE of this module, initialized in DllMain or WinMain
if (GetModuleFileNameW(ghinst, path, MAX_PATH))
{
_wsplitpath_s(path, drive, cch(drive), dir, cch(dir), NULL, 0, NULL, 0);
if (*drive || *dir)
{
swprintf_s(path, cch(path), L"%s%s%s%s", drive, dir, file, ext);
hm = LoadLibrary(path);
if (hm)
return hm;
}
}
}
else
{
// If we wanted to, we could have LoadDLL also support directories being specified
// in 'filename'. We could pass the path here. The result is if no path is specified,
// the module path is used as above, otherwise the path in 'filename' is specified.
// But the standard search logic of LoadLibrary is still avoided.
/*
hm = LoadLibrary(path);
if (hm)
return hm;
*/
}
return 0;
}
載入這兩個 DLL 之後,DbgHelp 會呼叫 GetProcAddress,以從它們取得所需的函式。
一般而言,呼叫 DbgHelp.dll 的程式代碼會藉由將 DbgHelp.dll 安裝到與起始目前進程之應用程式的相同目錄中,以確保載入正確的版本。 如果呼叫程式代碼位於 DLL 中,且無法存取或瞭解初始程式的位置,則 DbgHelp.dll 必須與呼叫 DLL 一起安裝,而且應該使用與 DbgHelp 的 LoadDLL 類似的程式代碼。
相關主題