title: "The specified procedure could not be found" error with .NET 4
body: <p>I am developing on a 64-bit Windows 7 box with Visual Studio 2012 (11.0.51106.01 Update 1).</p>
  
  <p>I have a Support project that compiles some C code into a (32-bit) DLL. In my header I have:</p>
  
  <pre><code>#define RET_TYPE(type) _declspec(dllexport) type __stdcall
  
  RET_TYPE(int) test_dll_call(int invar);
  </code></pre>
  
  <p>In my C file I have:</p>
  
  <pre><code>RET_TYPE(int) test_dll_call(int invar)
  {
     int retVal = 4 * invar;
  
     return retVal;
  }
  </code></pre>
  
  <p>I also have a (32-bit) WPF C# application that loads the DLL inside of a class as follows:</p>
  
  <pre><code>[DllImport("MyDll.dll", CharSet = CharSet.Ansi, BestFitMapping = true, ThrowOnUnmappableChar = true)]
  public static extern int test_dll_call(int invar);
  </code></pre>
  
  <p>This is wrapped as follows:</p>
  
  <pre><code>public void Wrap_TestDllCall()
  {
     try
     {
        int outTest = 0;
        int invar = 3;
  
        outTest = test_dll_call(invar);
     }
     catch (Exception ex)
     {
        dllError = ex.ToString();
     }
  }
  </code></pre>
  
  <p>When I run this in the debugger on my development box, this works fine. If I copy all the relevant files to a separate folder and run it from there, it works fine.</p>
  
  <p>If I copy all the necessary folders to another computer running 32-bit Windows XP SP3 I get the following error:</p>
  
  <pre><code>System.DllNotFoundException: Unable to load DLL 'MyDll.dll': The specified procedure could not be found. (Exception from HRESULT: 0x8007007F)
     at MyNamespace.MyClass.test_dll_call(Int32 invar)
     at MyNamespace.MyClass.Wrap_TestDllCall()
  </code></pre>
  
  <p>I have used dependency walker on both my compiled exe and the dll; the only missing dlls it found were <code>wer.dll</code> and <code>ieshims.dll</code>, which from my research are not needed on XP.</p>
  
  <p>I have installed the VS2012 C++ Redistributable, and .NET 4, and the .NET 4.0.3 update. Still no luck.</p>
  
  <p><strong>Edit</strong> As Hans points out, this <em>appears</em> to be that the app can't find the procedure in the DLL; I have also tried:</p>
  
  <pre><code>[DllImport("MyDll.dll", CallingConvention=CallingConvention.Cdecl)]
  public static extern int test_dll_call(int invar);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>__declspec(dllexport) int __cdecl test_dll_call(int invar);
  </code></pre>
  
  <p>Which also works fine on my dev box but gives the same error on the WinXP box.</p>
  
  <p>Help!</p>