1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| /*
CallFunctionHelper.cpp
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define CALLFUNCTION_MULTITHREAD
//
//----------------------------------------------------------------------------
//Fonctions relatives à la sauvegarde d'un registre
#ifdef CALLFUNCTION_MULTITHREAD
#include <stdlib.h>
#include <assert.h>
static DWORD g_tlsIndex = TLS_OUT_OF_INDEXES;
static DWORD GetTlsIndex(void)
{
//TODO: Ajouter de la synchronisation ici.
if(g_tlsIndex==TLS_OUT_OF_INDEXES)
{
g_tlsIndex = TlsAlloc();
if(g_tlsIndex==TLS_OUT_OF_INDEXES)
abort();
}
//Ici, on suppose que tout a réussi.
assert(g_tlsIndex!=TLS_OUT_OF_INDEXES);
return g_tlsIndex;
}
EXTERN_C void* __cdecl GetCallFunctionRegister(void)
{
void* addr = TlsGetValue(GetTlsIndex());
return addr;
}
EXTERN_C void __cdecl SetCallFunctionRegister(void* addr)
{
TlsSetValue(GetTlsIndex(), addr);
}
#else
#ifndef EXTERN_C
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C extern
#endif
#endif
void* g_addr;
EXTERN_C void* __cdecl GetESIRegister(void)
{
return g_addr;
}
EXTERN_C void __cdecl SetESIRegister(void* addr)
{
g_addr = addr;
}
#endif
//
//----------------------------------------------------------------------------
//Fonctions pour appeler les demi-fonctions Asm avec DispInvoke().
#include "CallFunction.h"
#include "oaidl.h"
#include <stdio.h>
#ifndef ARRAYSIZE
#define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0])
#endif
#define DISPID_GO 42
//--- Structures de données ---
typedef void (__cdecl *CPROC)();
typedef void (__stdcall *STDPROC)();
struct st_bidonCdeclVtbl
{
CPROC proc;
};
struct st_bidonCdecl
{
struct st_bidonCdeclVtbl *lpVtbl;
};
struct st_bidonStdcallVtbl
{
STDPROC proc;
};
struct st_bidonStdcall
{
struct st_bidonStdcallVtbl *lpVtbl;
};
//--- Fonctions pour tester ---
static int __cdecl UneFonctionCdecl(char const *str)
{
return lstrlenA(str);
}
static int __stdcall UneFonctionStdcall(char const *str)
{
int res = printf("%s", str);
putchar('\n');
return res;
}
void TestDispInvokeCdecl(void)
{
PARAMDATA parData[] = {
{ OLESTR("proc"), VT_I4 },
{ OLESTR("str"), VT_I4 } // Utiliser simplement un INT32 car DispInvoke ne reconnait pas un char* comme "bon" pointeur
};
METHODDATA mData[] = {
{ OLESTR("Go"), parData, DISPID_GO, 0, CC_CDECL, ARRAYSIZE(parData), DISPATCH_METHOD, VT_I4 }
};
INTERFACEDATA iData = { mData, ARRAYSIZE(mData) };
ITypeInfo *pInfo = NULL;
HRESULT hr = CreateDispTypeInfo(&iData, 0, &pInfo);
if(FAILED(hr))
{
MessageBoxA(NULL, "Failed to create type info", "TestDispInvoke() error", MB_OK|MB_ICONEXCLAMATION);
return;
}
struct st_bidonCdeclVtbl bidonVtbl = { reinterpret_cast<CPROC>(CallFunctionExCdecl) };
struct st_bidonCdecl bidon = { &bidonVtbl };
//La fonction à appeler et son argument pour le test
CPROC proc = reinterpret_cast<CPROC>(UneFonctionCdecl);
char const * str = "Frédéric";
//Ajouter les arguments en ordre inverse
VARIANTARG args[2];
size_t iArg = 0;
VariantInit(&args[iArg]);
V_VT(args+iArg) = VT_I4;
V_I4(args+iArg) = reinterpret_cast<LONG_PTR>(str);
iArg++;
VariantInit(&args[iArg]);
V_VT(args+iArg) = VT_I4;
V_I4(args+iArg) = reinterpret_cast<LONG_PTR>(proc);
DISPPARAMS dp = {args, NULL, ARRAYSIZE(args), 0};
VARIANT res;
hr = DispInvoke(&bidon, pInfo, DISPID_GO, DISPATCH_METHOD, &dp, &res, NULL, NULL);
if(SUCCEEDED(hr))
{
printf("Function call result: %d\n", V_I4(&res));
}
pInfo->Release(), pInfo=NULL;
}
void TestDispInvokeStdcall(void)
{
PARAMDATA parData[] = {
{ OLESTR("proc"), VT_I4 },
{ OLESTR("str"), VT_I4 } // Utiliser simplement un INT32 car DispInvoke ne reconnait pas un char* comme "bon" pointeur
};
METHODDATA mData[] = {
{ OLESTR("Go"), parData, DISPID_GO, 0, CC_STDCALL, ARRAYSIZE(parData), DISPATCH_METHOD, VT_I4 }
};
INTERFACEDATA iData = { mData, ARRAYSIZE(mData) };
ITypeInfo *pInfo = NULL;
HRESULT hr = CreateDispTypeInfo(&iData, 0, &pInfo);
if(FAILED(hr))
{
MessageBoxA(NULL, "Failed to create type info", "TestDispInvoke() error", MB_OK|MB_ICONEXCLAMATION);
return;
}
struct st_bidonStdcallVtbl bidonVtbl = { reinterpret_cast<STDPROC>(CallFunctionExStdcall) };
struct st_bidonStdcall bidon = { &bidonVtbl };
//La fonction à appeler et son argument pour le test
STDPROC proc = reinterpret_cast<STDPROC>(UneFonctionStdcall);
char const * str = "Frédéric";
//Ajouter les arguments en ordre inverse
VARIANTARG args[2];
size_t iArg = 0;
VariantInit(&args[iArg]);
V_VT(args+iArg) = VT_I4;
V_I4(args+iArg) = static_cast<LONG>(reinterpret_cast<LONG_PTR>(str));
iArg++;
VariantInit(&args[iArg]);
V_VT(args+iArg) = VT_I4;
V_I4(args+iArg) = static_cast<LONG>(reinterpret_cast<LONG_PTR>(proc));
DISPPARAMS dp = {args, NULL, ARRAYSIZE(args), 0};
VARIANT res;
hr = DispInvoke(&bidon, pInfo, DISPID_GO, DISPATCH_METHOD, &dp, &res, NULL, NULL);
if(SUCCEEDED(hr))
{
printf("Function call result: %d\n", V_I4(&res));
}
pInfo->Release(), pInfo=NULL;
} |
Partager