简体   繁体   English

Visual Studio - 在64位项目中编译32位代码

[英]Visual Studio - Compiling 32-bit code inside 64-bit project

So straight to my question: how can I compile my ASM files with a 32-bit ASM compiler, include it in my 64-bit project and access the compiled code by using the ASM file's function names? 所以直截了当地问我:如何用32位ASM编译器编译我的ASM文件,将它包含在我的64位项目中并使用ASM文件的函数名访问编译后的代码?

If it is a bit unclear, I can elaborate: 如果有点不清楚,我可以详细说明:

I'm converting a project of mine from 32-bit to 64-bit and I ran into a technical problem. 我正在将我的一个项目从32位转换为64位,我遇到了技术问题。 My project compiles an ASM file and use the compiled binary as input for it's usage. 我的项目编译ASM文件并使用编译的二进制文件作为其用法的输入。

When my project was 32-bit, it was quite easy. 当我的项目是32位时,它很容易。 I included the ASM files in the project and added a build rule to compile them with Microsoft Macro Assembler - then I could access the compiled code from my 32-bit project by exported each function I wanted to access from the ASM to a .h header file and access it using the function name (I was able to do so because it was compiled to obj and the linker knew the symbols because I exported the prototypes to a .h file). 我在项目中包含了ASM文件,并添加了一个构建规则,用Microsoft Macro Assembler编译它们 - 然后我可以通过将我想要从ASM访问的每个函数导出到.h头,从我的32位项目访问编译代码文件并使用函数名称访问它(我能够这样做,因为它被编译为obj,链接器知道符号,因为我将原型导出到.h文件)。

Now, I need to convert this code to 64-bit, but I still need the ASM to be compiled as 32-bit code and still be able to do the same thing (accessing the compiled 32-bit code from my 64-bit program). 现在,我需要将此代码转换为64位,但我仍然需要将ASM编译为32位代码,并且仍然可以执行相同的操作(从我的64位程序访问已编译的32位代码) )。 However, when I try to compile it, it obviously doesn't recognize the instructions because now the whole project is being compiled as 64-bit code. 但是,当我尝试编译它时,它显然无法识别指令,因为现在整个项目被编译为64位代码。

Thanks in advance. 提前致谢。

If I were trying to embed 32-bit code inside a 64-bit program (which is a dubious thing to do, but let's say for the sake of argument that you have a good reason and actually know what you're doing with the result) — I'd take the 32-bit code, whether written in C, assembly, or something else — and compile it as a separate project, producing a DLL as output. 如果我试图在64位程序中嵌入32位代码(这是一个可疑的事情,但是为了论证的缘故,我们说你有充分的理由并且实际上知道你正在对结果做什么) - 我将使用32位代码,无论是用C语言编写,汇编还是其他东西 - 并将其编译为单独的项目,生成DLL作为输出。 This involves no extra weirdness in the compile chain: It's just an ordinary 32-bit DLL. 这在编译链中没有任何额外的怪异:它只是一个普通的32位DLL。

That 32-bit DLL can then be embedded in your 64-bit application as a binary resource — just a blob of memory that you can load and access. 然后,可以将32位DLL作为二进制资源嵌入到64位应用程序中 - 只需要加载和访问的内存块即可。

So then how can you actually do anything with the compiled code in that DLL? 那么你如何才能真正对该DLL中的编译代码做任何事情呢? I'd use a somewhat-hacked version of Joachim Bauch's MemoryModule library to access it. 我会使用一个有点被黑客攻击的Joachim Bauch的MemoryModule库来访问它。 MemoryModule is designed to load DLLs from a hunk of memory and provide access to their exports — it's just like the Windows API's LoadLibrary() , only from memory instead of from a file. MemoryModule旨在从大量内存中加载DLL并提供对其导出的访问 - 它就像Windows API的LoadLibrary() ,仅来自内存而不是文件。 It's designed to do it for the same bit size as the calling process, but with a bit of hackery, you could probably make it compile as a 64-bit library, but able to read a 32-bit library. 它被设计为与调用进程相同的位大小,但有一点hackery,你可以使它编译为64位库,但能够读取32位库。 The resulting usages would be pretty simple: 由此产生的用法非常简单:

// Load the embedded DLL first from the current module.
hresource = FindResource(hmodule, "MyLibrary.DLL", "binary");
hglobal = LoadResource(hmodule, hresource);
data = LockResource(hglobal);
size = SizeofResource(hmodule, hresource);

// Turn the raw buffer into a "library".
libraryHandle = MemoryLoadLibrary(data, size);

// Get a pointer to some export within it.
myFunction = MemoryGetProcAddress(libraryHandle, "myFunction");

That said, as I alluded to before (and others also alluded to), even if you can get pointers to the exports, you won't be able to invoke them, because the code's 32-bit and might not even be loaded at an address that exists below the 4GB mark. 也就是说,正如我之前提到的(以及其他人也提到过),即使你可以获得指向导出的指针,你也无法调用它们,因为代码是32位的,甚至可能都没有加载到存在于4GB标记以下的地址。 But if you really want to embed 32-bit code in a 64-bit application, that's how I'd go about it. 但是如果你真的想在64位应用程序中嵌入32位代码,我就是这样做的。

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

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