Loading native dll (32bit) in in-process wmi provider(32 bit) on 64 bit OS
I am working on a in-process wmi provider which loads a native third party library using[DllImport]. The dll locations is fixed to c:\mydllpath. Before loading the dll I set current directory to the dll loaction, in the provider's Bind() method.
Environment.CurrentDirectory = Environment.ExpandEnvironmentVariables("%SystemDrive%") + "\mydllpath";
Provider is built for 'Any CPU', installed using gacutil, and installexe.
gacutil.exe /if myprovider.dll
installutil.exe myprovider.dll
It is working fine in all 32 bit Windows platforms. But in 64 bit platform (tried with Windows 7 64bit), it is not working when I test from WMIC. But when I tested with WMI Code creator, it was working fine.
After debugging I found it is throwing following error.
System.BadImageFormatException Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
After searching help from internet, it was suggested to build the provider for x86. But provider built with x86 is giving another error like...
ERROR: Code = 0x80041013 Description = Provider load failure Facility = WMI
I also tried to load the native dlls(actually 2 dlls, and a sys file) using /linkref commandline switch, but failed because of not possible to load the sys file using /linkref.
I wrote a test C# client application and keep all files ina same path, it was working fine. I hope I am missing some configurations for 64 bit installation.
开发者_如何学编程Any help would be really appreciated and very thanks in advance!
Do you need the code to be 64 bit? You can tell Visual Studio to compile your c# code to a 32 bit executable, then use the 32 bit native DLL's. This will work fine on a 64 bit OS.
.NET by default compiles .net IL to whatever the native instruction set is. In the project settings on the build tab, if the build target is set for "Any CPU" then it will compile to 64 bit on 64 bit platforms, and 32 bit on 32 bit platforms.
If you change this and force 32 bit compilation (set it to x86), then it will compile to 32 bit even on 64 bit platforms, allowing you to use your 32 bit DLL's.
UPDATE: Upon re-reading your question, you state you're building an in-process WMI provider. This would indicate that the provider might need to be 64 bit on 64 bit platforms (I don't know if that's the case). If so, then yeah.. you're out of luck.
UPDATE2: This article seems to indicate that WMI providers need not be 64 bit on 64 bit OS's except in "rare circumstances", so maybe you can get away with a 32 bit provider.
sorry pal, out of luck here. If your process (the main process, everything started) is running 64 bit you can not load 32 bit native dlls.
here is an article describing some of the difficulties
精彩评论