IRunningObjectTable.Register always sets pdwRegist

2019-02-25 12:35发布

问题:

I'm using IRunningObjectTable.Register and IRunningObjectTable.Revoke as shown in this tutorial. My VBScript client initially calls methods no problem, but when the C# COM server disposes, I always receive a "Value does not fall within the expected range" exception. This is due to the commented line below:

private const int ACTIVEOBJECT_STRONG = 0x0;

[DllImport("ole32.dll")]
private static extern int CreateBindCtx(int reserved,
    out IBindCtx bindCtx);

[DllImport("oleaut32.dll")]
private static extern int RegisterActiveObject
    ([MarshalAs(UnmanagedType.IUnknown)] object punk,
    ref Guid rclsid, 
    uint dwFlags, 
    out int pdwRegister);

// register instance so it appears in ROT
private static int Register<T>(T classToRegister) 
{  
    int pdwRegister;
    Guid guid = Marshal.GenerateGuidForType(typeof(T));

    RegisterActiveObject(classToRegister, 
        ref guid, 
        ACTIVEOBJECT_STRONG, 
        out pdwRegister);

    return pdwRegister;
}

// do stuff in VBScript before disposal calls Revoke with the stored 
// pdwRegister value from the method above

// revoke instance so it's removed from ROT
private static void Revoke(int pdwRegister)
{
    IBindCtx bc;
    CreateBindCtx(0, out bc);

    IRunningObjectTable rot;
    bc.GetRunningObjectTable(out rot);
    // EXCEPTION: pdwRegister is *always* 65536, an invalid value!
    rot.Revoke(pdwRegister);      
}

If I terminate the program and ignore the exception, the instance usually removes itself from the ROT. However, after some time, I've noticed multiple instances of my app's GUID in the ROT and my VBScript client starts failing on GetObject(, "my.id"). Any thoughts?

回答1:

I suspect that you are not maintaining the lifetime of the object correctly. I've done the same thing you have, but used two separate types, placing each of them in the Running Object Table.

Like you, the first object I placed in the table, I received a handle value of 65536. However, for the second item, I received a different handle.

However, when calling RevokeActiveObject, both calls returned an HRESULT of S_OK.

Something tells me that your object is being disposed of before the calls to revoke them are made.

Either that, or something is not incrementing/decrementing the reference count of the item on the ROT.



回答2:

The first one registered is always 65536, and that is correct.

Use

[DllImport("oleaut32.dll", PreserveSig = false)]
        public static extern void RevokeActiveObject(
            uint handle,
            IntPtr reserved);

And Not:

IBindCtx bc; 
    CreateBindCtx(0, out bc); 


IRunningObjectTable rot; 
bc.GetRunningObjectTable(out rot); 
// EXCEPTION: pdwRegister is *always* 65536, an invalid value! 
rot.Revoke(pdwRegister);       

And RevokeActiveObject needs to be called from the same thread and proc as it was registered in.