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?