Win32API インポートアドレステーブルに書かれているモジュール名及び関数名を取得する
インポートアドレステーブルに書かれているモジュール名とそのモジュールに属する関数名を取得し表示する。
#include <windows.h> #include <tchar.h> #include <dbghelp.h> #include <string> #include <stdlib.h> #pragma comment(lib, "dbghelp.lib") int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); ULONG ulSize; //.idataセクションの先頭のIMAGE_IMPORT_DESCRIPTOR構造体 PIMAGE_IMPORT_DESCRIPTOR pImportDesc; //自分自身のモジュールハンドル HMODULE hmodCaller; //自分自身の実行ファイル名 TCHAR szMyExecuteName[MAX_PATH]; GetModuleFileName(NULL, szMyExecuteName, _countof(szMyExecuteName)); //自分自身のモジュールハンドルを取得 hmodCaller = ::GetModuleHandleW(szMyExecuteName); //IMAGE_IMPORT_DESCRIPTOR構造体へのポインタを取得 pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR) ImageDirectoryEntryToData(hmodCaller, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &ulSize); ///////////////////////////// std::string s; while (pImportDesc->OriginalFirstThunk) { //DLL名を取得 PSTR pszDllName = (PSTR)((PBYTE)(hmodCaller) + pImportDesc->Name); s += pszDllName; s += '\n'; PIMAGE_THUNK_DATA pThunkData = (PIMAGE_THUNK_DATA)((PBYTE)hmodCaller + pImportDesc->OriginalFirstThunk); while (pThunkData->u1.AddressOfData) { if (IMAGE_SNAP_BY_ORDINAL(pThunkData->u1.Ordinal)) { continue; } PIMAGE_IMPORT_BY_NAME pImageImportByName = (PIMAGE_IMPORT_BY_NAME)((PBYTE)hmodCaller + pThunkData->u1.AddressOfData); //関数名を取得 PSTR pszFuncName = (PSTR)&pImageImportByName->Name[0]; s += pszFuncName; s += ','; pThunkData++; } s += "\n\n\n"; pImportDesc++; } MessageBoxA(GetActiveWindow(), s.c_str(), "Import Address Table", MB_OK); return 0; }