应该同时使用CoRegisterClassObject和RegisterActiveObject吗?

时间:2017-01-19 13:49:19

标签: c# vb.net com

我确定标题令人困惑,但我无法确定问题。

我关注this MS example to create an Out-of-Process Server我没有遇到任何问题。我可以编译,从VBA注册调用CreateObject,当VBA关闭时,我的COM服务器也是如此。

问题1:我不明白需要调用CoRegisterClassObject并在EXE启动时传递对Implements IClassFactory的对象的引用。

我读到这需要"注册我的对象"但是当我从VBA CreateObject中调用时,只有在我调用它之后才启动EXE并使用CoRegisterClassObject。

修改了代码

Dim hResult As Integer = COMNative.CoRegisterClassObject( _
    clsidSimpleObj, New VBSimpleObjectClassFactory, CLSCTX.LOCAL_SERVER, _
    REGCLS.SUSPENDED Or REGCLS.MULTIPLEUSE, Me._cookieSimpleObj)

**问题2:**如何在EXE服务器中获取我的类对象的实例,以便我可以调用RegisterActiveObject?如果我的术语不对,请原谅我。

这是实现的CreateInstance方法。你看到他们目前在那里使用" New VBSimpleObject"。

Public Function CreateInstance(ByVal pUnkOuter As IntPtr, ByRef riid As Guid, _
                               <Out()> ByRef ppvObject As IntPtr) As Integer _
                               Implements IClassFactory.CreateInstance

    ppvObject = IntPtr.Zero

    If (pUnkOuter <> IntPtr.Zero) Then
        ' The pUnkOuter parameter was non-NULL and the object does 
        ' not support aggregation.
        Marshal.ThrowExceptionForHR(COMNative.CLASS_E_NOAGGREGATION)
    End If

    If ((riid = New Guid(VBSimpleObject.ClassId)) OrElse _
        (riid = New Guid(COMNative.IID_IDispatch)) OrElse _
        (riid = New Guid(COMNative.IID_IUnknown))) Then
        ' Create the instance of the .NET object
        ppvObject = Marshal.GetComInterfaceForObject( _
         New VBSimpleObject, GetType(VBSimpleObject).GetInterface("_VBSimpleObject"))
    Else
        ' The object that ppvObject points to does not support the 
        ' interface identified by riid.
        Marshal.ThrowExceptionForHR(COMNative.E_NOINTERFACE)
    End If

    Return 0  ' S_OK
End Function

要调用RegisterActiveObject,我想我需要对VBSimpleObject的引用

 <DllImport("oleaut32.dll")> _
Public Shared Function RegisterActiveObject(<MarshalAs(UnmanagedType.IUnknown)> ByVal pUnk As Object, _
                                            ByRef rclsid As Guid, ByVal dwFlags As IntPtr, _
                                            <Out()> ByRef lpdwRegister As UInt32) As Integer
End Function

那么如何将VBSimpleObject的引用传递给RegisterActiveObject。我试着让VBSimpleObject成为EXE的Public变量,然后只传递CreateInstance这个变量,但我不认为这是对的。

对不起,我有点失落。如果我能找到一个C#示例,它显示了如何同时执行CoRegisterClassObject和RegisterActiveObject,我可以解决它(因此我将添加一个C#标记来提醒C#人员)。我找到了C++ example,但我很难理解,似乎他们要么调用CoRegisterClassObject,要么调用RegisterActiveObject。这是对的吗?

0 个答案:

没有答案