title: Calling C++ DLL from C# is ok under Windows 7 but fails under Windows 10
body: <p>My program calls a C++ DLL from my C# program.</p>
  
  <p>The problem is that the generated executable is running fine undex Windows 7 but not under Windows 10 !?</p>
  
  <p>The steps are listed below:</p>
  
  <p>I compile my C++ DLL using g++ (of TDM-GCC-64) in 64 bits.
  I compile my C# program using Visual studio 15 and target .NET framework 4.5.2 in 64 bits.</p>
  
  <p>The C++ DLL code is :</p>
  
  <h2>main.h</h2>
  
  <pre><code>#ifndef __MAIN_H__
  #define __MAIN_H__
  
  #include &lt;windows.h&gt;
  
  /*  To use this exported function of dll, include this header in your project.  */
  
  #ifdef BUILD_DLL
      #define DLL_EXPORT __declspec(dllexport)
  #else
      #define DLL_EXPORT __declspec(dllimport)
  #endif
  
  
  #ifdef __cplusplus
  extern "C"
  {
  #endif
  
  void DLL_EXPORT SomeFunction(const LPCSTR sometext);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif // __MAIN_H__
  </code></pre>
  
  <h2>main.cpp</h2>
  
  <pre><code>#include "main.h"
  #include &lt;iostream&gt;
  
  // a sample exported function
  void DLL_EXPORT SomeFunction(const LPCSTR sometext)
  {
      std::cout &lt;&lt; "TEST FROM DLL : " &lt;&lt; sometext &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>The build command is :
  C:\TDM-GCC-64\bin\g++ -shared -o ..\TestDllCall\bin\x64\Debug\myDLL.dll -m64 -g  -D BUILD_DLL  -L. main.cpp</p>
  
  <p>You can notice that the dll is created directly in the target directory of the c# test program (Debug in 64 bits).</p>
  
  <p>The C# main program is:</p>
  
  <h2>Program.cs</h2>
  
  <pre><code>using System;
  using System.IO;
  using System.Runtime.InteropServices;
  
  namespace TestHstLibrary
  {
      class MainProg
      {
          static void Main(string[] args)
          {
              ProgramTest ProgramTest = new ProgramTest();
              ProgramTest.dllCall();
              Console.ReadKey();
          }
      }
  
      class ProgramTest
      {
          [DllImport("myDLL.dll", EntryPoint = "SomeFunction")]
          static extern void SomeFunction(string sometext);
  
          public ProgramTest() {            
          }
  
          public void dllCall()
          {
              Console.WriteLine("dllCall ... ");            
              try
              {
                  SomeFunction("Hello !");
              } catch (Exception e)
              {
                  Console.WriteLine("EXCEPTION : " + e.Message);
                  Console.WriteLine(e.ToString());
              }
              Console.WriteLine("");
          }        
      }
  }
  </code></pre>
  
  <p>Note : The build is done on the final target plateform : Win10 64bits.</p>
  
  <p>Running on my Windows 10, I have the following :</p>
  
  <blockquote>
    <p>dllCall ...<br>
    EXCEPTION : Unable to load DLL 'myDLL.dll': A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT : 0x8007045A)
    System.DllNotFoundException: Unable to load DLL 'myDLL.dll': A dynamic link library (DLL) initialization routine failed. (Exception de HRESULT : 0x8007045A)
       à ProgramTest.SomeFunction(String sometext)
       à ProgramTest.dllCall() dans C:\TestDllCall\TestDllCall\Program.cs:ligne 30</p>
  </blockquote>
  
  <p>After a copy of the entire build directory from Win10 to a Win7,
  running it on my Win7, I have the following :</p>
  
  <blockquote>
    <p>dllCall ... <br>
    TEST FROM DLL : Hello !</p>
  </blockquote>
  
  <p>It's working fine.</p>
  
  <p>If someone has an idea why it fails under Win10 and not under Win7, I will be pleased to have the answer.</p>
  
  <p>I check with dependency walker and had the following:
  - Under Windows 10, some dependencies are missing even if it has been generated under Win10
  - Under Windows 7, all dependencies are ok.</p>
  
  <p>So I try with an other c++ compiler from g++ of TDM-GCC-64, I tested with the one from cygwin : does not give a better result, even worse.</p>
  
  <p>I also try to pass my c# string parameter as a IntPtr as shown below:</p>
  
  <pre><code>IntPtr myptr = Marshal.StringToHGlobalAnsi("Hello !");
  SomeFunction(myptr);
  </code></pre>
  
  <p>But it does not work either under Win10 but still working under Win7.</p>
  
  <p>An other test was to remove the std::cout form my dll, finally the call is ok but I still want to make it work as this is in a test environment and in a production environment I will have to make it with an external dll which I don't have the source code.</p>