title: C++ Dll in WPF C# application
body: <p>After looking for solutions already proposed to a similar question to mine, 
  and since this is the first time I'm using a non-.NET DLL in a .NET application, 
  I really need your help.</p>
  
  <p>I have an WPF application, using MVVM Pattern, and in my <code>ViewModel</code> class I need to use a DLL done in C++ to recover a token. I have an example in C++ that uses this DLL, so I have the method's names, but I can't do the same in C#. I know that I must use <code>DllImport</code> to use this methods, but how implement it and use the pointer in C#??</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  int main()
  {
      HINSTANCE hinst = NULL;
  
      typedef bool ( *GetTokenProto )( char ** );
      typedef void ( *FreeTokenProto )( char * );
  
      GetTokenProto GetToken;
      FreeTokenProto FreeToken;
  
      std::string str = "DllName.dll";
      std::string token;
  
      if ( (hinst = LoadLibraryA(str.c_str()) ) )
      {
          GetToken = (GetTokenProto) GetProcAddress(hinst, "GetToken");
          FreeToken = (FreeTokenProto) GetProcAddress(hinst, "FreeToken");
  
          if (GetToken &amp;&amp; FreeToken)
          {
              char *buf;
              if (GetToken(&amp;buf))
              {
                  token = buf;
                  FreeToken(buf);
  
                  std::cout &lt;&lt; "Token:" &lt;&lt; token &lt;&lt; std::endl;
              }
              else
              {
                  std::cerr &lt;&lt; "DLL loaded but no token" &lt;&lt; std::endl;
                  exit(1);
              }
          }
          else
          {
              std::cerr &lt;&lt; "DLL loaded but missing proc address(es)" &lt;&lt; std::endl;
              exit(1);
          }
  
          FreeLibrary(hinst);
      }
      else
      {
          std::cerr &lt;&lt; "Failed to load DLL" &lt;&lt; std::endl;
          exit(1);
      }
      return 0;
  }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <pre><code>[DllImport("DllName.dll", EntryPoint = "GetToken", CallingConvention = CallingConvention.Cdecl)] 
  public static extern bool get_token(ref string token); 
  
  [DllImport("DllName.dll", EntryPoint = "FreeToken", CallingConvention = CallingConvention.Cdecl)] 
  public static extern void free_token(ref string token);
  
  public static string a_token; 
  public string get_token_method() 
  { 
      try 
      { 
          string buffer = null; 
          if (get_token(ref buffer)) 
          { 
              a_token = buffer; 
              free_token(ref buffer); 
              Debug.WriteLine("token : " + a_refresh_token); 
          } 
          else 
          { 
              Debug.WriteLine("DLL Loaded but no token"); 
          } 
      } 
      catch (Exception ex) 
      { 
         Debug.WriteLine("\n" + ex.Message); 
      } 
      return a_refresh_token; 
  }
  </code></pre>
  
  <p><strong>The error</strong></p>
  
  <blockquote>
    <p>I have an Exception "System.DllNotFoundException" : Unable to load DLL
    'DllName.dll': The specified module could not be found. (Exception
    from HRESULT: 0x8007007E).</p>
  </blockquote>
  
  <p>Dll file is in the same folder of the .exe (..\bin\Debug) </p>