Skip to content

Instantly share code, notes, and snippets.

@mrwonko
Created March 16, 2013 20:14
Show Gist options
  • Select an option

  • Save mrwonko/5178120 to your computer and use it in GitHub Desktop.

Select an option

Save mrwonko/5178120 to your computer and use it in GitHub Desktop.
Testing calling __thiscall functions (member functions) from (inline) asm (MSVC)
#include "stdafx.h"
#include <stdio.h> //printf
#include <stdlib.h>//system
class OpaqueClass
{
public:
OpaqueClass(const char* name) : mName(name) {}
void CallMe(char* a, int b)
{
printf( "%s's CallMe called with (%s, %d)!\n", mName, a, b);
}
private:
const char* const mName;
};
void (OpaqueClass::* memberFuncPtr)(char*, int) = &OpaqueClass::CallMe;
void* funcToCall = (void*&) memberFuncPtr;
void CallFunc(void* obj, char* a, int b)
{
__asm
{
mov ecx, obj ; this-pointer goes to ECX
push b ; push right to left
push a
call funcToCall
}
}
int _tmain(int argc, _TCHAR* argv[])
{
OpaqueClass obj("Object");
CallFunc(&obj, "foo", 42);
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment