0

IDE: Code::Blocks Compiler: g++ 4.5.4 (from Mingw32_i686) Type application: console type

Ok

    // sourcefile: winfunctions.h

    #ifndef __WINFUNCTIONS_H__
    #define __WINFUNCTIONS_H__

    #include <windef.h>
    #include <winbase.h>

    typedef void (__stdcall *F)();
    void StdCall(char *mName, char *fName)();
    typedef void (__stdcall *F_)();
    void StdCall_(char *mName, char *fName)(auto *args[]);
    typedef auto (__stdcall *_F)();
    auto _StdCall(char *mName, char *fName)();
    typedef auto (__stdcall *_F_)();
    auto _StdCall_(char *mName, char *fName)(auto *args[]);

    #endif // __WINFUNCTIONS_H__




    // sourcefile: winfunctions.cpp

    #include "winfunctions.h"

    void StdCall(char *mName, char *fName)()
    {
        HMODULE hM = LoadLibrary(mName);
        F f = (F)GetProcAddress(hM,fName)();
        if (f) f();
        FreeLibrary(hM);
    }

    void StdCall_(char *mName, char *fName)(auto *args[])
    {
        HMODULE hM = LoadLibrary(mName);
        F_ f = (F_)GetProcAddress(hM,fName)(args);
        if (f) f(args);
        FreeLibrary(hM);
    }

    auto _StdCall(char *mName, char *fName)()
    {
        HMODULE hM = LoadLibrary(mName);
        _F f = (_F)GetProcAddress(hM,fName)();
        if (f) return f();
        FreeLibrary(hM);
    }

    auto _StdCall_(char *mName, char *fName)(auto *args[])
    {
        HMODULE hM = LoadLibrary(mName);
        _F_ f = (_F_)GetProcAddress(hM,fName)(args);
        if (f) return f(args);
        FreeLibrary(hM);
    }

I will give the code, it does not work as expected. This code should show my desire.

I want using four wrapper functions to cause any winapi function.

F - function returns nothing and has no input parameters

F_ - function does not return anything, but it has the input parameters

_F - function returns a value, but has no input parameters

_F_ - function returns a value, and has input parameters

I want to define the type of return value of the function and the types of parameters to be passed to the runtime functions, rather than at compile time.

Of course, I also do not want to determine the number of parameters to be passed to functions at compile time.

Any ideas? This is even possible?

UPD:

Thank you rodrigo

I downloaded the libffi 3.0.6 win32 (used libffi.dll.a and header files in my test console application), but given alert: "libffi-5.dll not found".

I downloaded http://rpm.pbone.net/index.php3/stat/4/idpl/24142309/dir/fedora_16/com/mingw32-libffi-3.0.9-2.fc15.noarch.rpm.html, unpacked it, found libffi-5.dll an copy this .dll in C:\Mingw32\bin.

The code for the test:

#include <iostream>
#include <stdio.h>
#include <windows.h>
#include "ffi.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;

     }

Yes! I have a working code!

I will continue to study this question.

Community
  • 1
  • 1

0 Answers0