Skip to content

Instantly share code, notes, and snippets.

@7shi
Created October 8, 2011 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 7shi/1273062 to your computer and use it in GitHub Desktop.
Save 7shi/1273062 to your computer and use it in GitHub Desktop.
サンクでthiscallをcdeclに変換
#include <stdio.h>
#include "xbyak/xbyak.h"
template <class T, class TA1>
struct Thunk : public Xbyak::CodeGenerator {
Thunk(T *t, void (T::*p)(TA1)) {
mov(ecx, reinterpret_cast<intptr_t>(t));
push(ptr[esp+4]);
call(*reinterpret_cast<void **>(&p));
ret();
}
void (*getPtr())(TA1) {
return reinterpret_cast<void (*)(TA1)>(
const_cast<Xbyak::uint8 *>(getCode()));
}
};
struct Test {
int n;
Test(int n) : n(n) {}
void show(int n2) {
printf("%d, %d\n", n, n2);
}
};
void show(int n) {
printf("%d\n", n);
}
void call(void (*f)(int), int n) { f(n); }
int main() {
Test t(1);
Thunk<Test, int> thunk(&t, &Test::show);
call(thunk.getPtr(), 2);
call(show, 3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment