0

我正在尝试使用一种方法创建一个 COM 类,该方法将代表 VBScript 将对象转换为特定接口。

这是我正在使用的方法签名:
public object GetInterface(object unknown, string iid)

我认为这是可能的,因为如果该方法将返回类型显式声明为:
public IRequestedInterface GetInterface(object unknown, string iid)

然后 VBScript 获取对所需接口的引用。

所以我试着只投射到界面
return (IRequestedInterface)unknown;

不幸的是,VBScript 引用了默认接口而不是请求的接口。

我尝试通过使用ICustomMarshaler.
我认为这会起作用,因为该方法MarshalManagedToNative返回一个IntPtr.

正因为如此,我认为如果我只是返回IntPtr到界面
return Marshal.GetComInterfaceForObject(unknown, typeof(IRequestedInterface));
它会工作。但是,显然,它没有达到预期的效果:(

那么有人知道它是否可行以及您将如何做到这一点?

编辑:

我认为添加一个具体示例(尽管它是人为的)来解释为什么我不接受 VBScript 将始终获得默认界面会很有帮助。我仍然坚持我的希望。

您将在下面找到 3 个文件的内容,“TestLib.cs”、“Build.cmd”和“Test.vbs”。这些希望能证明为什么我仍然认为它“应该”是可能的。

注意:我已经在 Windows XP SP3 (x86) 上对此进行了测试。

测试库.cs

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

[assembly: ComVisible(false)]
[assembly: Guid("64e20009-c664-4883-a6e5-1e36a31a0fd8")]
[assembly: AssemblyVersion("2012.06.*")]

[ComVisible(true)]
[Guid("EB77C7B1-D1B9-4BB3-9D63-FBFBD56C9ABA")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IPerformQi
{
    [DispId(1000)]
    object GetInterface(object unknown, string iid);

    [DispId(2000)]
    IRequested GetIRequested(object unknown);
}

[ComVisible(true)]
[Guid("7742BC0A-8719-483E-B1DF-AE9CD9A958DC")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IDefault
{
    [DispId(1000)]
    void SayHello(string name);
}

[ComVisible(true)]
[Guid("FFF34296-2A06-47D4-B09C-B93B63D5CC53")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IRequested
{
    [DispId(1000)]
    void SayGoodbye(string name);
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IPerformQi))]
[Guid("222BB88D-B9FA-4F23-8DB3-BA998F4E668B")]
[ProgId("TestLib.PerformQi")]
public class PerformQi : IPerformQi
{
    object IPerformQi.GetInterface(object unknown, string iid)
    {
        if(iid == "FFF34296-2A06-47D4-B09C-B93B63D5CC53")
            return (IRequested)unknown;

        throw new Exception("Unable to find inteface");
    }

    IRequested IPerformQi.GetIRequested(object unknown)
    {
        return (IRequested)unknown;
    }
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IDefault))]
[Guid("174ABED6-3325-4878-89E3-BF8BD1107488")]
[ProgId("TestLib.Test")]
public class Test : IDefault, IRequested
{
    void IDefault.SayHello(string name)
    {
        MessageBox.Show(string.Format("Hello '{0}'", name));
    }

    void IRequested.SayGoodbye(string name)
    {
        MessageBox.Show(string.Format("Goodbye '{0}'", name));
    }
}

构建.cmd

"%windir%\Microsoft.Net\Framework\v4.0.30319\csc.exe" /out:TestLib.dll /target:library /r:System.Windows.Forms.dll TestLib.cs
"%windir%\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" TestLib.dll /codebase /tlb:TestLib.tlb
PAUSE

测试.vbs

Dim oPerformQi 'As TestLib.PerformQi
Dim oTest 'As TestLib.Test
Dim oTest2 'As IRequested
Dim oTest3 'As IRequested

Set oPerformQi = CreateObject("TestLib.PerformQi")
Set oTest = CreateObject("TestLib.Test")
Call oTest.SayHello("Robert")


Set oTest2 = oPerformQi.GetIRequested(oTest)
'Note: This works
Call oTest2.SayGoodbye("Robert")


Set oTest3 = oPerformQi.GetInterface(oTest, "FFF34296-2A06-47D4-B09C-B93B63D5CC53")
'Note: This does not work
Call oTest3.SayGoodbye("Robert")

使用调用oPerformQi.GetIRequested(oTest)使调用oTest3.SayGoodbye("Robert")生效。这使我认为您不仅限于 VBS 中的默认界面。

也许 .Net 由于对返回值的隐式转换而无法返回指定的接口?理想情况下,我会为此使用泛型,但众所周知,COM 不支持泛型。

在此限制下,您还有其他方法可以实现这一目标吗?

编辑2:

我发现我可以使用 VB6 实现这一点,下面是该类的代码。

Option Explicit

Public Function GetInterface(ByVal oUnknown As Object, ByVal IID As String) As Variant

    Dim oIRequested As IRequested

    If IID = "FFF34296-2A06-47D4-B09C-B93B63D5CC53" Then
        Set oIRequested = oUnknown
        Set GetInterface = oIRequested
    Else
        Err.Raise 1, , "Unable to find inteface"
    End If

End Function

如果有人能对这个主题有所了解,我仍然想找到一个 C# 版本,我将不胜感激。

4

1 回答 1

1

为了IDispatch在单个对象上实现多个派生接口,以便从脚本环境访问,您应该实现IDispatchEx并在发生脚本调用时调用其方法。

您面临的问题是由于脚本查询您的IDispatch第一个,并且您的两个IDispatch派生接口都返回相同的“main” IDispatch,因此没有机会访问其他接口的方法。

当 VBS 主机要调用对象上的方法时,它首先会查询IDispatchEx. 如果找到,调用将通过传递IDispatchEx::InvokeEx,您的 COM 类可以在内部将调用路由到正确的IDispatch实现,无论是私有的还是转发到外部/内部对象。

如果IDispatchEx没有找到,它会寻找IDispatch并且你有麻烦,因为它只看到你的“主”界面。也就是说,您的解决方法是实施IDispatchEx. 您可以采用任何一种方式:直接在您的 COM 类上实现,或者创建一个代理类来接受脚本调用IDispatchEx::InvokeEx并转发以纠正IDispatch您的代码。

示例:AB类都实现了IXIY接口,B另外还实现了IDispatchEx. 接口方法是IX::X1, IY::Y1.

On Error Resume Next

Set A = CreateObject("Test.A")
WScript.Echo A.X1 ' Success, via IX::Invoke
WScript.Echo A.Y1 ' Failure, A's IDispatch is IX's parent and does not have Y1 method

Set B = CreateObject("Test.B")
WScript.Echo B.X1 ' Success, via IDispatchEx::InvokeEx
WScript.Echo B.Y1 ' Success, via IDispatchEx::InvokeEx
于 2012-06-28T17:16:30.233 回答