如何在Visual Studio 2012创建的HelloWorld COM互操作操作、如何在、Visual、Studio

2023-09-02 01:42:19 作者:清酒桃花

首先,我是新来的COM,目前很困惑。我读过关于COM MSDN上和一般网站大量的文档资料,但很多似乎已经过时和过于复杂。

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简单对象),我称之为InteropDemo 在类视图中,右键单击IInteropDemo,并添加一个方法HelloWorld的。 Create a C# console app project. I name it CSharpApp Create a C++ ATL project. I call it ComLib.Interop. Add class (template: ATL Simple Object), which I call "InteropDemo" In class view, right-click IInteropDemo and add a method HelloWorld. 呼叫regsrv32.exe在编译COM DLL,然后选择DLL中的COM选项卡中的添加引用...

感谢的答案,我更新的问题,以反映综合解决方案。

Thanks to the answers, I updated the question to reflect the combined solution.

推荐答案

请尝试以下步骤:

确保这两个项目,非托管C ++和托管C#具有相同的位数,x86或x64。比方说,这是86,为了清晰。 开启管理命令提示符并注册COM DLL: C: WINDOWS Syswow64资料 regsvr32.exe的C:完整路径 ComLib.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:WindowsSysWOW64regsvr32.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.