问题描述
首先,我是 COM 新手,目前很困惑.我在 MSDN 和一般网络上阅读了很多关于 COM 的文档,但其中很多似乎已经过时且过于复杂.
First off, I am new to COM, and currently quite confused. I've read a lot of documentation on COM on MSDN and the general web, but a lot of it seems outdated and overly complex.
这是我认为让它发挥作用所必需的.它还没有,所以我确定我遗漏了一些东西,但是通过提供我的食谱,我希望有人能发现问题:
Here's what I believe to be necessary to get it to work. It doesn't yet, so I am sure I am missing something, but by giving my recipe, I hope someone can spot the problem:
- 创建一个 C# 控制台应用项目.我把它命名为 CSharpApp
- 创建一个 C++ ATL 项目.我称之为 ComLib.Interop.
- 添加类(模板:ATL Simple Object),我称之为InteropDemo"
- 在类视图中,右键单击 IInteropDemo 并添加方法 HelloWorld.
- 在编译好的 COM dll 上调用 regsrv32.exe,然后在添加引用..."的 COM 选项卡中选择 DLL
感谢答案,我更新了问题以反映综合解决方案.
Thanks to the answers, I updated the question to reflect the combined solution.
推荐答案
尝试以下步骤:
- 确保两个项目(非托管 C++ 和托管 C#)具有相同的位数(x86 或 x64).为了清楚起见,假设它是 x86.
- 打开 Admin 命令提示符并注册您的 COM DLL:
C:WindowsSysWOW64 egsvr32.exe c:full-pathComLib.Interop.dll
- 以管理员身份运行 Visual Studio.执行步骤 1、2、4、5、6.不要做3.
- Make sure both projects, unmanaged C++ and managed C# have the same bitness, either x86 or x64. Let's say it's x86, for clarity.
- Open Admin Command Prompt and register your COM DLL:
C:WindowsSysWOW64 egsvr32.exe c:full-pathComLib.Interop.dll
- Run Visual Studio as Admin. Do steps 1,2,4,5,6. Don't do 3.
看看你能不能达到 7.我认为这应该可行.
See if you get to 7. I think that should work.
请注意,您只需要在开发机器上进行注册.独立的 COM 应该可以在其他任何地方工作.
Note you only need the registration on the Development machine. Isolated COM should work everywhere else.
这篇关于如何在 Visual Studio 2012 中创建 HelloWorld COM 互操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!