BadImageFormatException:带有 hdf5dll.dll 的 PInvoke ImportDll

发布于 2024-09-13 23:32:20 字数 1463 浏览 5 评论 0原文

好的,我从官方网站下载了 HDF5 库,还有一些 DLL,包括 hdf5dll.dll 和 hdf5_hldll.dll。

我认为在我的类 H5H5LTH5FH5T< 中,有一些围绕本机调用的包装器/代码>。 H5.cs 示例:

namespace HDF5
{
    using hid_t = System.Int32;
    using herr_t = System.Int32;
    using hsize_t = System.UInt64;
    using size_t = System.UInt32;
    // hbool_t is 0:false, +:true
    using hbool_t = System.UInt32;
    // htri_t is 0:false, +:true, -:failure
    using htri_t = System.Int32;

    public class H5
    {
        const CharSet StringMarshallingType = CharSet.Ansi;
        const string DLLNAME = "hdf5dll.dll";

        ///* Functions in H5.c */
        //H5_DLL herr_t H5open(void);
        [DllImport(DLLNAME,
            CharSet = StringMarshallingType)]
        public static extern herr_t H5open();

在 Program.cs 中,我使用 H5.H5open();,但出现 BadImageFormatException。我需要不同的 DLL 吗?方法签名看起来有问题吗?

作为下一步,我想在 C# 中得到这个: http:// www.hdfgroup.org/HDF5/Tutor/h5lite.html

操作系统: Windows 7 64 位
环境: Visual Studio 2008 Professional

更新:我不知道这是否会相关,而且我不记得我的环境是否是VS2008 SP1,但是这个问题可能是解开谜团的关键。我现在正在尝试在家里的 32 位 VS 2010 上重复这个场景。

Ok, I have the HDF5 library downloaded from the official site, and I have a few DLLs, including hdf5dll.dll, and hdf5_hldll.dll.

I have what I think to be some wrappers around the native calls, in my classes H5, H5LT, H5F, and H5T. Example from H5.cs:

namespace HDF5
{
    using hid_t = System.Int32;
    using herr_t = System.Int32;
    using hsize_t = System.UInt64;
    using size_t = System.UInt32;
    // hbool_t is 0:false, +:true
    using hbool_t = System.UInt32;
    // htri_t is 0:false, +:true, -:failure
    using htri_t = System.Int32;

    public class H5
    {
        const CharSet StringMarshallingType = CharSet.Ansi;
        const string DLLNAME = "hdf5dll.dll";

        ///* Functions in H5.c */
        //H5_DLL herr_t H5open(void);
        [DllImport(DLLNAME,
            CharSet = StringMarshallingType)]
        public static extern herr_t H5open();

And in Program.cs, I use H5.H5open();, but I get a BadImageFormatException. Do I need a different DLL? Does the method signature look wrong?

I'd like to, as the next step, get this in C#: http://www.hdfgroup.org/HDF5/Tutor/h5lite.html .

OS: Windows 7 64 bit
Environment: Visual Studio 2008 Professional

Update: I don't know if this will be related, and I don't remember if my environment is VS2008 SP1, but this question may hold a key to solving the mystery. I am as of now trying to repeat the scenario on 32 bit VS 2010 at home.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

公布 2024-09-20 23:32:20

当您尝试从 x64 进程内对适用于 x86 架构的 dll 运行 P/Invoke 操作时,就会发生这种情况,反之亦然。我会检查所有这些,如果它们不同步,请考虑将 HDF5 定位到您的应用程序的处理器,或检查特定于处理器的版本是否可用。

That happens when you're trying to run P/Invoke operations on a dll meant for x86 architecture from within an x64 process or vice versa. I'd check all of that and if they're out of sync, consider targeting the processor that HDF5 targets with your application, or checking if a processor-specific version is available.

薄凉少年不暖心 2024-09-20 23:32:20

从这里查看文档,函数原型是:

herr_t H5open(void);

并且 DLLNAME 也是不允许的,您必须显式指定 dll 名称 - 没有问题。

正确的签名是:

[DllImport("hdf5dll.dll")]public static extern herr_t H5open();

确保您定义了类型 herr_t...

让运行时为您处理编组...

还要确保 DLL 存在于与编译后的 .EXE(您的代码)在其中生成。

编辑:感谢OP指出我的错误......

Looking at the documentation from here, the function prototype is:

herr_t H5open(void);

And also the DLLNAME is disallowed, you must explicitly specify the dll name - no questions asked.

The proper signature is:

[DllImport("hdf5dll.dll")]public static extern herr_t H5open();

Make sure you have the type herr_t defined...

Let the runtime take care of the marshalling for you....

Also make sure, the DLL is present in the same path as where the compiled .EXE (your code) is generated.

Edit: Thanks to the OP for pointing out my blooper....

柏林苍穹下 2024-09-20 23:32:20

在 x64 操作系统上,.net 程序通常以 x64 模式运行。
只需将目标处理器架构设置为 x86,然后重试。
只需在 Visual Studio 中打开“解决方案配置”管理器并添加新的目标平台。

On x64 operatingsystems .net programs usually run in x64 mode.
Just set your target processor architecture to x86 and try again.
Just in Visual studio open your "Solution Configuration"-Manager and add a new target Platform.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文