当接收对象是 C# COM 对象并且函数以接口作为参数时,VBScript 抛出异常
这是我的 VBScript 代码,
Set object= CreateObject("Demo. Object")
Set service= CreateObject("Demo.ManageServer ")
service.SetObject object
我收到以下消息:
错误:无效的过程调用或 参数:'Service.SetObject'代码: 800A005
Demo.Service 是 C# COM 对象
[ProgId("Demo.ManageServer")]
[Guid("A4F0A0F0-9C0C-4C71-AD58-A36F750D92FE")]
[ComVisible(true)]
public class MyServer :IMyServer
{
public void SetObject (IMyObject obj) { … }
}
接口:
[TypeLibType(TypeLibTypeFlags.FDual | TypeLibTypeFlags.FNonExtensible | TypeLibTypeFlags.FDispatchable)]
[Guid("B3ACBCAD-5ED3-4113-B9D2-FC67FA49012B")]
public interface IMyServer
{
[DispId(1)]
void SetObject (IMyObject obj);
}
注意:
1)当我将“Nothing”作为参数发送给函数时,它可以正常工作
2)当我更改函数的接口以接收对象时,它也可以再次工作。
你知道为什么在函数签名中使用显式类型,VBScript 会抛出异常吗?
我已经尝试过了,但不起作用。 此外, 如果接收者是 C++ COM 对象,我可以毫无问题地发送托管对象和非托管对象 如果接收者是 C# COM 对象,则仅当我使用括号
manageServer.PrintType (nativeObject) 时,我才能传递管理对象 如果没有括号,我会收到相同的异常 但如果我尝试将非托管对象发送到非托管代码中,则没有任何帮助
Here is my VBScript code
Set object= CreateObject("Demo. Object")
Set service= CreateObject("Demo.ManageServer ")
service.SetObject object
I get the following message:
Error: Invalid procedure call or
argument: ‘Service.SetObject’ Code:
800A005
Demo.Service is C# COM object
[ProgId("Demo.ManageServer")]
[Guid("A4F0A0F0-9C0C-4C71-AD58-A36F750D92FE")]
[ComVisible(true)]
public class MyServer :IMyServer
{
public void SetObject (IMyObject obj) { … }
}
The interface:
[TypeLibType(TypeLibTypeFlags.FDual | TypeLibTypeFlags.FNonExtensible | TypeLibTypeFlags.FDispatchable)]
[Guid("B3ACBCAD-5ED3-4113-B9D2-FC67FA49012B")]
public interface IMyServer
{
[DispId(1)]
void SetObject (IMyObject obj);
}
NOTE:
1) When I sent ‘Nothing’ as parameter to the function it works ok
2) When I change the interface of the function to receive object, it also work again.
Do you know why using explicit Type in the function signature, VBScript throws exception?
I already tried it and it does not work.
In addition,
If the receiver is C++ COM object I have no problem to send both Manage and Unmanaged Objects
If the receiver is C# COM object I can pass a manage object only if I use parenthesis
manageServer.PrintType (nativeObject)
Without the parenthesis I receive the same exception
But in case I tries to send into a unmanaged code an unmanaged object nothing help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用对象,CLR 将接受 VBS 传递的 IDispatch 引用。
然后,当您将其转换为接口类型时,CLR 将使用通常的 QueryInterface 方法。
当您将参数标记为这样时,您可能会很幸运:
这应该告诉 CLR 它将收到与 IUnknown 兼容的内容,并且应该尝试使用 QueryInterface 将其转换为您的接口类型。
If you use Object, the CLR will accept the IDispatch reference which VBS is passing.
When you then cast it to your interface type, the CLR will use the usual QueryInterface approach.
You may have luck when you mark the parameter as such:
This should tell the CLR that it will receive something compatible to IUnknown and that it should try to use QueryInterface to cast it to your interface type.