检索 COM 类工厂中 CLSID 为 {28E68F9A-8D75-11D1-8DC3-3C302A000000} 的组件失败,原因是出现以下错误: 80040154 没有注册类

根据自己是32位还是64位来选择Regsvr32,以下是用代码注册的DLL

// Assembly assembly = Assembly.LoadFile(dllPath);
// 获取CLSID
// Guid guid = new Guid(((GuidAttribute)Attribute.GetCustomAttribute(assembly, typeof(GuidAttribute))).Value);
using (Process process = new Process())
{
	string regPath = System.Environment.GetFolderPath(Environment.SpecialFolder.System);
	if (Environment.Is64BitOperatingSystem)
	{
		regPath = System.Environment.GetFolderPath(Environment.SpecialFolder.SystemX86);
	}
	regPath = Path.Combine(regPath, "regsvr32");

	string reg32DllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "OPCDAAuto.dll"); // 要注册的dll路径
	ProcessStartInfo startInfo = new ProcessStartInfo(reg32DllPath)
	{
		WindowStyle = ProcessWindowStyle.Hidden,
		FileName = regPath,
		Arguments = $" {reg32DllPath} /s ",
		Verb = "runas"
	};
	process.StartInfo = startInfo;
	process.Start();
	process.WaitForExit();
}
Console.WriteLine(reg32DllPath + "注册成功");

发表评论

邮箱地址不会被公开。 必填项已用*标注