3

I have an SDK from a vendor, of which I am using a particular DLL which provides COM interop with the vendor system.

I have successfully created a POC C# project using classes in this DLL. I add the reference to the DLL, add the using clause for the namespace, voila. My target framework is 4.0 and platform is x86.

I now want to do use Powershell to achieve the same task, however I cannot get the DLL to load.

When I try

add-type -path $myDLL

or

[System.Reflection.Assembly]::LoadFrom($myDLL)

I get the following exception:

add-type : Could not load file or assembly 'file:///C:\Program Files 
(x86)\Interwoven\WorkSite\IManage.dll' or one of its dependencies. The module was expected to contain 
an assembly manifest.
At line:2 char:1
+ add-type -path $myDLL
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type], BadImageFormatException
    + FullyQualifiedErrorId : System.BadImageFormatException,Microsoft.PowerShell.Commands.AddTypeComma 
   nd

I have confirmed I'm running in Powershell x86 and i even tried forcing CLRVersion to match the DLL ImageRuntimeVersion using the method described here with no success.

What else am I missing here? Thanks!

PS H:\> $env:processor_architecture
x86
PS H:\> $psversiontable

Name                           Value                                                                    
----                           -----                                                                    
PSVersion                      3.0                                                                      
WSManStackVersion              3.0                                                                      
SerializationVersion           1.1.0.1                                                                  
CLRVersion                     4.0.30319.34209                                                          
BuildVersion                   6.2.9200.16398                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0}                                                          
PSRemotingProtocolVersion      2.2   
3
  • 2
    You need to run the 32 bit powershell console. C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe You can't load a 32 bit dll in a 64 bit process or vice versa unless the dll was compiled for Any Cpu, in which case .Net JIT's it to the appropriate platform. Which is not the case with a com interop dll. Commented Jul 21, 2015 at 17:39
  • I am running 32bit powershell session Commented Jul 21, 2015 at 17:44
  • 1
    Note that $env:Processor_Architecture tells you the processor architecture not the process bitness. Of course, since it is x86, there is no possibility of running a 64-bit process by mistake. Use [Environment]::Is64BitProcess to tell if the current process is 64-bit. Commented Jul 21, 2015 at 17:57

2 Answers 2

4

The major clue here is the BadImageFormatException. This means you are either trying to load a 32-bit dll into a 64-bit PowerShell process or a 64-bit dll into a 32-bit PowerShell process.

Is the vendor providing the interop DLL you are loading or are you generating it with a tool like tlbimp? Usually these DLLs are architecture neutral (Any CPU) unless they need to pinvoke to a dll or load a COM dll in process.

Sign up to request clarification or add additional context in comments.

3 Comments

yes, but I check my $env:PROCESSOR_ARCHITECTURE is x86, which matches my C# project build platform target. I have tried loading this in x64 poweshell version just for the hell of it with same result. If the process bit-ness (?) matches the one of the DLL, what else could it be? The vendor is providing the DLL.
BadImageFormatException can occur for multiple reasons and the error message here indicates a different cause. The OP has tried to load an unmanaged dll as a .NET dll. The same error occurs (with the same message) if you try to load kernel32.dll in a 32 or 64-bit process.
The question states that it is a C# project. However, if that project uses another library that pinvokes a native DLL or the project directly pinvokes a native DLL, then this exception will occur. If there is no need for pinvoking native DLLs then the project should target Any CPU with "prefer 32-bit" unchecked for maximum flexibility.
1

In my case I had a content replacement script that was corrupting the dll. However, this was complicated by the fact that Add-Type seems to cache assemblies, even ones that failed to load. I replaced the dll with a good one and it continued to fail until I tried to load it from a different location (I imagine closing the powershell ISE would have also fixed it).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.