开发者

Calling Windows API with libffi on MinGW

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开发者_Go百科 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);
    }
}


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;
     }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜