Technical 技术

Wrapping a C# DLL with COM Interface to be Called by C++ in VS2015

Step 1 – Run Visual Studio 2015.

Step 2 – Create a ‘Class Library’ project in VS2015.

Create a Call Library

Step 3 – Right click project -> Properties -> Build -> Select ‘Register for COM Interop’

Register for COM Interop

Step 4 – Create a class file in which you would put the COM interface and class.

Step 5 – Using the following namespace in your class file.

using System.Runtime.InteropServices;

Step 6 – Create an interface and set the following attribute.

[ComVisible(true)]

Step 7 – Generate GUID attribute for the interface using the ‘Generate GUID’ tool.

Step 8 – Add a new class which implements the interface just created. Add the 2 attributes below to the class.

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]

Step 9 – Generate GUID for the class.

Step 10 – Implement the function in the class.

Step 11 – Set [ComVisible(true)] in AssemblyInfo.cs

Step 12 – Build the project. A tlb file and a dll file would be generated.

Step 13 – Open your C++ project.

Step 14 – Put the tlb file in the project folder. Add the import code like below. After building your C++ project, a tlh file would be generated.

import “xxx.tlb”

Step 15 – Don’t forget to use the namespace you created in the C# dll. Myself always forget this.

Step 16 – Adding the codes in your C++ project like below.

CoInitialize(NULL);
interfaceInCSharp p(__uuidof(classInCSharp));
_bstr_t pt = p->functionInCSharp( );
CoUninitialize();

Step 17 – Use REGASM.exe to register C# dll.

Subscribe
Notify of
guest
9 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
trackback
invicta 31290
2 years ago

invicta 31290

although internet sites we backlink to beneath are considerably not associated to ours, we really feel they’re in fact worth a go by means of, so have a look

trackback
винтажные постеры
2 years ago

винтажные постеры

very few web-sites that occur to be in depth beneath, from our point of view are undoubtedly properly really worth checking out

trackback
3 years ago

Google

Always a huge fan of linking to bloggers that I adore but really don’t get quite a bit of link adore from.

trackback
3 years ago

Google

Here is a good Blog You might Uncover Fascinating that we encourage you to visit.

Mariko Ayars
3 years ago

Hi there, I discovered your website by the use of Google even as looking for a similar topic, your site came up, it seems to be great. I have bookmarked it in my google bookmarks.

Harry Matias
3 years ago

Excellent post. Keep posting such kind of information on your blog. Im really impressed by it.

우리카지노
3 years ago

Asking questions are truly nice thing if you are not understanding something entirely, except this paragraph offers nice understanding yet.

SMS
SMS
4 years ago

Thanks so much for the blog post.

เบอร์สวยมงคล

Thanks for fantastic info I was looking for this info for my mission.

ทิชชู่เปียกแอลกอฮอล์

A big thank you for your article.

ปั้มไลค์

Like!! Thank you for publishing this awesome article.

grolyrtolemcs
4 years ago

I’ve recently started a blog, the info you provide on this web site has helped me greatly. Thanks for all of your time & work.

Jeane Toomsen
4 years ago

Thanks a bunch for sharing this with all of us you really know what you’re talking about! Bookmarked. Please also visit my web site =). We could have a link exchange arrangement between us!

9
0
Would love your thoughts, please comment.x
()
x