Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > dotnet interop > may 2004 >

dotnet interop : Register an Object in the ROT


Nicolas Wieseke via .NET 247
5/4/2004 4:07:03 AM
Hi

I want to register an instance of an object in the Running Object Tabel, using either UCOMIRunningObjectTable.Register(), or the oleaut32.dll-function RegisterActiveObject() like I did it in VB6. But both did not work.
Can somebody of you tell me what is wrong with my C#-code

//C#
public const long ACTIVEOBJECT_STRONG = 0;
public const long ACTIVEOBJECT_WEAK = 1;

[DllImport("ole32.dll", EntryPoint = "CLSIDFromString")]
public static extern int CLSIDFromString(byte[] lpszCLSID, out Guid pclsid);
[DllImport("oleaut32.dll", EntryPoint = "RegisterActiveObject")]
public static extern int RegisterActiveObject([MarshalAs(UnmanagedType.IUnknown)] object punk, Guid rclsid, long dwFlags, out long pdwRegister);
[DllImport("ole32.dll", EntryPoint = "GetRunningObjectTable")]
public static extern int GetRunningObjectTable(int reserved, out UCOMIRunningObjectTable ROT);
[DllImport("ole32.dll", EntryPoint = "CreateItemMoniker")]
public static extern int CreateItemMoniker(byte[] lpszDelim, byte[] lpszItem, out UCOMIMoniker ppmk);

public MyNamespace.MyClass MyObject = new MyNamespace.MyClass();

// OLE-Version
public void Register_OLE32()
(
UnicodeEncoding enc = new UnicodeEncoding();
int errorcode;
Guid guid = new Guid();
long register;

string itemname = "MyNamespace.MyClass";
byte[] item = enc.GetBytes(itemname);

errorcode = CLSIDFromString(item, out guid);
Marshal.ThrowExceptionForHR(errorcode);
errorcode = RegisterActiveObject(this.MyObject, guid, ACTIVEOBJECT_WEAK, out register);
Marshal.ThrowExceptionForHR(errorcode);
}

// UCOMI-Version
public void Register_UCOMI()
{
UnicodeEncoding enc = new UnicodeEncoding();
int errorcode;
UCOMIRunningObjectTable rot;
UCOMIMoniker moniker;
int register;

string delimname = "!";
byte[] del = enc.GetBytes(delimname);
string itemname = "MyNamespace.MyClass";
byte[] item = enc.GetBytes(itemname);

errorcode = GetRunningObjectTable(0, out rot);
Marshal.ThrowExceptionForHR(errorcode);
errorcode = CreateItemMoniker(del, item, out moniker);
Marshal.ThrowExceptionForHR(errorcode);
rot.Register(0, this.MyObject, moniker, out register);
}

OLE: RegisterActiveObject throws an NullReferenceException (but guid is correct)
UCOMI: There is an entry in the ROT but it has no ProgID and I can't get an reference to the MyObject (Maybe there are some Moniker-Function, where I can specify the guid of MyObject)

Thank you

--------------------------------
From: Nicolas Wieseke

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

Mattias Sjögren
5/4/2004 1:26:13 PM
Nicolas,

[quoted text, click to view]

should be

public static extern int RegisterActiveObject(
[MarshalAs(UnmanagedType.IUnknown)] object punk, ref Guid rclsid, uint
dwFlags, out uint pdwRegister);



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
AddThis Social Bookmark Button