我试图让FFI支持我的新编程语言,它是用C++与QT一起使用MinGW工具链编写的。
为此,我使用了以下自定义的libffi版本:http://ftp.gnome.org/pub/GNOME/binaries/win32/dependencies/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通过下载Linux上的SRPM文件,提取它,并将所需的文件复制到一个Windows分区。
无论如何,我包含了所需的头文件,将导入库添加到项目中,并将.dll放在应用程序的.exe旁边,它编译并运行,成功地调用了MessageBeep()。接下来,我用MessageBoxA()尝试了一下,但是它一直在崩溃。调试器似乎没有提供太多有用的信息(编辑:除了对MessageBoxA的调用确实发生了),所以我一直在摆弄一些东西,但是没有结果。
为了将问题与语言的细节隔离开来,我尝试手动调用MessageBoxA,方法是填充所有参数,导致下面的代码仍然崩溃。
因此,我的问题是:如何让下面的代码片段在question /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);
}
}发布于 2011-08-02 11:49:48
您应该将地址传递给值数组而不是值。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;
}https://stackoverflow.com/questions/6886995
复制相似问题