Click here to Skip to main content
15,886,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I have a DLL (native Windows, 32 bit, no COM) which I used to call from a managed C++ wrapper using LoadLibrary and GetProcAddress statements. All went fine, the DLL got loaded, I was able to retrieve the entrypoints and call all the required methods using explicit marshalling. This is on a 32 bit environment, no 64 bits are envolved.

For reasons beyond my control I am forced to rewrite this mechanism using C#, so I created the same (wrapper) classes in C# and created DllImport-attributed signatures for all my DLL functions. However, when I call the first method, I am getting an BadImageFormatException, no matter what function I call. I even declared a non-existing function, but the result was the same exception.

Googling on this topic provides me a lot of 32 vs 64 bit issues, but since my environment is completely 32 bit this solves nothing for me. The only thing that worries me, is when I inspect the DLL using the dependency walker (depends.exe) it shows the following message: "Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.". The IEFRAME.DLL is marked red, but using the LoadLibrary statements I was able to load the DLL.

The declaring statement is:
[DllImport(@"retrievalNLE.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern void SetWebsearchNLETraceLevel(Int32 level);

(but I have also tested with the options CharSet and SetLastError omitted, as well as with every calling convention )


The result is:
System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

The native Windows LastError is 2.

The file and all dependencies are located in the c:\windows\system32 directory.

Any suggestions on how to call such a DLL from C# are greatly appreciated.

Kind regards,
Martin de Jong
The Netherlands
Posted
Updated 11-Mar-10 5:34am
v3

Error code 2 is "cannot find file", so it looks like your DLL is not anywhere that the loader can find it. Try making sure it's in WINDOWS\Sytem32 or provide the full path in your DllImport statement.
 
Share this answer
 
I use to call native function from Windows library this approach:


C#
using System.Runtime.InteropServices;
....
[DllImport("Netapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
       private static extern int NetServerGetInfo(
           string serverName,
           int level,
           out IntPtr pSERVER_INFO_XXX);

....


Also see pinvoke.net
 
Share this answer
 
Thanks for the answers so far regarding the DllImport-use as well as the c:\windows issue. Unfortunately they are not appropriate, so I have updated the question to indicate this.
 
Share this answer
 
MartinDeJong wrote:
The file and all dependencies are located in the c:\windows\system32 directory.


Well the only other answer must be that the dll is somehow corrupt; I can't see any other reason why you would get this error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900