【发布时间】:2013-10-30 08:21:01
【问题描述】:
更新:我发现它只是在我使用 .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());
}
}
}
【问题讨论】:
-
“dll返回错误结果”是什么意思?
-
是 DLL 返回不正确,还是 video_analyze 返回 -1?尝试移除video_analyze中的catch块,看看是否抛出异常。