Wednesday 15 April 2015

c++ - correct convention call to a class method -


I can not use the __ thiscall , giving me the following error: < Error C4234: Non-standard extension used: The '__thiscall' keyword reserved for future use

I am calling a class function DLL:

  Type type zero (* SETDIRPROC) (Zero *, D3DXVECTOR3 & amp;;); Zero ZMyCharacter_SetDirection_Rev (zero) {if (pZMyCharacter == 0) {printf ("can not set directions now \ n"); Return; } SETDIRPROC SetDir_Proc = (SETDIRPROC) 0x004A2AF0; D3DXVECTOR3 pos = D3DXVECTOR3 (4.0 F, 2.0F, 1.0F); SetDir_Proc (pZMyCharacter, pos); }   

pZMyCharacter is a zero * indicator on the actual class ZMyCharacter on a real code. It works, but I get debug errors (which can be ignored) Warning that call conference is different It is actually because SETDIRPROC is by __ Cdecl and I can not convert it to __ thiscall .

// Error C4234

How can I get along with this?

I'm taking you down the dark and horrible road in the country of undefined behavior ...

The problem here is that you need to call the pointer in a member function, in fact no member is an indicator for the function. You can use a little magic from UB's land to fulfill it. This dark magic will allow you to convert a simple integer value into a completely usable predictive member function. To do this, we can create a union ...

  // magical beans. Change your own beans if you have the structure ZMyCharacter {}; // Here is the dark magic! Union Function Address {Typingf Zero (ZMIRAKER :: :: MAAMRAFUNC) (D3XXpector 3); Function Adapter (UITPRET_T ADR): address (re-reference & lt; zero * & gt; (additional)) {} MEMBER_FUNC function; Private: Zero * Address; };   

This magical union will allow you to set the pointer function through a simple integer value through your worker ...

  Function Editor pZMyFunction (0x004A2AF0);   

To make use of it, instead of going to the pointer for any other function, to make a few changes in the ZMyCharacter_SetDirection_Rev to directly call the member function (Which depends on your previous question that I think there are some inline assembly) ... ZMyCharacter_SetDirection_Rev (zero) {if (pZMyCharacter == NULL) {printf (" Can not set directions now \ n "); Return; } D3DXVECTOR3 pos = D3DXVECTOR3 (4.0 F, 2.0F, 1.0F); // Call directly to the member function (pZMyCharacter-> * pZMyFunction.function) (POS); }

Keep in mind that if the function is virtual, then you are pressing the virtual function dispatch. If this is the case then you should be prepared for the situations where it breaks polymorphism. If you want to fully support the transmission of virtual functions, then you will need to completely reverse the layout of the virtual table. For this, you can do something simple that does not use very dark magic.

  // reverse engineer viral function layer / ordering strat ZMyCharacter {virtual void func1 (); Virtual zeros (func2); Virtual zeros (func3); Virtual Zero target_function (D3dxactor 3 & amp; nbsp;); Virtual zeros (func4); }; Zero ZMyCharacter_SetDirection_Rev (zero) {if (pZMyCharacter == NULL) {printf ("can not set directions now \ n"); Return; } D3DXVECTOR3 pos = D3DXVECTOR3 (4.0 F, 2.0F, 1.0F); PZMyCharacter-> Target_function (pos); }   

I know that some things change for your current code base. You should either be able to integrate the Code or Concepts, which you are trying to fulfill.


The undefined behavior I mentioned is in relation to reaching a non-active member of a union. Usually it is considered bad.

No comments:

Post a Comment