none
C# DLLImport Error: An attempt was made to load a program with an incorrect format.( Exception from HRESULT: 0x8007000B). RRS feed

  • Question

  • when I run my program it says " An attempt was made to load a program with an incorrect format.( Exception from HRESULT: 0x8007000B)", when I try to use the functions from it. here is my code :

        [DllImport("mxgpio.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr mxdgio_open();
    
        [DllImport("mxgpio.dll", CharSet = CharSet.Unicode)]
        public static extern int mxdgio_get_input_signal(IntPtr fd, int port);
    
        [DllImport("mxgpio.dll", CharSet = CharSet.Unicode)]
        public static extern void mxdgio_close(IntPtr fd);

    Now the functions in the .dll are C++ and they use Handle but I replace that with IntPtr because C# does not have Handle, so maybe that is why I am having this problem:

    IntPtr hDIO = mxdgio_open();
    
            Murphy_Switch1 = mxdgio_get_input_signal(hDIO, 0); 
            AppendTextBox(Convert.ToString(Murphy_Switch1));
            Murphy_Switch2 = mxdgio_get_input_signal(hDIO, 1);
            AppendTextBox(Convert.ToString(Murphy_Switch2));
            Flow_Indicator_Switch = mxdgio_get_input_signal(hDIO, 2);
            AppendTextBox(Convert.ToString(Flow_Indicator_Switch));

    The program fails at that 1st line:

    IntPtr hDIO = mxdgio_open();

    I checked 3 times and all the .dlls files are from the x64 folder and my computer run a 64 bit OS

    I checked 3 times and all the .dlls are from the x64 fold and my computer runs a 64-bit OS 


    • Edited by Btb4198 Monday, November 13, 2017 4:11 PM
    Monday, November 13, 2017 3:42 PM

All replies

  • Hi,

    You checked the dll and theOS are 64bits, but did you check your C# application is compiled to x64 platform or if it's compiled "Any CPU" did you uncheck the "Prefer 32bits" on the build tab of the project properties page ?

    Regards,


    Yan Grenier

    Merci de bien vouloir "Marquer comme réponse", les réponses qui ont répondues à votre question, et de noter les réponses que vous avez trouvé utiles.

    Monday, November 13, 2017 4:15 PM
  • it is 64 bit, will I am not telling it to do 32 bit

    Monday, November 13, 2017 4:23 PM
  • ok I change it from any CPU in to 32 bit and it seem to be working

    but I do not understand why I had to ?

    Monday, November 13, 2017 4:42 PM
  • For a good way to determine the bitness of both native Dlls and .Net assemblies see https://stackoverflow.com/questions/480696/how-to-find-if-a-native-dll-file-is-compiled-as-x64-or-x86
    Monday, November 13, 2017 4:49 PM
  • ok I published the program and now I am running it on another computer but I am getting a error that says " unable to load "mxgpio.dll" the specified module could not be found exception from hresult 0x8007007e"

    however everything was just working  in debug mode on my developer computer. 

     
    Monday, November 13, 2017 5:23 PM
  • Does mxgpio.dll exist on the other machine?  If so, is it in a location where it will be found?  See https://stackoverflow.com/questions/2864673/specify-the-search-path-for-dllimport-in-net
    Monday, November 13, 2017 5:43 PM
  • It is,

    I have it in Systems32  and where the application.exe is but that is not working


    • Edited by Btb4198 Monday, November 13, 2017 6:47 PM
    Monday, November 13, 2017 6:43 PM
  • How do I get the Publish Wizard to handle that ?

    Monday, November 13, 2017 6:50 PM
  • [DllImport("mxdgio.dll", SetLastError = true)]
            static extern bool SetDllDirectory(@"C:\Windows\System32");

    ok this code does not work...

    it does not like @"C:\Windows\System32" nor "C:\\Windows\\System32"

    Monday, November 13, 2017 6:59 PM
  • Does the mxgpio.dll have dependencies on other dlls that exist on your development system but are not present on the other system?

    Monday, November 13, 2017 7:04 PM
  • it does but I copy them along when I copy mxgpio.dll.

    I added a new  Environment Variable called Path -> C:\Windows\Systm32

    but nothing!!!

    Monday, November 13, 2017 7:11 PM

  • I added a new  Environment Variable called Path -> C:\Windows\Systm32


    This doesn't make sense.  By default the system contains a PATH environment variable.

    What about the 32 bit code issue?

    And how do you know that you have copied all of the dependent dlls?
    • Edited by RLWA32 Monday, November 13, 2017 7:26 PM
    Monday, November 13, 2017 7:25 PM
  • And how do you know that you have copied all of the dependent dlls?

    because is works on my labtop and at one point I was getting the same error on my laptop.

    This doesn't make sense.  By default the system contains a PATH environment variable.

    there moved one in there. I deleted some.

    What about the 32 bit code issue?

    I more the file over to the SysWOW64 but it did not work


    • Edited by Btb4198 Monday, November 13, 2017 7:45 PM
    Monday, November 13, 2017 7:32 PM
  • Monday, November 13, 2017 7:43 PM
  • Monday, November 13, 2017 7:44 PM
  • see ?
    Monday, November 13, 2017 7:44 PM
  • it works on my labtop :

    Monday, November 13, 2017 7:50 PM
  • Can you see the pictures ?

    also, 

    I moved the files over to where the .exe file is and that did not help. That is what I have to do on my lap tab to get it to work. I move the .dll files to the bebug folder because that is where my .exe file is and it works ...

    but not on the other computer.  

    Monday, November 13, 2017 8:16 PM
  • The fact that something "works" on your laptop is irrelevant when it comes to resolving issues on a different system.

    You have not answered how you determined exactly which dlls mpxgio.dll is dependent upon to ensure that ALL of them are present on the other system.

    Also, you have not definitively stated whether you are dealing with 32 bit or 64 bit code.  It seems you don't know so you try everything you can think of.

    Deleting entries from the System PATH environment variable is a BAD idea.  You should restore what you removed.

    Good luck. :)

    Monday, November 13, 2017 8:18 PM
  • ok I got it to work !!!

    I had to move all the .dll files to some weird folder with numbers and letters as a name.

    it is the same folder that had my program other .dll files... the one that are C# ...


     

    Monday, November 13, 2017 8:25 PM
  • why did  visual studio not add them there  for me ?
    Monday, November 13, 2017 8:26 PM
  • why did  visual studio not add them there  for me ?

    Hi Btb4198,

    You could try to specify the path of the dll in your code instead of copy the dll to a specific folder.

    And check the platform again. Here is the link of platform for your reference.

    https://stackoverflow.com/questions/18007967/net-framework-error-hresult-0x8007000b

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 22, 2017 1:54 AM
    Moderator