如果我从 C++ 代码调用 Win32 API 函数,如下例所示:
fSuccess = WriteFile(
hPipe, // pipe handle
lpvMessage, // message
cbToWrite, // message length
&cbWritten, // bytes written
NULL); // not overlapped
然后 Visual C++ 编译器会将这些调用转换为对数据段中导入地址表 (IAT) 的绝对引用。 IAT 本身将包含外部库的实际绝对地址,该外部库包含 Win32 API 函数。在上面的 WriteFile
函数示例中,该外部库是 kernel32.dll
。
如果我在运行时修改 IAT 中的一个条目,使其指向不同的库/函数,那么原始 C++ 中对该函数的所有调用也将发生变化。我只想改变其中的一些。或者使得某个Win32 API函数根本不出现在IAT中,而是直接被代码段引用。
我怎样才能做到这一点?我能否以某种方式强制编译器在代码段中包含对外部库的直接绝对引用,而不是通过 IAT 进行间接引用?
最佳答案
I would like only some of them to change. Or to make it such that a certain Win32 API function does not even appear in the IAT at all, but it is directly referenced by the code segment.
How can I do that?
为此,您必须使用 LoadLibrary()
/GetModuleHandle()
和 GetProcAddress()
在运行时动态访问函数> (或者编译器的延迟加载包装器,如果有的话),而不是在编译时静态链接到函数。例如:
typedef BOOL (WINAPI *LPFN_WRITEFILE)(HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED);
LPFN_WRITEFILE lpWriteFile = (LPFN_WRITEFILE) GetProcAddress(GetModuleHandle("kernel32"), "WriteFile");
fSuccess = lpWriteFile(
hPipe, // pipe handle
lpvMessage, // message
cbToWrite, // message length
&cbWritten, // bytes written
NULL);
关于c++ - 可移植可执行文件是否可以在代码段中直接绝对引用外部库,而不是通过 IAT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36050085/