Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have three C++ projects under one solution file and each project has multiple C++ files. I need to write C++ CLI wrapper so C# application can access functionalities of C++ classes.

I have created managed dll project with one class which is mocking all functionalities of one C++ class.

My question is, do I need to create three managed dll project referring to 3 different C++ project? Also, do I need to have one to one mapping between C++ class and managed class, e.g. one managed class for one native C++?

Regards,
Maddie
Posted
Comments
Nathan Minier 13-Oct-15 11:16am    
Why don't you just write one managed mapper that PInvokes from all three? Unless you plan to have a highly mutable application, I would think that this would be the most expedient route.
Sergey Alexandrovich Kryukov 13-Oct-15 12:44pm    
There no difference how many C++ modules (projects are irrelevant, you wrap the modules, more exactly, separate functions using P/Invoke) you wrap. You can wrap them in a single assembly (project), not only in a single solution. It's not clear why you see a problem here. Any particular concerns?
—SA
Philippe Mori 14-Oct-15 12:41pm    
You can do whatever you want. If you current C++ code is well designed and almost fit .NET philosophy, you can make simple wrappers for each public class.

If you existing code is discutable or hard to use from .NET, then you might design the managed classes cleanly and do complex stuff under the hood.

A single mixed mode DLL can uses multiple projects. You can also create one wrapper for each DLL. It really depends on the usage whichever is preferable.

1 solution

First of all, please see my comment to the question.

If those C++ projects are yours and you have the source code, the best way to create a CLI wrapper would be migrating your projects from C++ to C++/CLI. This way, you can create mixed-mode
modules (managed+unmanaged) of dual purpose: they can be uses as regular unmanaged library modules, as well as regular .NET assemblies, so you could use them in your C# or VB.NET projects in a usual way, by referencing the assemblies. And the best way to organize such C++/CLI projects is to separate them into unmanaged code and managed wrappers for it. You can wrap unmanaged classes in your managed CLI "ref" classes/structures and expose the the referencing assemblies them by making public.

Please see: https://msdn.microsoft.com/en-us/library/ms235282.aspx[^].

See also my past answers:
How to invoke C++ DLL in ASP.NET?[^],
Dealing with windows form application[^].

If you still need to use explicit P/Invoke, by one or another reason, please see: https://msdn.microsoft.com/en-us/library/2x8kf7zx.aspx[^].

You will see that everything is based on DLLImportAttribute, which is uses based on the library module name (DLL) and an entry point name, per function: https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute%28v=vs.110%29.aspx[^].

It means that you can wrap any number of module even in a single project, not only a single solution.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900