简体   繁体   English

如何从C#注册/取消注册EXE作为COM对象?

[英]How do you register/unregister an EXE as a COM object from C#?

EDIT 5/11/2013 : This question originally asked how to programmatically register a COM object using code from .NET without having to use REGASM. 编辑5/11/2013 :这个问题最初询问如何使用.NET中的代码以编程方式注册COM对象,而不必使用REGASM。 Yahia's answer covers both how to register a DLL programmatically and an EXE. Yahia的答案涵盖了如何以编程方式注册DLL和EXE。

ORIGINAL QUESTION : At the moment, we have a batch script that we call from our UI to register/unregister our software in COM when setting the default version (using regasm and adding registry entries). 原始问题 :目前,我们有一个批处理脚本,在设置默认版本时(使用regasm和添加注册表项),我们从UI调用以在COM中注册/取消注册我们的软件。 We no longer wish to use a batch script as we are running into a problem where if the script fails, the program is not aware of the failure. 我们不再希望使用批处理脚本,因为我们遇到了一个问题,如果脚本失败,程序就不会意识到失败。 There are several ways I can address the issue, but I don't like the notion of calling batch scripts from code if I don't have to, so I'd like to switch to registering our software with COM using managed code. 有几种方法可以解决这个问题,但我不喜欢从代码调用批处理脚本的概念,如果我不需要,所以我想切换到使用托管代码向COM注册我们的软件。

Assuming it's possible, how do you register/unregister COM objects from C#? 假设它是可能的,你如何从C#注册/注销COM对象? Googling only turns up ways to do this from installer packages, or tells me to use regasm, but I can't find any information on how to do this programmatically or if it's even possible. 谷歌搜索只能找到从安装程序包中执行此操作的方法,或者告诉我使用regasm,但我无法找到有关如何以编程方式执行此操作的任何信息,或者甚至是否可能。

Thanks in advance for any assistance with this. 在此先感谢任何帮助。

EDIT 5/10/2013 : It looks like registering an EXE as a COM object is not the same as registering a DLL. 编辑5/10/2013 :将EXE注册为COM对象看起来与注册DLL不同。 Is there a programmatic way in .NET (PInvoke is fine) to register an EXE as a COM object (in the 32 bit registry)? .NET中是否有一种编程方式(PInvoke很好)将EXE注册为COM对象(在32位注册表中)? This is currently how we're doing it in the script, the app and REGCMD vars are just shorthand: 这是我们目前在脚本中的做法,app和REGCMD变量只是简写:

    set app=%PathToTheExecutable%
    set REGCMD=%PathTo32BitRegExe%
    "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\regasm" /tlb /codebase %app%
    %REGCMD% ADD HKCR\CLSID\{app-guid}\LocalServer32 /ve /t REG_SZ /d %app% /f
    %REGCMD% DELETE HKCR\CLSID\{app-guid}\InprocServer32 /f

Basically we run regasm on the executable, then add our exe as a LocalServer32 and remove the entry which associates us as an InprocServer. 基本上我们对可执行文件运行regasm,然后将我们的exe添加为LocalServer32并删除将我们关联为InprocServer的条目。 Is it possible to do this programmatically without having to call regasm from code? 是否有可能以编程方式执行此操作而无需从代码中调用regasm?

You can do that in C# - but only by using some pinvoke, specifically LoadLibrary / GetProcAddress / FreeLibrary. 你可以在C#中做到这一点 - 但只能使用一些pinvoke,特别是LoadLibrary / GetProcAddress / FreeLibrary。

Basically you need to load the DLL, call the register/unregister functions inside that DLL and then unload it. 基本上你需要加载DLL,调用该DLL中的注册/取消注册函数然后卸载它。

For a complete walkthrough including working C# source code see here . 有关完整的演练,包括工作C#源代码,请参见此处 Reference see MSDN as per comment. 参考资料根据评论参见MSDN

UPDATE after the OP updated the question: OP更新后更新问题:

The above method only works for DLLs. 上述方法仅适用于DLL。

With Assemblies (esp. EXE COM-server implemented in .NET) the easiest and most robust option is actually to use regasm . 使用Assemblies(特别是在.NET中实现的EXE COM服务器),最简单和最强大的选项实际上是使用regasm

IF you really need to do this programmatically you should take a look at these MSDN-links: 如果您确实需要以编程方式执行此操作,则应该查看这些MSDN链接:

Basically you need to load your EXE via Assembly.Load and then call RegisterTypeForComClients on the relevant Type s. 基本上你需要通过Assembly.Load加载你的EXE,然后在相关的Type上调用RegisterTypeForComClients

Another option is to do this dynamically (at runtime) - perhaps this is of some use to you... 另一种选择是动态地(在运行时)这样做 - 也许对你有用...

var proc = System.Diagnostics.Process.Start("regsvr32", "msxml6.dll /s");
proc.WaitForExit();
Console.WriteLine(proc.ExitCode);

proc = System.Diagnostics.Process.Start("regasm", name+" /silent");
proc.WaitForExit();
Console.WriteLine(proc.ExitCode);

Now you have the exit code, the result of the registering. 现在你有了退出代码,即注册的结果。 Testing on msxml6 had an exit code of '0' for success, and when I poorly named it exit code of '3'. msxml6上的测试成功退出代码为'0',当我命名不佳时退出代码为'3'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM