I have a C++ project and a DLL library made using C#. Is it possible to add it to the C++ project and use its methods? I am using Visual Studio开发者_运维百科 2008
You have two options that I know of:
1) You can compile the C# DLL with COM support, and then access it from C++ via COM. This will involve COM Interop which can be slower than managed-to-managed .NET calls.
To compile a C# DLL with COM support, go to the project's Properties->Application->Assembly Information->Make assembly COM-Visible. You use regasm to register the DLL on the users machine. This is similar to using regsvr32, except regasm is for .NET DLLs:
http://www.csharphelp.com/archives/archive190.html
I personally also use the /CODEBASE switch with regasm for DLLs that I intend to be for COM use only. It will still work without the /CODEBASE switch, but the DLL will show up in the Global Assembly Cache as well, which just adds clutter to the GAC and enrolls you in alot of features I don't need for my COM interop scenarios.
You can also set "Register for COM-Interop" under Build, but this is not required. It is just helpful when repeatedly debugging, as it will register the COM DLL with regasm on your machine when you compile it. It doesn't really have anything to do with the DLL itself. You will still need to regasm the DLL on user's machines.
After registering, you should be able to add a COM reference to the C# DLL, and make calls to it from native C++ just like you would any other COM component.
Or
2) You can compile the C++ project as a managed C++/CLI project, allowing you to reference .NET DLLs, such as a C# DLL. I think this would be the better option if C++/CLI isn't a problem for you. The calls between the C++/CLI code and C# DLL should perform better than COM Interop, but on the other hand C++/CLI itself is slightly slower than native C++.
http://support.microsoft.com/kb/828736
精彩评论