title: Get function address from a function structure member
body: <p>I'm trying to get function addresses which are hidden behind structures. Unfortunately, the <b><code>void*</code></b> basic C++ conversion doesn't work, so I used <b><code>C++ template</code></b> instead.</p>
  
  <p><b>1. Basic </b><b><code>void*</code></b> <b>C++ conversion doesn't work with functions inside structures, why?</b></p>
  
  <pre><code>void * lpfunction;
  lpfunction = scanf; //OK
  lpfunction = MessageBoxA; //OK
  </code></pre>
  
  <p>I made a simple structure :</p>
  
  <pre><code>struct FOO{
  
      void PRINT(void){printf("bla bla bla");}
  
      void SETA(int){} //nothing you can see
      void SETB(int){} //nothing you can see
  
      int GETA(void){} //nothing you can see
      int GETB(void){} //nothing you can see
  };
  ///////////////////////////////////////////
  void *lpFunction = FOO::PRINT;
  </code></pre>
  
  <p>And the compiling error :</p>
  
  <pre><code>error C2440: 'initializing' : 
  cannot convert from 'void (__thiscall FOO::*)(void)' to 'void *'
  </code></pre>
  
  <p><strong>2. Is getting function member addresses impossible?</strong></p>
  
  <p>Then, I made a template function which is able to convert a function member to address. Then I will call it by assembly. It should be something like this:  </p>
  
  <pre><code>template &lt;class F,void (F::*Function)()&gt;  
  void * GetFunctionAddress() {
  
      union ADDRESS  
      { 
          void (F::*func)();  
          void * lpdata;  
      }address_data;  
  
      address_data.func = Function;  
      return address_data.lpdata; //Address found!!!  
  
  }  
  </code></pre>
  
  <p>And here is the code :  </p>
  
  <pre><code>int main()
  {
      void * address = GetFunctionAddress&lt;FOO,&amp;FOO::PRINT&gt;();
  
      FOO number;
      number.PRINT(); //Template call
      void * lpdata = &amp;number;
  </code></pre>
  
  <p><b></p>
  
  <pre><code>    __asm mov ecx, lpdata //Attach "number" structure address
      __asm call address //Call FOO::PRINT with assembly using __thiscall
  </code></pre>
  
  <p></b></p>
  
  <pre><code>printf("Done.\n");
  system("pause");
  return 0;
  }
  </code></pre>
  
  <p>But, I see it is <strong>extremely specific</strong>. It looks like <strong>LOCK - KEY</strong>, and I have to make a new template for every set of argument types.</p>
  
  <p>Original (OK) :</p>
  
  <pre><code>void PRINT(); //void FOO::PRINT();
  </code></pre>
  
  <p>Modify a bit :</p>
  
  <pre><code>void PRINT(int); //void FOO::PRINT(int);
  </code></pre>
  
  <p>Immediately with old template code the compiler shows :  </p>
  
  <pre><code>//void (F::*func)();
  //address_data.func = Function;
  </code></pre>
  
  <p><b></p>
  
  <pre><code>error C2440: '=' : cannot convert from 
  'void (__thiscall FOO::*)(int)' to 'void (__thiscall FOO::*)(void)'
  </code></pre>
  
  <p></b></p>
  
  <p><b>Why?</b> They are only addresses.</p>
  
  <pre><code>69:       address_data.func = Function;
  00420328  mov  dword ptr [ebp-4],offset @ILT+2940(FOO::PRINT) (00401b81)
  </code></pre>
  
  <p>...</p>
  
  <p>EDIT3 : I know the better solution :</p>
  
  <pre><code>void(NUMBER::*address_PRINT)(void) = FOO::PRINT;
  int(NUMBER::*address_GETA)(void) = FOO::GETA;
  int(NUMBER::*address_GETB)(void) = FOO::GETB;
  void(NUMBER::*address_SETA)(int) = FOO::SETA;
  void(NUMBER::*address_SETA)(int) = FOO::SETB;
  </code></pre>
  
  <p>It's much better than <strong>template</strong>. And by the way I want to achieve the goal :</p>
  
  <pre><code>&lt;special_definition&gt; lpfunction;
  lpfunction = FOO::PRINT; //OK
  lpfunction = FOO::GETA; //OK
  lpfunction = FOO::GETB; //OK
  lpfunction = FOO::SETA; //OK
  lpfunction = FOO::SETB; //OK
  </code></pre>
  
  <p><strong>Is this possible?</strong></p>