如何从PowerShell 1.0中调用的DLL方法

编程入门 行业动态 更新时间:2024-10-23 13:26:21
本文介绍了如何从PowerShell 1.0中调用的DLL方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

其实,我使用的是PowerShell版本1.0的脚本调用从DLL文件的方法,并使用了以下code加载DLL文件到PowerShell中。 [System.Reflection.Assembly] ::的LoadFile(DLL路径) 成功loded

GAC版本位置 --- ------- -------- DLL虚假V2.0.50727位置

本类包含一个公共的默认构造函数。我试着用下面的code创建一个类的对象。

$ OBJ =新的对象namespce.classname

和它引发以下错误。

  

新对象:异常调用.ctor和0的说法(S)的类型初始namespce.classname'引发了异常。   在行:1字符:18   + $ OBJ =新的对象<<<< namespce.classname      + CategoryInfo:InvalidOperation:(:) [新-对象],MethodInvocationException      + FullyQualifiedErrorId:ConstructorInvokedThrowException,Microsoft.P​​owerShell.Commands.NewObjectCommand`

当我试图调用类的方法,无需创建对象时,它抛出下面的错误,即使类包含的方法。

PS C:\ WINDOWS \ SYSTEM32> [namespace.classname] ::方法() 方法调用失败,因为[namespace.classname]不包含名为'方法'方法。 在行:1字符:39 + namespace.classname] ::方法<<<< ()     + CategoryInfo:InvalidOperation:(方法:字符串)[],RuntimeException的     + FullyQualifiedErrorId:MethodNotFound

解决方案

最有可能的方法是一个实例方法,这意味着你将需要有一个类的实例。你可以得到通过在类如公共默认构造函数:

$ OBJ =新的对象namespace.classname $ obj.Method()

也许唯一的公共consctructor的要求参数,如:

$ OBJ =新的对象namespace.classname -argstring_arg',7 $ obj.Method()

也许没有公共构造,但有一个静态的创建或分析方法,它返回一个实例,例如:

$的obj = [namespace.classname] ::创建() $ obj.Method()

Actually, I am using a PowerShell version 1.0 script to call a method from a DLL file and used the following code to load the DLL file into PowerShell. [System.Reflection.Assembly]::LoadFile("path of dll") is loded successfully

GAC Version Location --- ------- -------- False v2.0.50727 location of dll

The class contains a public default constructor. I tried to create an object of the class using the below code.

$obj = new-object namespce.classname

And it throws the following error.

New-Object : Exception calling ".ctor" with "0" argument(s): "The type initializer for 'namespce.classname' threw an exception." At line:1 char:18 + $obj = new-object <<<< namespce.classname + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand`

When I tried to call the method of the class without creating the object, it throws the below error even though the class contains the method.

PS C:\Windows\system32> [namespace.classname]::method() Method invocation failed because [namespace.classname] doesn't contain a method named 'method'. At line:1 char:39 + [namespace.classname]::method <<<< () + CategoryInfo : InvalidOperation: (method:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

解决方案

Most likely the method is an instance method which means you will need to have an instance of the class. You can get that via a public default constructor on the class e.g.:

$obj = new-object namespace.classname $obj.Method()

Perhaps the only public consctructor's require parameters e.g.:

$obj = new-object namespace.classname -arg 'string_arg',7 $obj.Method()

Or maybe there are no public constructors but there is a static Create or Parse method that returns an instance e.g.:

$obj = [namespace.classname]::Create() $obj.Method()

更多推荐

如何从PowerShell 1.0中调用的DLL方法

本文发布于:2023-06-11 23:31:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/645290.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   PowerShell   DLL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!