Created
October 8, 2011 23:34
-
-
Save 7shi/1273062 to your computer and use it in GitHub Desktop.
サンクでthiscallをcdeclに変換
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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