簡體   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