COM Visible Class GUID Assignment

U

urkec

I am testing COM interop (.Net server - COM client). This is a very simple
class I made COM visible:


using System;
using System.Runtime.InteropServices;

namespace COMTest
{
//[GuidAttribute("63745640-F8C1-405c-9206-BBAD3297D05B")]
interface ICOMTest
{
string MachineName
{
get;
}
}

//[GuidAttribute("CCFC70BE-D23E-4988-8F61-E59AF55971E4")]
public class COMTest: ICOMTest
{
public COMTest()
{
}

public string MachineName
{
get { return System.Environment.MachineName; }
}
}
}


I got it to work both with and without explicitly assigned GUIDs. When I
left the GuidAttribute part out, my class still got automatically assigned a
GUID. My question is, when do I want to explicitly assign a GUID to a COM
visible class, and when do I leave it to regasm.exe?

--
urkec

My blog:
http://theadminblog.blogspot.com/

My CodeProject articles:
http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=4210975
 
P

Pavel Minaev

I am testing COM interop (.Net server - COM client). This is a very simple
class I made COM visible: ....
I got it to work both with and without explicitly assigned GUIDs. When I
left the GuidAttribute part out, my class still got automatically assigned a
GUID. My question is, when do I want to explicitly assign a GUID to a COM
visible class, and when do I leave it to regasm.exe?

The algorithm used to auto-assign GUIDs is as follows:

"You can assign a specific universal unique identifier (UUID) to the
class by applying the GuidAttribute immediately above the managed
class definition. During the conversion process, the export process
transfers the value provided to the GuidAttribute to the UUID in the
type library. Otherwise, the export process obtains UUIDs from a hash
that includes the complete name of the class, including the namespace.
Using the full name ensures that a class with a given name in a given
namespace always generates the same UUID, and that two classes with
different names never generate the same UUID." (MSDN Library)

As far as COM code is concerned, all that matters is that your classes
and interfaces have a unique GUID, and that it doesn't change in the
future. It's easier to guarantee the latter with explicit GUID
assignments, as otherwise refactoring your code (e.g. changing
namespace names) can change it without you noticing... but it's still
your call. Both methods work.
 
Top