I have a windows forms app written in C++/cli. I want to extend this app with some new forms and I'd like to create them in C# in a separate project.
Is it possible to simply add a C# project to a solution that has the C++ project and the two will interact? By interaction, I mean that, say, a button clicked on a form written in the c# project will be able to call methods in the c++ project. Asked perhaps in a different way, can an object in the C# project reference an object in the c++ project? if so, do you know of an example code to get me started?
Thanks.
If a positively charged body is brought near to a neutral or uncharged body, it induces a negative charge on the near side and a positive charge on the far side of the neutral object. This creates a force of attraction between the two bodies.
Positive and negative charged objects attract or pull each other together, while similar charged objects (2 positives or 2 negatives) repel or push each other apart.
In contrast to the attractive force between two objects with opposite charges, two objects that are of like charge will repel each other. That is, a positively charged object will exert a repulsive force upon a second positively charged object. This repulsive force will push the two objects apart.
If a positive charge and a negative charge interact, their forces act in the same direction, from the positive to the negative charge. As a result opposite charges attract each other: The electric field and resulting forces produced by two electrical charges of opposite polarity. The two charges attract each other.
Yes. A C++/CLI application will be able to interface with a C# application, in one of two ways:
If you are using CLI extensions (which from your post it sounds like it), you will be able to write code using the new object references:
Managed objects: System::String^ myString
(in C++) is the same as string myString
in C#
Managed refs: System::String% myString
is equivalent to ref string myString
.
If you want to use C++ native types, then you will have to use P/Invoke, but that's an entirely different category. For what you want to do, just add the C++ project as a reference to your C# project, write a publicly-visible class in C++ using managed types, and then compile. Your project should then be visible to your C# class in whatever namespace you chose for the C++ class.
EDIT: oh, and if you need to allocate managed objects through C++, you will need to use gcnew
instead of new
.
This can be done by compiling a dll with the C++ project, then having the C# app load that dll and then it will be able to call its exported functions. This method allows your C++ code to be unmanaged code.
As far as finding a sample that is already set up, I can only find a Visual Studio 2008 project:Microsoft All-In-One Code Framework
For visual studio 2010, here's how to do the C++ side: How to make a dll with C++ Using Explicit PInvoke in C++
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With