title: How to call an exported function by ordinal number
body: <p>I wanted to call an exported function (MessageBoxW from User32.dll) by its ordinal number. I have written the following code but it doesn't show a message box to me. I used dependecy walker to find out ordinal number of MessageBoxW in User32.dll. Ordinal number of the exported function is 2157.</p>
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;iostream&gt;
  #include &lt;functional&gt;
  
  typedef int(WINAPI *MsgBOX)
  (
      _In_opt_ HWND hWnd,
      _In_opt_ LPCWSTR lpText,
      _In_opt_ LPCWSTR lpCaption,
      _In_ 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;
      int ordinal_number = 2157;
  
      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, (LPCSTR)ordinal_number));
  
          if (!MsgBoxInstance)
          {
              std::cout &lt;&lt; &quot;Function didn't resolved.&quot;;
          }
          else
          {
              MsgBoxInstance(NULL, TEXT(&quot;Resource not available\nDo you want to try again?&quot;), TEXT(&quot;Account Details&quot;), MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2);
          }
      }
  
      FreeLibrary(handle_user32_dll);
  
      return 0;
  }
  </code></pre>
  <p>When I run the above program, it doesn't show MessageBoxW result to me, also it shows me the following error:</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>Although, I specified WINAPI in typedef signature but it gives me the above error again. However, when I used the name of the MessageBoxW rather than its ordinal number, programs work fine.</p>