最善の策は、実行可能ファイルとして割り当てたメモリに .bin ファイルの内容を読み取る通常の C プログラムを作成し、それを呼び出すことです。
この(完全にテストされていない)プログラムは、読み取り/書き込み/実行可能なメモリをVirtualAlloc
. 何も返さず、パラメーターをとらない関数ポインター ( func
) は、バッファーの先頭を指してから呼び出されます (通常の C 関数と同様)。
.bin
これは、 (オフセット 0 で) 実行できる「通常の」関数で開始することを前提としていますcall
。
#include <stdio.h>
#include <Windows.h>
int main(int argc, char** argv)
{
FILE* f;
int size;
// A function pointer to your binary blob.
// Change this prototype, depending on the function you're calling
void (*func)(void);
if (argc < 2) exit(1);
f = fopen(argv[1], "rb");
if (!f) exit(1);
// Get file size
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);
// Allocate executable buffer
buf = VirtualAlloc(NULL,
size,
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE);
// Read the file into the buffer
if (fread(buf, 1, size, f) != size)
exit(1);
// Point your function pointer to the buffer
func = (void*)buf;
// ...and call it
func();
return 0;
}