简体   繁体   中英

c# calling LoadLibrary from a 64-bit process to load a 64-bit library fails

I'm having a problem on a machine while attempting at loading a library with a P/Invoke call to LoadLibrary.

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr LoadLibrary(string dllToLoad);

This is my configuration. The calling assembly ( A ) is compiled in x64 and it calls another assembly ( B ) compiled as AnyCPU. From B I call LoadLibrary(dll_C_Path) to a library C that is 64-bit. All this works on my machine running Win10 64-bit, but it fails on another machine running Win7 64-bit with the following error (after calling GetLastError ): "%1 is not a valid Win32 application" .

On B , before calling LoadLibrary I've verified that Environment.Is64BitProcess=true . I've opened the library C with DependencyWalker and it appears 64-bit. But the most strange thing to me is that on one machine works while on another not. What can be the cause?

EDIT

I'm passing the full absolute path to LoadLibrary . Here below a screenshort from Depency Walker (library C is aec.dll). I'm not used to it, but one thing I noticed is that the msvcr120.dll, which aec.dll depends on, has not the icon of a 64-bit. Can this give some hint?

在此处输入图片说明

There are two possible explanations:

  1. Your code finds a 32 bit DLL, or an otherwise invalid image. We don't know whether you rely of the DLL search path or specify an absolute path.
  2. Your code finds a 64 bit DLL but when it resolves its dependencies, a 32 bit or otherwise invalid module is found.

You should do some debugging to work out what is wrong. I would write a simple C++ program to load the library and avoid the extra complexity of p/invoke. I'd use Dependency Viewer in profile mode to determine which dependency is not valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM