如何确定哪个 Windows DLL 用于函数调用

How can I determine which Windows DLL is being used for a function call?

本文关键字:DLL 用于 函数调用 Windows 何确定      更新时间:2023-10-16

我一直在研究_vsnprintf,并了解到它在ntdll.dll和msvcrt.dll中可用。

我可以使用GetModuleHandleGetProcAddress来访问_vsnprintf,例如:

static int(__cdecl *p__vsnprintf)(char *str, size_t count, const char *format, va_list valist);
static void init(const char *dll)
{
    HMODULE hmod = GetModuleHandleA(dll);
    if (hmod)
    {
        printf("*** Testing %s ***n", dll);
        p__vsnprintf = (void *)GetProcAddress(hmod, "_vsnprintf");
        if (p__vsnprintf) test__vsnprintf();
        else printf("_vsnprintf not found in %s.n", dll);
    }
    else printf("*** Unable to load %s ***n", dll);
    printf("n");
}
int main(void)
{
    init("ntdll.dll"); /* ntdll _vsnprintf */
    init("msvcrt.dll"); /* msvcrt _vsnprintf */
    printf("*** Testing normal function call ***n");
    test_vsnprintf(); /* _vsnprintf in ??? */
    return 0;
}

对于通用调用,如何判断 Windows 使用的是来自 ntdll.dll 还是 msvcrt.dll 的_vsnprintf

dumpbin /imports会告诉你。 此外,方便的depends实用程序。

要务实地做到这一点,您有两个主要选项:

  1. 如果是静态导入,则可以对 IAT 进行探测并检查其导入的模块。
  2. 如果你是动态的(即:使用GetProcAddress),你可以使用VirtualQueryGetModuleFileName来找出它的模块。还有用于查找模块名称GetModuleBaseName
  3. 只需跟踪在上面的示例中成功GetProcAddress时使用的HMODULE即可。