无法在 C# .NET WPF 项目上加载 mpusbapi.dll

Can´t load mpusbapi.dll on C# .NET WPF project

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

我做了研究,发现问题出在非托管 dll,所以我尝试通过 DllImport 进行引用。但是此时,那也没有用。

我在下面分享我的代码,希望有人能帮助我或给我一些参考,以更好地完成我的 objective 使用 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

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

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

谢谢大家!

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

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