在 MinGW 上使用 libffi 调用 Windows API

发布于 2024-11-27 07:08:25 字数 1796 浏览 2 评论 0原文

我正在尝试为我的新编程语言提供 FFI 支持,该语言是使用 MinGW 工具链通过 QT Creator 用 C++ 编写的。

为此,我使用了此处找到的 libffi 的自定义版本: http://ftp.gnome.org/pub/GNOME/binaries/win32/dependency/libffi-dev_3.0.6-1_win32.zip

我还尝试了另一个版本:http://pkgs.org/fedora-14/fedora-updates-i386/mingw32-libffi-3.0.9-1.fc14.noarch.rpm.html 通过下载 SRPM 文件在 Linux 上,解压它,并将所需的文件复制到 Windows 分区。

无论如何,我包含了所需的头文件,将导入库添加到项目中,并将 .dll 放在应用程序的 .exe 旁边,它编译并运行,成功调用 MessageBeep()。接下来我尝试使用 MessageBoxA(),但它一直崩溃。调试器似乎没有提供太多有用的信息(编辑:除了发生对 MessageBoxA 的调用确实这一事实之外),所以我一直在摆弄东西并重新运行,但无济于事。

为了将问题与我的语言细节隔离开来,我尝试通过填写自己的所有参数来手动调用 MessageBoxA,导致下面的代码仍然崩溃。

所以我的问题归结为:如何让下面的代码片段在 QT Creator/MinGW 下运行并实际显示消息框?

#include "libffi/include/ffi.h"
#include <QLibrary>

void testMessageBox()
{
    int n = 4;
    ffi_cif cif;
    ffi_type **ffi_argTypes = new ffi_type*[n];
    void **values = new void*[n];

    values[0] = new ulong(0);
    values[1] = (void *) "hello";
    values[2] = (void *) "mommy";
    values[3] = new int32_t(0);

    ffi_argTypes[0] = &ffi_type_ulong;
    ffi_argTypes[1] = &ffi_type_pointer;
    ffi_argTypes[2] = &ffi_type_pointer;
    ffi_argTypes[3] = &ffi_type_uint32;

    ffi_type *c_retType = &ffi_type_sint32;
    int32_t rc; // return value

    if (ffi_prep_cif(&cif, FFI_STDCALL, n, c_retType, ffi_argTypes) == FFI_OK)
    {
        QLibrary lib("user32.dll");
        lib.load();
        void *msgbox = lib.resolve("MessageBoxA");
        ffi_call(&cif, (void (*)()) msgbox, &rc, values);
    }
}

I'm trying to have FFI support for my new programming language, which is written in C++ with QT Creator using the MinGW toolchain.

To do this I used a custom-built version of libffi found here: http://ftp.gnome.org/pub/GNOME/binaries/win32/dependencies/libffi-dev_3.0.6-1_win32.zip

I also tried it with another build: http://pkgs.org/fedora-14/fedora-updates-i386/mingw32-libffi-3.0.9-1.fc14.noarch.rpm.html by downloading the SRPM file on Linux, extracting it, and copying the needed files to a Windows partition.

Anyway, I included the required header file, added the import library to the project and put the .dll beside the application's .exe, it compiles and runs, calling MessageBeep() successfully. I tried it next with MessageBoxA(), but it keeps crashing. The debugger doesn't seem to provide much useful information (edit: beside the fact that a call to MessageBoxA did happen) so I keep fiddling with stuff and re-running to no avail.

To isolate the problem from the details of my language, I tried to manually call MessageBoxA by filling myself all the parameters, resulting in the code below, still crashing.

So my question distills to: How can I get the code snippet below to run under QT Creator/MinGW and actually show a message box?

#include "libffi/include/ffi.h"
#include <QLibrary>

void testMessageBox()
{
    int n = 4;
    ffi_cif cif;
    ffi_type **ffi_argTypes = new ffi_type*[n];
    void **values = new void*[n];

    values[0] = new ulong(0);
    values[1] = (void *) "hello";
    values[2] = (void *) "mommy";
    values[3] = new int32_t(0);

    ffi_argTypes[0] = &ffi_type_ulong;
    ffi_argTypes[1] = &ffi_type_pointer;
    ffi_argTypes[2] = &ffi_type_pointer;
    ffi_argTypes[3] = &ffi_type_uint32;

    ffi_type *c_retType = &ffi_type_sint32;
    int32_t rc; // return value

    if (ffi_prep_cif(&cif, FFI_STDCALL, n, c_retType, ffi_argTypes) == FFI_OK)
    {
        QLibrary lib("user32.dll");
        lib.load();
        void *msgbox = lib.resolve("MessageBoxA");
        ffi_call(&cif, (void (*)()) msgbox, &rc, values);
    }
}

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

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

发布评论

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

评论(1

人间☆小暴躁 2024-12-04 07:08:25

您应该将地址而不是值传递给值数组。 mingw64下的工作代码是

   #include <stdio.h>
   #include <ffi.h>
   #include <Windows.h>
   int main()
   {

     ffi_cif cif;
            HINSTANCE dllHandle = LoadLibrary("user32.dll");

            int n = 4;    

            ffi_type *ffi_argTypes[4]; 
            void *values[4];
            UINT64 a=0;
            UINT32 b=0;
            TCHAR* s1= "hello"; 
            TCHAR* s2= "hello2";  
            values[0] = &a;
            values[1] = &s1;
            values[2] = &s2;
            values[3] = &b;    
            ffi_argTypes[0] = &ffi_type_uint64;    
            ffi_argTypes[1] = &ffi_type_pointer;    
            ffi_argTypes[2] = &ffi_type_pointer;    
            ffi_argTypes[3] = &ffi_type_uint; 
            ffi_type *c_retType = &ffi_type_sint;    
            ffi_type rc; // return value    
            if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 4, &ffi_type_sint, ffi_argTypes) == FFI_OK)    {


                ffi_call(&cif, FFI_FN(GetProcAddress(dllHandle,"MessageBoxA")), &rc, values); 
            }


       return 0;
     }

you should pass the address to the values array instead of the values. the working code under mingw64 is

   #include <stdio.h>
   #include <ffi.h>
   #include <Windows.h>
   int main()
   {

     ffi_cif cif;
            HINSTANCE dllHandle = LoadLibrary("user32.dll");

            int n = 4;    

            ffi_type *ffi_argTypes[4]; 
            void *values[4];
            UINT64 a=0;
            UINT32 b=0;
            TCHAR* s1= "hello"; 
            TCHAR* s2= "hello2";  
            values[0] = &a;
            values[1] = &s1;
            values[2] = &s2;
            values[3] = &b;    
            ffi_argTypes[0] = &ffi_type_uint64;    
            ffi_argTypes[1] = &ffi_type_pointer;    
            ffi_argTypes[2] = &ffi_type_pointer;    
            ffi_argTypes[3] = &ffi_type_uint; 
            ffi_type *c_retType = &ffi_type_sint;    
            ffi_type rc; // return value    
            if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 4, &ffi_type_sint, ffi_argTypes) == FFI_OK)    {


                ffi_call(&cif, FFI_FN(GetProcAddress(dllHandle,"MessageBoxA")), &rc, values); 
            }


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