我遇到了类似的问题,并找到了以下 Github 存储库,其中显示了 IDL 文件的正确格式。它还向您展示了需要添加哪些注册表项。您可以将其用作起点。
链接
您的 IDL 文件的基本结构应该如下所示:
[
uuid(your library GUID),
version(1.0),
helpstring("ComTestLibrary")
]
library ComTestLibrary
{
importlib("STDOLE2.TLB");
[
odl,
uuid(your interface GUID),
dual,
oleautomation,
nonextensible,
helpstring("ComTestLibrary"),
object,
]
interface IComTest : IDispatch
{
[
id(1),
helpstring("Test")
]
HRESULT Test(
[in] double firstValue,
[in] double secondValue,
[in] BSTR comment,
[out, retval] double* ReturnVal);
};
[
uuid(your class GUID),
helpstring("ComTest")
]
coclass ComTest
{
[default] interface IComTest;
};
}
对于注册表条目,您可以向COM类添加DllRegisterServer和DllUnregisterServer函数。 在我的情况下,它们看起来像这样:
[ComRegisterFunction]
public static void DllRegisterServer(Type t)
{
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(@"CLSID\{" + AssemblyInfo.ClassGuid + @"}"))
{
using (RegistryKey typeLib = key.CreateSubKey(@"TypeLib"))
{
typeLib.SetValue(string.Empty, "{" + AssemblyInfo.LibraryGuid + "}", RegistryValueKind.String);
}
}
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(@"Interface\{" + AssemblyInfo.InterfaceGuid + @"}"))
{
using (RegistryKey typeLib = key.CreateSubKey(@"ProxyStubClsid32"))
{
typeLib.SetValue(string.Empty, "{00020424-0000-0000-C000-000000000046}", RegistryValueKind.String);
}
using (RegistryKey typeLib = key.CreateSubKey(@"TypeLib"))
{
typeLib.SetValue(string.Empty, "{" + AssemblyInfo.LibraryGuid + "}", RegistryValueKind.String);
Version version = typeof(AssemblyInfo).Assembly.GetName().Version;
typeLib.SetValue("Version", string.Format("{0}.{1}", version.Major, version.Minor), RegistryValueKind.String);
}
}
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(@"TypeLib\{" + AssemblyInfo.LibraryGuid + @"}"))
{
Version version = typeof(AssemblyInfo).Assembly.GetName().Version;
using (RegistryKey keyVersion = key.CreateSubKey(string.Format("{0}.{1}", version.Major, version.Minor)))
{
keyVersion.SetValue(string.Empty, AssemblyInfo.Attribute<AssemblyDescriptionAttribute>().Description, RegistryValueKind.String);
using (RegistryKey keyWin32 = keyVersion.CreateSubKey(@"0\win32"))
{
keyWin32.SetValue(string.Empty, Path.ChangeExtension(Assembly.GetExecutingAssembly().Location, ".comhost.tlb"), RegistryValueKind.String);
}
keyVersion.SetValue(string.Empty, AssemblyInfo.Attribute<AssemblyDescriptionAttribute>().Description, RegistryValueKind.String);
using (RegistryKey keyWin64 = keyVersion.CreateSubKey(@"0\win64"))
{
keyWin64.SetValue(string.Empty, Path.ChangeExtension(Assembly.GetExecutingAssembly().Location, ".comhost.tlb"), RegistryValueKind.String);
}
using (RegistryKey keyFlags = keyVersion.CreateSubKey(@"FLAGS"))
{
keyFlags.SetValue(string.Empty, "0", RegistryValueKind.String);
}
}
}
}
[ComUnregisterFunction]
public static void DllUnregisterServer(Type t)
{
Registry.ClassesRoot.DeleteSubKeyTree(@"TypeLib\{" + AssemblyInfo.LibraryGuid + @"}", false);
Registry.ClassesRoot.DeleteSubKeyTree(@"Interface\{" + AssemblyInfo.InterfaceGuid + @"}", false);
Registry.ClassesRoot.DeleteSubKeyTree(@"CLSID\{" + AssemblyInfo.ClassGuid + @"}", false);
}