title: Call unmanaged C++ VS 6.0 MFC dll from C#
body: <p>I have an unmanaged C++ MFC dll that was developed in VS 6.0. I would like to use it in my C# app. I'm trying to use PInvoke.</p>
  
  <p>Here is the C++ code:</p>
  
  <pre><code>// testDll.cpp : Defines the entry point for the DLL application.
  //
  #include "stdafx.h"
  
  
  extern "C" {
  
  BOOL APIENTRY DllMain( HANDLE hModule, 
                         DWORD  ul_reason_for_call, 
                         LPVOID lpReserved
                       )
  {
      return TRUE;
  }
  
   __declspec(dllexport) int test(int a)
  {
      return a * a;
  }
  }
  </code></pre>
  
  <p>And here is how I'm trying to invoke the method "test" from C#:</p>
  
  <pre><code>// PInvokeTest.cs
  using System;
  using System.Runtime.InteropServices;
  
  class PlatformInvokeTest
  {
  
      [DllImport("TestDll.dll")]
      internal static extern int test(int number);
  
      public static void Main()
      {
          Console.WriteLine(test(5));     
      }
  }
  </code></pre>
  
  <p>This approach works just fine when I set C++ dll to be just a regular Win32 dll.</p>
  
  <p>But once I change the project type to MFC  ("Use MFC in a Shared DLL") I'm getting this error:</p>
  
  <p><strong>Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'TestDll.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
     at PlatformInvokeTest.test(Int32 number)</strong></p>
  
  <p>Thanks!</p>