DbgHelp ライブラリの呼び出し

DbgHelp.dllすべてのバージョンの Windows に付属していますが、呼び出し元は、 Windows 用デバッグ ツール パッケージに記載されている、この DLL の最新バージョンの 1 つを使用することを検討する必要があります。 DbgHelp の配布の詳細については、 DbgHelp のバージョンを参照してください。

DbgHelp を使用する場合、最適な方法は、 デバッグ ツール for Windows パッケージからライブラリのコピーを、それを呼び出すソフトウェアに論理的に隣接するアプリケーション ディレクトリにインストールすることです。 シンボル サーバーとソース サーバーも必要な場合は、SymSrv.dllとSrcSrv.dllの両方をDbgHelp.dllと同じディレクトリにインストールする必要があります。DbgHelp は、同じディレクトリを共有する場合にのみこれらの DLL を呼び出します。 (DbgHelp は、標準の検索パスからこれら 2 つの 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;
}

これら 2 つの DLL を読み込んだ後、DbgHelp は GetProcAddress を呼び出して、必要な関数を取得します。

通常、DbgHelp.dllを呼び出すコードでは、現在のプロセスを開始したアプリケーションと同じディレクトリにDbgHelp.dllをインストールすることで、正しいバージョンが読み込まれます。 呼び出し元のコードが DLL 内にあり、最初のプロセスの場所にアクセスできない場合、または最初のプロセスの場所に関する知識がない場合は、呼び出し元の DLL と共にDbgHelp.dllをインストールし、DbgHelp の LoadDLL と同様のコードを使用する必要があります。

DbgHelp のバージョン

LoadLibrary

GetProcAddress

GetModuleFileName