title: Run-Time Check Failure - Different Calling Convention
body: <p>I wanted to load a sample library (i.e. user32) and then using one exported function of that library like messageboxw to show a message to the user. My programs works fine, it shows the message but when I click on a button to close the program, it shows the following message:</p>
  <blockquote>
  <p>Run-Time Check Failure #0 - The value of ESP was not properly saved
  across a function call.  This is usually a result of calling a
  function declared with one calling convention with a function pointer
  declared with a different calling convention.</p>
  </blockquote>
  <p>My source code:</p>
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;iostream&gt;
  #include &lt;functional&gt;
  
  typedef int(*MsgBOX)
  (
      HWND    hWnd,
      LPCWSTR lpText,
      LPCWSTR lpCaption,
      UINT    uType
  );
  
  int main(int argc, char* argv[])
  {
      HINSTANCE handle_user32_dll = LoadLibrary(TEXT(&quot;User32.dll&quot;));
      std::function&lt;int(HWND, LPCWSTR, LPCWSTR, UINT)&gt; MsgBoxInstance;
      
      if(!handle_user32_dll)
      {
          std::cout &lt;&lt; &quot;Dll isn't loaded successfuly.&quot; &lt;&lt; std::endl;
      }
      else
      {
          MsgBoxInstance = reinterpret_cast&lt;MsgBOX&gt;(GetProcAddress(handle_user32_dll, &quot;MessageBoxW&quot;));
          if (!MsgBoxInstance)
          {
              std::cout &lt;&lt; &quot;Function didn't resolved.&quot;;
          }
          else
          {
              MsgBoxInstance(NULL, L&quot;Resource not available\nDo you want to try again?&quot;, L&quot;Account Details&quot;, MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2);
          }
      }
  
      FreeLibrary(handle_user32_dll);
  
      return 0;
  }
  </code></pre>
  <p>Where I did mistake and how can I fix this issue?</p>