繁体   English   中英

在C#WPF应用程序中使用外部C ++ DLL

[英]Using an External C++ DLL in C# WPF application

全面披露...我是C / C ++ / C#newb。 我一直在linux机器( http://ssdeep.sourceforge.net/ )上玩ssdeep。

Python包装器效果很好( https://python-ssdeep.readthedocs.org/en/latest/usage.html )。 我现在正在尝试编写使用此库的Windows GUI应用程序(C#WPF)。 在Windows Binary下载中,有许多文件,包括DLL和DEF文件。

在API.TXT文件中,作者写道:

Windows ssdeep程序包包括Win32 DLL和.def文件。 尽管MSVC用户无法直接使用DLL,但他们可以使用Microsoft LIB工具轻松创建.lib文件:

C:> lib /机器:i386 /def:fuzzy.def

然后,您可以使用生成的库来编译程序:

C:> cl sample.c模糊.lib

我已经做到了,现在有了fuzzy.dllfuzzy.deffuzzy.expfuzzy.lib 经过大量的搜索之后,我不确定如何在WPF应用程序中实际使用这些文件。

我在解决方案中应放在哪里(无论需要什么文件)? 我是否需要using System.Runtime.InteropServices; 最好是,我会将这个dll或lib打包在我的代码中,因此它不是外部依赖关系,但是在这一点上,我只是很乐意在库中调用一个函数。

编辑:

我发现这个旧链接为我提供了以下代码:

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
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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();
        }
    }
}

这给了我这个错误:

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.

签名不匹配是什么意思? 这是否意味着我的函数的输入与头文件的输入不匹配? 这些是cpp头文件中的函数:

int     fuzzy_hash_filename (const char *filename, char *result)
int     fuzzy_compare (const char *sig1, const char *sig2)

您无法像使用C ++那样链接到C#中的静态库,因此无法将代码打包在一起。 为了使用您的库,您将需要Win32 DLL-如果您只有.lib,则需要为其创建包装DLL。

如果确实有有效的Win32 DLL,则可以使用P / Invoke从C#调用C ++ DLL上的方法。

为此,您需要使用DllImport声明要使用的每个C ++方法。 我无法为您提供确切的语法,因为它取决于您要从DLL中使用的方法。 例如:

DllImport(“ gdi32.dll”,ExactSpelling = true,SetLastError = true)]静态外部IntPtr SelectObject(IntPtr hdc,IntPtr hgdiobj);

在[ http://www.pinvoke.net]上有一个用于标准Win32 DLL的DllImport声明库,可以帮助您入门。

声明后,您可以像在.Net中一样调用方法。 棘手的是如何处理不同数据类型的编组-您将需要了解如何使用IntPtr。

您有一个DLL。 您需要使用PInvoke来调用其中的方法。

我强烈建议您获得亚当·纳特汉斯(Adam Nathans)出色的书,其中详细介绍了pinvoke http://www.amazon.com/NET-COM-Complete-Interoperability-Guide/dp/067232170X

教程中的Pinvoke很简单,在现实生活中可能会变得复杂。 这取决于方法签名

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM