我认为问题和答案都是错误的。这里涉及三个名称:
- ClassId或GUID,具有熟悉的格式,例如, “{F4754C9B-64F5-4B40-8AF4-679732AC0607}”
- ProgId或程序名称,应格式化为Vendor.Component.Version(请参阅http://msdn.microsoft.com/en-us/library/aa911706.aspx) - 例如“Word.Document.12”
- 人类可读的名称 - 这是通常显示在COM组件列表中的名称(如VS中的添加引用/ COM对话框或OLE应用程序中的插入 - >对象菜单),例如“Microsoft Word文档”
在注册表中的相应条目为:
CLASSES_ROOT\CLSID\ClassId @= "Human readable name"
CLASSES_ROOT\CLSID\ClassId\ProgId @= "ProgId"
CLASSES_ROOT\ProgId @= "Human readable name"
CLASSES_ROOT\ProgId\CLSID @= "ClassId"
看看你自己的注册表中的一些例子。
如果您反思regasm.exe,您会发现它始终使用Type.FullName作为人类可读的名称。这是一个痛苦的屁股,因为这意味着你必须使用com注册挂钩来设置HRN。例如,他们很容易查找DisplayName属性,然后使用它。无论如何,这是一个设置HRN条目的帮助函数。从您的注册挂钩调用它。
有关COM注册钩更多细节参见http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.comregisterfunctionattribute.aspx
public static void SetHumanReadableName(Type t, string humanReadableName)
{
var classKeyName = string.Format(@"CLSID\{{{0}}}", t.GUID);
var classKey = Registry.ClassesRoot.OpenSubKey(classKeyName, true);
if (classKey == null)
return;
classKey.SetValue(string.Empty, humanReadableName);
// if it has a progid set the HRN on the progid entry too
foreach (ProgIdAttribute progId in t.GetCustomAttributes(typeof(ProgIdAttribute), false))
{
var progIdKey = Registry.ClassesRoot.OpenSubKey(progId.Value, true);
if (progIdKey != null)
progIdKey.SetValue(string.Empty, humanReadableName);
}
}