Powershell v2 ::加载COM Interop DLL

时间:2012-04-20 15:49:32

标签: powershell powershell-v2.0

我的系统上有这个DataLink DLL - Interop.MSDASC.dll 我试图像这样从Powershell加载它 -

[Reflection.Assembly]::LoadFile("C:\Interop.MSDASC.dll") | out-null

但是,我收到以下错误 -

Exception calling "LoadFile" with "1" argument(s): "Could not load file or assembly 'Interop.MSDASC.dll' or one of its dependencies.  is not a 
valid Win32 application. (Exception from HRESULT: 0x800700C1)"
At line:1 char:32
+ [Reflection.Assembly]::LoadFile <<<< ("C:\Interop.MSDASC.dll") | out-null
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

如何正确加载?

3 个答案:

答案 0 :(得分:9)

这是一个32位COM对象,因此您必须从32位PowerShell实例加载它。要在64位版本的Windows上执行此操作,您可以在此文件夹中执行powershell.exe或powershell_ISE.exe: %SYSTEMROOT%\ SYSWOW64 \ windowspowershell \ V1.0

而且,这是完整的代码:

[Reflection.Assembly]::LoadFile("C:\Interop.MSDASC.dll") 
$dataLinkInstance = new-object MSDASC.DataLinksClass
$dataLinkInstance.WriteStringToStorage("C:\\FrmPowershell.udl", "Provider=SQLOLEDB.1;", 2)

答案 1 :(得分:3)

我刚刚从http://datadictionary.codeplex.com/下载了它,并以您使用的相同方式加载程序集,没有问题:

 [System.Reflection.Assembly]::LoadFile( "c:\Program Files\DataDictionaryCreator\Interop.MSDASC.dll")

GAC    Version        Location
---    -------        --------
False  v2.0.50727     c:\Program Files\DataDictionaryCreator\Interop.MSDASC.dll

你是否可能使用x64操作系统? 如果是,请阅读http://datadictionary.codeplex.com/workitem/28807

答案 2 :(得分:-1)

$comInterOp = "C\Temp\Interop.YourAssembly.dll"
[System.Reflection.Assembly]::LoadFile($comInterOp)
$yourClassObj = new-object YourAssembly.YourClassNameClass
$yourResult = $yourClassObj.YourMethod()