Created
March 16, 2013 20:14
-
-
Save mrwonko/5178120 to your computer and use it in GitHub Desktop.
Testing calling __thiscall functions (member functions) from (inline) asm (MSVC)
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 "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