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());
}
}
}
答案 0 :(得分:0)
p / invoke代码在所有.net应用程序中以完全相同的方式处理。所以,显然两个项目之间的区别在于其他地方。一些可能的原因包括:
要跟踪此情况,您应该努力确切地隔离两个应用程序之间的许多差异触发行为更改。
答案 1 :(得分:0)
您是否在DLLImport中使用了正确的字符串类型? 您应该使用
指定ANSI或UnicodeCharSet=Ansi
或
CharSet=Unicode
DLLImport属性中的。有关详细信息,请参阅此MSDN topic。