新 .exe 中的 WinAPI 函数

发布于 2024-09-30 02:48:45 字数 244 浏览 14 评论 0原文

我最近一直在考虑创建一种新的母语。我了解 PE 格式的(非常)基础知识,并且我从网上获取了一个具有相当友好界面的汇编器,我已成功地使用它来实现一些简单的功能。但我在使用库中的函数时遇到了问题。我以前从动态编译的函数调用库函数的唯一方法是手动传入函数指针 - 如果我创建 PE 文件并在自己的进程中执行它们,则无法执行此操作。现在,我不打算使用 CRT,但我需要访问 Win API 来实现我自己的标准库。如何生成对 WinAPI 函数的引用以便 PE 加载程序对其进行修补?

I've been looking recently into creating a new native language. I understand the (very) basics of the PE format and I've grabbed an assembler with a fairly kind interface off the webs, which I've successfully used to implement some simple functions. But I've run into a problem using functions from a library. The only way that I've called library functions from a dynamically compiled function previously is to pass in the function pointer manually- something I can't do if I create PE files and execute them in their own process. Now, I'm not planning on using the CRT, but I will need access to the Win API to implement my own standard libraries. How do I generate a reference to a WinAPI function so that the PE loader will patch it up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2024-10-07 02:48:45

您需要编写一个导入表。它基本上是您希望在应用程序中使用的函数名称列表。它由 PE 标头指向。加载程序将 DLL 文件加载到进程内存空间中,在导出表中找到所请求的函数,并将其地址保留在导入表中。然后,您通常会取消引用它并在那里jmp

查看 Izelion 的汇编教程 了解完整的详细信息和 asm 示例。

You need to write an import table. It's basically a list of function names that you wish to use in your application. It's pointed to by the PE header. The loader loads the DLL files into the process memory space for you, finds the requested function in their export table and leaves the address for it in the import table. You then usually dereference that and jmp there.

Check out Izelion's assembly tutorial for the full details and for asm examples.

眼角的笑意。 2024-10-07 02:48:45

从发出 C 而不是汇编开始怎么样?那么直接写入ASM只是一种优化。

我并不是在开玩笑:大多数编译器都会在最终的本机代码传递之前生成某种中间代码。

我意识到您正试图摆脱所有以 null 分隔的繁琐内容,但无论如何您都需要 WinAPI 函数。

重新阅读您的问题:您确实意识到可以通过调用 LoadLibrary(),然后调用 GetProcAddress(),然后设置调用来获取 WinAPI 函数地址...对吗?

如果您想了解如何从纯汇编中引导它:旧的 SDK 有 ASM 示例代码,可能新的 SDK 仍然有。如果他们不这样做,DDK 就会这样做。

How about starting by emitting C instead of assembly? Then writing directly to ASM is just an optimization.

I'm not being facetious: most compilers turn out some kind of intermediate code before the final native code pass.

I realize you're trying to get away from all the null-delmited rigmarole, but you'll need that for the WinAPI functions anyway.

Re-reading your question: you do realize that you can get the WinAPI function addresses by calling LoadLibrary(), then calling GetProcAddress(), and then setting up the call...right?

If you want to see how to bootstrap this from pure assembly: the old SDKs had ASM sample code, probably the new ones still do. If they don't, the DDK will.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文