C#DLLImport错误:尝试加载格式不正确的程序。(HRESULT异常:0x8007000B)。 [英] C# DLLImport Error: An attempt was made to load a program with an incorrect format.( Exception from HRESULT: 0x8007000B).

查看:612
本文介绍了C#DLLImport错误:尝试加载格式不正确的程序。(HRESULT异常:0x8007000B)。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


当我运行我的程序时它说" 试图加载一个格式不正确的程序。(HRESULT异常:0x8007000B)",当我尝试使用它的功能。这是我的代码:

     [  DllImport   " mxgpio.dll"      CharSet     =     CharSet    Unicode  )]   
公开 静态 extern IntPtr mxdgio_open ();

[ DllImport " mxgpio.dll" CharSet = CharSet Unicode )]
公开 静态 extern int mxdgio_get_input_signal IntPtr fd int 端口 );

[ DllImport " mxgpio.dll" CharSet = CharSet Unicode )]
公开 静态 extern 无效 mxdgio_close IntPtr fd );



现在.dll中的函数是C ++,它们使用Handle但我用IntPtr替换它,因为C#没有Handle,所以也许这就是我遇到这个问题的原因:

   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 ));



程序在第一行失败:



IntPtr hDIO = mxdgio_open();



我检查了3次,所有.dll文件都来自x64文件夹,我的电脑运行64位操作系统







我检查了3次,所有.dlls
都来自x64折,而我的电脑运行的是64位操作系统
 

解决方案


您检查了dll,而OS是64位,但您是否检查过您的C#应用​​程序是否已编译为x64平台或是否已编译为"任何CPU"你是否在项目属性页面的构建选项卡上取消选中"首选32位"?


问候,



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 

解决方案

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,


这篇关于C#DLLImport错误:尝试加载格式不正确的程序。(HRESULT异常:0x8007000B)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆