PInvoke c++来自 c# 的 dll - 尝试加载格式不正确的程序。 (HRESULT 异常:0x8007000B)

发布于 2024-11-03 21:22:55 字数 669 浏览 4 评论 0原文

尝试使用 Dll 导入从 C# 访问旧的 C++ DLL。 已经在网上搜索了大约4天,但没有成功。

我正在尝试使用来自 asp.net mvc 3 网站 .net 4 的 c++ dll。

我已将平台目标更改为 x86。 我已在 iis 中的应用程序池上将“启用 32 位应用程序”设置为 true。

将 Windows Server 2008 R2 与 iis 7.5 一起使用

是否有某种方法可以调试此问题或获取有关可能出现问题的更多信息?

链接ASP.NET P/Invoke“尝试加载格式不正确的程序” 指出:

“要使用 32 位 dll,您的 Web 应用程序必须在 32 位模式下运行,即通过将 IIS 切换为以 32 位模式运行来完成”,并提供了有关如何更改此设置的链接,但这似乎只涉及 IIS 6 和 asp.net 2

提前致谢! 最好的问候

编辑1: C# 中 C++ 方法调用的错误声明是否会导致此错误?阅读错误后,我们可能会猜测,调用具有 3 个参数但应接收 4 个参数的函数会导致此错误。

Trying to access an old c++ dll from c# using Dll Import.
Been searching around the net for aprox 4 days now without success.

I'm trying to use the c++ dll from an asp.net mvc 3 website .net 4.

I've changed platform target to x86.
I've set "Enable 32 bit application" to true on the application pool in iis.

Using Windows Server 2008 R2 with iis 7.5

Is there some way to debug this or get more info about what might be wrong?

The link ASP.NET P/Invoke "An attempt was made to load a program with an incorrect format" states that:

"To use the 32 bit dll, your web application has to run in 32 bit mode, which is done by switching IIS to run in 32 bit mode" and gives a link about how you can change this, however this seems only to concern IIS 6 and asp.net 2

Thanks in advance!
Best Regards

Edit 1:
Would an erroneous declaration of the c++ method calls in c# result in this error? Reading the error one could guess that calling a function with 3 params that should receive 4 would result in this error.

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

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

发布评论

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

评论(1

拥抱没勇气 2024-11-10 21:22:55

好回答你的最后一个问题:

C# 中 C++ 方法调用的错误声明是否会导致此错误?

不,我不相信它会 - C++ 方法的错误声明可能会导致发生许多事情,从正确执行函数到引发互操作异常,但我不相信会引发此异常- 这是加载 dll 时出现的特定问题。

您应该检查以确保尝试加载此 dll 的进程实际上是一个 32 位进程,方法是通过某种方式获取进程 ID(例如,通过捕获并记录异常),然后在任务管理器中进行检查。如果该进程实际上是 64 位进程,那么您可能配置不正确。

您还应该检查以确保您尝试加载的 dll(及其所有依赖项)实际上是 32 位的 - 我相信如果您尝试将 64 位 dll 加载到 32 位中,您也会遇到类似的问题位进程。您应该能够使用 Dependency walker(一个非常有用的全能工具)来检查这一点。

Well to answer your last question:

Would an erroneous declaration of the C++ method calls in c# result in this error?

No, I don't believe that it would - erroneous declaration of the C++ method could result in a number of things happening ranging from the function executing correctly to an interop exception being thrown, however I don't believe that this exception would be thrown - this is specific to a problem loading the dll.

You should check to make sure that the process attempting to load this dll is actually a 32-bit process by somehow obtaining the process ID (e.g. by catching and logging the exception), and then checking in task manager. If the process is actually a 64-bit process then you may have incorrectly configured something.

You should also check to ensure that the dll you are attempting to load (and all its dependencies) are in fact 32-bit - I believe that you will experience a similar issue if you attempt to load a 64-bit dll into a 32-bit process. You should be able to use Dependency walker (an all-round very useful tool) to check this.

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