【问题标题】:Can´t load mpusbapi.dll on C# .NET WPF project无法在 C# .NET WPF 项目上加载 mpusbapi.dll
【发布时间】:2017-06-08 19:14:47
【问题描述】:

我需要构建一个 gui 以通过批量 USB 从我的 Windows PC 与 PIC 微控制器进行通信。我正在尝试使用 mpusbapi.dll,因为我在互联网上看到了不同的教程,但我无法在我的项目中成功引用 dll。 VS 2015 显示此错误:"mpusbapi.dll" 无法添加。确保该文件是可访问的,并且它是一个 valis 程序集或 COM 组件。

我做了研究,发现问题出在非托管 dll 上,所以我尝试通过 DllImport 进行引用。但此时,这也不起作用。

我在下面分享我的代码,希望有人可以帮助我或给我一些参考,以更好地使用 USB 实现我的目标。

using System.Runtime.InteropServices;

namespace GUI_ROBOT
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {        
        public MainWindow()
        {            
            InitializeComponent();                   
        }

        //Problem here
        [DllImport("mpusbapi.dll",CallingConvention = CallingConvention.Cdecl)]       
        static extern UInt32 MPUSBGetDLLVersion();

        [DllImport("mpusbapi.dll")]
        static extern UInt32 MPUSBGetDeviceCount(string pVID_PID);               

        private void btnStart_Click(object sender, RoutedEventArgs e) 
        {
            try
            {
                string dCount = MPUSBGetDLLVersion().ToString();                
                listBox1.Items.Add(dCount);                
            }
            catch (Exception j)
            {
                System.Windows.MessageBox.Show("Error: " + "\nMessage = " + 
j.Message);
            }            
        }
    }
} 

try catch 声明给了我以下结果: Error after try catch statetment

Translate "Can´t load the .dll file 'mpuasbapi.dll': Can´t find the specified module."

我不明白,因为我刚刚在我的项目中添加了文件。

谢谢大家!

【问题讨论】:

  • dll是否在编译好的exe目录下?
  • 你的意思是在项目文件夹里面吗?如果是这样,是的
  • 不,我的意思是在已编译的文件夹中。将dll的构建动作设置为复制。
  • 好的,该文件未处于复制模式,因此我将其更改为始终复制并将构建操作属性设置为内容。现在我收到另一个错误... “尝试加载格式不正确的程序。异常 HRESULT:0x8007000B” 是另一个配置问题吗?或者如果我寻找另一种方式来实现 USB 通信会更好?

标签: c# .net wpf dll usb


【解决方案1】:

问题在于 .dll 没有被复制到输出文件夹,正如 Fruchtzwerg 所说。所以我只是按照我在 cmets 中所说的那样更改了 .dll 文件的属性。

然后[见 cmets] 我与 mpusbapi.dll 发生 32 位/64 位冲突问题已使用此答案解决:Could not load file or assembly ... An attempt was made to load a program with an incorrect format (System.BadImageFormatException)

【讨论】:

    猜你喜欢
    • 2017-09-07
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 2022-11-22
    • 2020-03-28
    • 1970-01-01
    相关资源
    最近更新 更多