简体   繁体   English

如何链接DLL以供LoadLibrary()使用(在Windows上的C ++中),并从调用exe导入变量

[英]How to link a DLL for LoadLibrary() use (in C++ on Windows ) and do import of variables from the calling exe

On Windows I have a program executable which is the target of DLL injection via a Windows hook. 在Windows上,我有一个程序可执行文件,它是通过Windows钩子注入DLL的目标。 The hook injects my DLL into the program space of the target by forcing it to do a LoadLibrary(). 挂钩通过强制我执行LoadLibrary()将DLL注入目标程序空间。 I want to export a particular variable from the exe to the DLL so that the DLL can read the contents of that variable. 我想将特定变量从exe导出到DLL,以便DLL可以读取该变量的内容。

Since this is dynamic linking, the exe and DLL are not linked together by the linker. 由于这是动态链接,因此链接程序不会将exe和DLL链接在一起。 I can't build the DLL because I get linker error 2001 - unresolved external - on the variable I'm trying to import from the exe. 我无法生成DLL,因为我在尝试从exe导入的变量上收到链接器错误2001-外部未解决。 I tried using this in the DLL: extern "C" __declspec(dllimport) EGL_UINT8 *ssFrameDataBlock[]; 我尝试在DLL中使用它:extern“ C” __declspec(dllimport)EGL_UINT8 * ssFrameDataBlock [];

But this does not resolve the link error. 但这不能解决链接错误。 Obviously I'm missing some steps. 显然我缺少一些步骤。 How do I export a symbol from the exe and import it into the DLL for dynamic linking? 如何从exe导出符号并将其导入DLL以进行动态链接? What is the correct syntax on each side for the export and import? 导出和导入的每一侧正确的语法是什么?

Did you remember to decorate the executable's definition of the variable with __declspec(dllexport)? 您还记得用__declspec(dllexport)装饰变量的可执行文件定义吗? You will then need to provide the exe's .lib file as part of the DLL build. 然后,您需要提供exe的.lib文件作为DLL构建的一部分。

If you do not export at least one symbol (whether by dllexport, or a EXPORTS statement in a def file or on the command line) no .lib file will be produced. 如果您不导出至少一个符号(无论是dllexport还是def文件或命令行中的EXPORTS语句),将不会生成.lib文件。

If you can't provide a .lib to the DLL build you could also use GetProcAddress() from within the DLL code (despite the name it can obtain the address of any export, not just functions). 如果您不能为DLL版本提供.lib,您也可以从DLL代码中使用GetProcAddress()(尽管它的名称可以获取任何导出的地址,而不仅仅是函数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM