IDE:Code::Blocks 编译器:g++ 4.5.4(来自 Mingw32_i686) 类型应用程序:控制台类型
好的
// 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);
}
我会给出代码,它没有按预期工作。这段代码应该表明我的愿望。
我想使用四个包装函数来引发任何 winapi 函数。
F
- 函数不返回任何内容并且没有输入参数
F_
- 函数不返回任何内容,但它具有输入参数
_F
- 函数返回一个值,但没有输入参数
_F_
- 函数返回一个值,并有输入参数
我想定义函数返回值的类型和要传递给运行时函数的参数类型,而不是在编译时。
当然,我也不想在编译时确定要传递给函数的参数数量。
有任何想法吗?这甚至可能吗?
升级版:
我下载了 libffi 3.0.6 win32(在我的测试控制台应用程序中使用了 libffi.dll.a 和头文件),但给出了警告:“找不到 libffi-5.dll”。
我下载了http://rpm.pbone.net/index.php3/stat/4/idpl/24142309/dir/fedora_16/com/mingw32-libffi-3.0.9-2.fc15.noarch.rpm.html,解压,在 C:\Mingw32\bin 中找到了 libffi-5.dll 并复制了这个 .dll。
测试代码:
#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;
}
是的!我有一个工作代码!
我会继续研究这个问题。