2015-05-28 90 views
4

我有一个在名为BS.Logon一个COM对象命名GoLogon方法,该方法需要5个参数:检索会话ID

  • 用户名
  • 密码
  • 的applicationID
  • XMLRoot
  • 的IPAddress

此方法使用用户名,密码和其他详细信息登录到Web服务器,并返回会话ID。

我写了Powershell脚本来调用方法GoLogon如下。

$obj=New-Object -ComObject BS.Logon 
$sessionid=$obj.GoLogon([ref]$bUsername,$bPassword,$appid,$xmlroot,[ref]$ipad) 
Write-Host $sessionid 

脚本执行时,该方法使登录成功。我可以在数据库中看到会话ID的详细信息,但我无法通过脚本获取会话ID的详细信息。变量$sessionid返回null。另外,脚本抛出异常,如

Exception calling "GoLogon" with "5" argument(s): "Invalid callee. (Exception from HRESULT: 0x80020010 (DISP_E_BADCALLEE))" 
At E:\Logon.ps1:18 char:1 
+ $obj.Login([ref]$bUsername,$bPassword,$appid,$xmlroot,[ref]$ipad) 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
+ FullyQualifiedErrorId : ComMethodTargetInvocation` 

堆栈跟踪是:

Exception    : System.Management.Automation.MethodInvocationException: Exception calling "GoLogon" with "5" argument(s): "Invalid callee. (Exception from HRESULT: 0x80020010 (DISP_E_BADCALLEE))" ---> System.Reflection.TargetInvocationException: 
Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: Invalid callee. (Exception from HRESULT: 0x80020010 (DISP_E_BADCALLEE)) 
--- End of inner exception stack trace --- 
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) 
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) 
at System.Management.Automation.ComMethod.InvokeMethod(PSMethod method, Object[] arguments) 
--- End of inner exception stack trace --- 
at System.Management.Automation.ComMethod.InvokeMethod(PSMethod method, Object[] arguments) 
at System.Management.Automation.Adapter.BaseMethodInvoke(PSMethod method, PSMethodInvocationConstraints invocationConstraints, Object[] arguments) 
at System.Dynamic.UpdateDelegates.UpdateAndExecute6[T0,T1,T2,T3,T4,T5,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) 
at System.Management.Automation.Interpreter.DynamicInstruction 7.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) 
TargetObject   : 
CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
FullyQualifiedErrorId : ComMethodTargetInvocation 
ErrorDetails   : 
InvocationInfo  : System.Management.Automation.InvocationInfo 
ScriptStackTrace  : at <ScriptBlock>, E:\Logon.ps1: line 18 
PipelineIterationInfo : {} 
PSMessageDetails  : 

请建议我如何从方法的会话ID值。

+0

您的问题很可能是您在参数中使用了[ref]。 *“[ref]被COM或RPC用来声明指针属性,.NET中的[ref]是 ,用于参数重定向。这些都不兼容。”* –

+0

@Jan Chrbolka其实这两个[ref]参数是方法的参数,指的是COM中的指针变量。 –

+0

这在PowerShell中对我来说一直是个问题。看看这里... [RE:“无效的被调用者”调用COM对象](http://www.archivum.info/microsoft.public.windows.powershell/2010-01/00361/RE--quot-Invalid -callee-quot - calling-a-com-object.html)这将描述如何使用Variant包装器。它可能适用于你的情况。 '$ variable = new-object object $ wrapper = New-Object Runtime.InteropServices.VariantWrapper($ variable) ##现在使用包装对象调用方法 $ object.method([ref] $ wrapper)' –

回答

2

PowerShell似乎遇到了定义为VARIANT的COM参数问题。

这就是这里的情况,正如GoLogon的C++签名所暗示的那样。

STDMETHODIMP CLOG::GOLogon(VARIANT *pvEmailId, BSTR bsPassword, BSTR bsIPAddress, BSTR bsSoloBuildVer, VARIANT *pvXML, BSTR *bsSessionId) 

在这篇文章中建议的答案"Invalid callee" calling a com object是使用VariantWrapper。

在你的情况下,这应该做的伎俩。

#define VariantWrapper for variables to be passesed as VARIANT 
$wrapbUservame = New-Object Runtime.InteropServices.VariantWrapper($bUsername) 
$wrappvXML = New-Object Runtime.InteropServices.VariantWrapper($pvXML) 

#pass a [ref]VariantWrapper to .COM method 
$sessionid=$obj.GoLogon([ref]$wrapbUservame,$bPassword,$appid,[ref]$wrappvXML,$ipad)