PInvoke c++ dll from c# - An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

感情迁移 提交于 2020-01-02 06:55:14

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/5811955/pinvoke-c-dll-from-c-sharp-an-attempt-was-made-to-load-a-program-with-an-inc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!