How can I add a VC++ DLL as a reference in my C# Visual Studio project?

▼魔方 西西 提交于 2019-12-04 11:35:47

There are two options for using a C++ DLL from C#: either COM interop, or P/Invoke. COM Interop involves creating a COM object in your C++ DLL, and then adding it as a reference. You can use the COM object like a C# object (for the most part) at this point.

P/Invoke allows you to call exported functions from C# (think calling standard Win32 API functions from C#). This is likely easier to set up, since all you need to do is export a function, however that could cause you to refactor your code in the C++ DLL, since it's not a very OOP way of doing things.

You can only use C++ components in C# when they have been prepared for use, for example by being written in C++/CLI or being written as a COM server.

If your component is a plain C++ dll you'll need to write some wrapper code, probably best is C++/Cli

I am not sure whether this solve..

run:

tlbimp /out:MyOldCom.dll MyNewAssembly.tlb

Then use it as you would any other assembly.

Please refer

http://msdn.microsoft.com/en-us/library/aa302324.aspx

http://msdn.microsoft.com/en-us/magazine/cc301501.aspx

ie)

One way is to package your DLL as a COM class and Another way is using DllImport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!