简体   繁体   English

使用def文件从可执行文件导出功能

[英]Exporting functions from an executable using a def file

There is plenty of information available about how to export functions from a dll (which I've done many times), but I heard that it's also possible to export functions from an executable , so that an external dll can call them. 关于如何从dll导出函数有很多可用的信息(我已经做过很多次了),但是我听说也可以从可执行文件中导出函数,以便外部dll可以调用它们。

Although I've managed to get this working, it seems as though there's some problem with the entry point: 尽管我设法使它起作用,但是入口点似乎存在一些问题:

  • If it is not explicitly set, then it defaults to the wrong "main" in an obscure sub-library. 如果未显式设置,则在晦涩的子库中它默认为错误的“ main”。
  • If it is explicitly set, then its input arguments, argc and argv get corrupted (argc can be ~20000000 or ~-700000). 如果明确设置,那么它的输入参数,argc和argv遭到损坏(ARGC可以〜20000000或〜-700000)。

I'm having trouble finding any documentation on exporting functions from an executable - should I be taking the hint and not doing it? 我在查找有关从可执行文件导出功能的任何文档时遇到麻烦-我应该接受提示而不要这样做吗?

[Context: This was part of an effort to make our process work on both Windows and Linux. [上下文:这是使我们的流程在Windows和Linux上都能正常工作的一部分。 The Linux version was accidentally picking up functions from the executable, rather than ones explicitly exported from an attendant dll (the functions had the same name, but different args). Linux版本意外地从可执行文件中获取功能,而不是从附带的dll中显式导出的功能(这些功能具有相同的名称,但参数不同)。 We decided to try to run with this, and export the functions from the executable on Windows as well.] 我们决定尝试与此一起运行,并从Windows上的可执行文件中导出功能。]

I'm posting this just to summarise my own learning on this, in absence of a better answer: 我发布此内容只是为了总结自己对此的学习,而没有更好的答案:

  • Immediately after adding the def file, the linker complained that it couldn't determine an entry point. 添加def文件后,链接器立即抱怨无法确定入口点。 It was for this reason that I added the /ENTRY reference. 出于这个原因,我添加了/ ENTRY引用。 During rework, however, I removed the /ENTRY while removing the def file, and I could compile without error - I must have removed a subtly conflicting option in the meantime. 但是,在返工期间,我在删除def文件的同时删除了/ ENTRY,并且可以进行编译而不会出现错误-同时,我必须删除了一个细微冲突的选项。
  • The def file does export the functions from the .exe successfully, and these can then be used in a dll of that process (if it links to DelayImp.lib and the executable's .lib). DEF文件不会从.exe成功导出的功能,这些都可以再在这个过程中的一个dll可以使用(如果它链接到DELAYIMP.LIB和可执行的.LIB)。
  • I was never able to get the /ENTRY option to work satisfactorily, and combined with the mildly discouraging comments on the MSDN item [ https://msdn.microsoft.com/en-us/library/f9t8842e.aspx] , I see no reason to use it in this case. 我从没能使/ ENTRY选项令人满意地工作,并与对MSDN项目[ https://msdn.microsoft.com/en-us/library/f9t8842e.aspx]的令人不快的评论结合在一起,我看不到在这种情况下使用它的原因。

I hope that this is of some use to anybody else attempting to do something similar. 我希望这对尝试做类似事情的其他人有用。 I will be happy to re-designate a more technical answer as "the solution", should one appear... 如果有人出现,我将很乐意将一个更技术性的答案重新指定为“解决方案”。

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

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