title: Exporting a function, cast into a pointer through a DLL
body: <p>This is an elaboration to an earlier question: <a href="https://stackoverflow.com/questions/33519285/how-to-reset-state-machines-when-unit-testing-c">How to reset state machines when unit testing C</a><br>
  There is also a similar question but i don't the answer match my problem and i have some examples i wish to present: <a href="https://stackoverflow.com/questions/7764074/exporting-a-function-pointer-from-dll">Exporting a function pointer from dll</a></p>
  
  <p>I have two sets of code that i expect should do the same but the latter crashes.
  Using mingw32 and Win7.</p>
  
  <p>The function to be exported. This is to be considered legacy and unmutable.<br>
  addxy.c</p>
  
  <pre><code>    int addXY(int x, int y)
      {
        return x + y;
      }    
  </code></pre>
  
  <p>addxy.h</p>
  
  <pre><code>    int addXY(int x, int y);
  </code></pre>
  
  <h2>The working example</h2>
  
  <p>main.c</p>
  
  <pre><code>    #include &lt;stdio.h&gt;
      #include "addxy.h"
  
      typedef int (__cdecl *addXYWrap_t)(int a, int b);
  
      addXYWrap_t addXYWrap = (addXYWrap_t)addXY;
  
      void main()
      {
        printf("result: %d", addXYWrap(3, 4));
      }
  </code></pre>
  
  <p>Yielding</p>
  
  <pre><code>result: 7
  </code></pre>
  
  <h2>The crashing example</h2>
  
  <p>addxydll.c</p>
  
  <pre><code>    #include &lt;stdio.h&gt;
      #include "addxy.h"
  
      typedef int (__cdecl *addXYWrap_t)(int a, int b);
  
      __declspec(dllexport) addXYWrap_t addXYWrap = (addXYWrap_t)addXY;
  </code></pre>
  
  <p>main.c</p>
  
  <pre><code>    #include &lt;windows.h&gt;
      #include &lt;stdio.h&gt;
  
      typedef int (__cdecl *func)(int a, int b);
  
      void main()
      {
        HINSTANCE loadedDLL = LoadLibrary("addxy.dll");
  
        if(!loadedDLL) {
          printf("DLL not loaded");
          return;
        } else {
          printf("DLL loaded\n");
        }
  
        func addition = (func)GetProcAddress(loadedDLL, "addXYWrap");
  
        if(!addition) {
          printf("Func not loaded");
          return;
        } else {
          printf("Func loaded\n");
        }
  
        printf("result: %d", addition(3, 4));
      }
  </code></pre>
  
  <p>Yielding</p>
  
  <pre><code>DLL loaded
  Func loaded
  </code></pre>
  
  <p>before it chrashes.</p>
  
  <p>The crash gives no information as to why or what.<br>
  Is it a syntactical error or conceptional one?</p>