title: Using an External C++ DLL in C# WPF application
body: <p>Full disclosure... I'm a C/C++/C# newb.
  I have been playing around with ssdeep on my linux box (<a href="http://ssdeep.sourceforge.net/" rel="nofollow">http://ssdeep.sourceforge.net/</a>).</p>
  
  <p>The Python wrapper works great (<a href="https://python-ssdeep.readthedocs.org/en/latest/usage.html" rel="nofollow">https://python-ssdeep.readthedocs.org/en/latest/usage.html</a>). I am now trying to write a Windows GUI application (C# WPF) that uses this library. In the Windows Binary download, there are a number of files, including a DLL and a DEF file. </p>
  
  <p>In the API.TXT file, the author writes:</p>
  
  <blockquote>
    <p>The Windows ssdeep package includes a Win32 DLL and a .def file.
    Although MSVC users can't use the DLL directly, they can easily create
    a .lib file using the Microsoft LIB tool:</p>
    
    <p>C:> lib /machine:i386 /def:fuzzy.def</p>
    
    <p>You can then compile your program using the resulting library:</p>
    
    <p>C:> cl sample.c fuzzy.lib</p>
  </blockquote>
  
  <p>I have done this, and I now have <code>fuzzy.dll</code>, <code>fuzzy.def</code>, <code>fuzzy.exp</code>, and <code>fuzzy.lib</code>. After much googling, I am unsure how to actually use these files in my WPF application.</p>
  
  <p>Where do I put it (whatever file I need) in my solution? Do I need to use <code>using System.Runtime.InteropServices;</code>? Preferably, I'd be packaging this dll or lib in my code so its not an external dependency, but at this point, I'd just be happy to call a function in the library.</p>
  
  <p>Edit:</p>
  
  <p>I found <a href="http://jessekornblum.livejournal.com/249104.html?view=704016#t704016" rel="nofollow">this old link</a> which gave me this code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  using System.Runtime.InteropServices;
  
  
  namespace FuzzyBear
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
  
  
          [DllImport("fuzzy.dll")]
          public static extern int fuzzy_hash_filename([MarshalAs(UnmanagedType.LPStr)] string fname, [MarshalAs(UnmanagedType.LPStr)] string result);
  
  
          [DllImport("fuzzy.dll")]
          public static extern int fuzzy_compare(string sig1, string sig2);
  
  
  public MainWindow()
          {
              string result = "";
              int test = fuzzy_hash_filename("C:\\dev\\tools\\ssdeep-2.13\\API.txt", result);
              System.Diagnostics.Debug.Write("Lookie here: ");
              System.Diagnostics.Debug.WriteLine(test.ToString());
              InitializeComponent();
          }
      }
  }
  </code></pre>
  
  <p>Which gives me this error:</p>
  
  <pre><code>Additional information: A call to PInvoke function 'FuzzyBear!FuzzyBear.MainWindow::fuzzy_hash_filename' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
  </code></pre>
  
  <p>What does it mean when the signature doesn't match? Does it mean that my function's input doesn't match the <a href="http://ssdeep.sourceforge.net/api/html/fuzzy_8h.html" rel="nofollow">header file's</a> input? These are the functions from the cpp header file:</p>
  
  <pre><code>int     fuzzy_hash_filename (const char *filename, char *result)
  int     fuzzy_compare (const char *sig1, const char *sig2)
  </code></pre>