我一直在研究_vsnprintf
,并了解到它在ntdll.dll和msvcrt.dll中可用。
我可以使用GetModuleHandle
和GetProcAddress
来访问_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
实用程序。
要务实地做到这一点,您有两个主要选项:
- 如果是静态导入,则可以对 IAT 进行探测并检查其导入的模块。
- 如果你是动态的(即:使用
GetProcAddress
),你可以使用VirtualQuery
和GetModuleFileName
来找出它的模块。还有用于查找模块名称GetModuleBaseName
。 - 只需跟踪在上面的示例中成功
GetProcAddress
时使用的HMODULE
即可。