【问题标题】:C dll return a wrong value in C# wpfC dll 在 C# wpf 中返回错误的值
【发布时间】: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块,看看是否抛出异常。

标签: c# .net wpf pinvoke


【解决方案1】:

p/invoke 代码在所有 .net 应用程序中的处理方式完全相同。因此,显然这两个程序之间的区别在于其他地方。一些可能的原因包括:

  1. 程序正在寻找不同版本的本地 DLL。
  2. 程序使用两个 XML 文件的不同版本。
  3. p/invoke 声明在某种程度上不正确,.net 版本和/或操作系统的更改恰好突出了一个程序中的错误,而不是另一个程序中的错误。

要追踪这一点,您应该努力找出两个应用程序之间的众多差异中究竟是哪一个触发了行为变化。

【讨论】:

    【解决方案2】:

    您是否在 DLLImport 中使用了正确的字符串类型? 您应该使用

    指定 ANSI 或 Unicode
    CharSet=Ansi
    

    CharSet=Unicode
    

    在 DLLImport 属性中。请参阅此MSDN topic 了解更多信息。

    【讨论】:

    • 两个程序都一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多