0

I need to build a gui to communicate via bulk USB from my Windows PC to a PIC microcontroller. I'm trying to use mpusbapi.dll as I see over diferent tutorials on internet, but I can´t acommplish to succesfully reference the dll in my project. VS 2015 show me this error: "mpusbapi.dll" could not be added. Make sure that the file is accessible, and that its a valis assembly or COM component.

I did research and i figured out the problem was the unmanaged dll, so I tried to reference via DllImport. But at this time, that did not work either.

I share my code below expecting someone could help me or give me some reference to a better way to accomplish my objective with 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);
            }            
        }
    }
} 

The try catch statetment give me the follow result: Error after try catch statetment

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

I don´t undesrtand because I just added the file in my project.

Thanks people!

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
  • Is the dll in the directory of the compiled exe? – Fruchtzwerg Jun 08 '17 at 19:27
  • do you mean inside the project folder? If so, yes – Arturo Rosete Jun 08 '17 at 20:07
  • No I mean insinde the compiled folder. Set the build action of the dll to copy. – Fruchtzwerg Jun 08 '17 at 20:26
  • Ok, the file was not in Copy mode, so I've changed it to Copy always and the Build action property set to Content. Now I get another error... _"An attempt was made to load a program with an incorrect format. Exception HRESULT:0x8007000B"_ Is it another configuration problem? Or is it better if I look for another way to achieve USB communication? – Arturo Rosete Jun 09 '17 at 05:21
  • Check out this answer: https://stackoverflow.com/questions/11370344/could-not-load-file-or-assembly-an-attempt-was-made-to-load-a-program-with-a – Fruchtzwerg Jun 09 '17 at 07:39
  • Thanks a lot @Fruchtzwerg, my problem was that the mpusbapi.dll had be built on 32-bits, and my program is built over 64-bits. – Arturo Rosete Jun 09 '17 at 15:28

1 Answers1

0

The problem was that the .dll have not being copied to the output folder, as Fruchtzwerg said. So I just changed the properties of the .dll file as I say in the comments.

Then [see comments] I had a 32-bit / 64-bit conflict with the mpusbapi.dll The problem was solve using this answer:Could not load file or assembly ... An attempt was made to load a program with an incorrect format (System.BadImageFormatException)