Upadte:我发现它只是在我使用.net framework 3.5或更低版本时发生的,但我应该使用.net framework 3.5。
我有一个分析我的文件的c DLL 。
我编写了一个 C# Windows 窗体,它是打开文件并将文件路径发送到我的 DLL 并显示结果。我需要使用 WPF 并编写相同的WPF,而无需对我的代码进行任何更改。但是DLL 返回错误的结果。
我在网上搜索,但找不到任何答案。
Windows 窗体代码:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
static string cascade_name_face = "cascade_name_face.xml";
static string cascade_name_face2 = "cascade_name_face2.xml";
[DllImport("aref_video_score", CallingConvention = CallingConvention.Cdecl)]
public static extern double aref_video_score(String fileName, int a, String cascade_name_face, String cascade_name_face2);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "All file|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
Address1.Text = dlg.FileName;
}
}
private double video_analyze(string filename)
{
try
{
return aref_video_score(filename, 20, cascade_name_face, cascade_name_face2);
}
catch
{
return -1;
};
}
private void button2_Click(object sender, EventArgs e)
{
if (IsVideo(Address1.Text))
MessageBox.Show("This is a 'Video' and video's scroe is " + video_analyze(Address1.Text).ToString());
}
}
}
WPF代码:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Linq;
using System.Windows;
namespace WpfApplication7
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
static string cascade_name_face = "cascade_name_face.xml";
static string cascade_name_face2 = "cascade_name_face2.xml";
[DllImport("aref_video_score", CallingConvention = CallingConvention.Cdecl)]
public static extern double aref_video_score(String fileName, int a, String cascade_name_face, String cascade_name_face2);
private void Button_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.Filter = "All file|*.*";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
// Open document
string filename = dlg.FileName;
Address1.Text = filename;
}
}
private double video_analyze(string filename)
{
try
{
return aref_video_score(filename, 20,cascade_name_face, cascade_name_face2);
}
catch
{
return -1;
};
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if(IsVideo(Address1.Text))
MessageBox.Show("This is a 'Video' and video's scroe is " + video_analyze(Address1.Text).ToString());
}
}
}