Is it even possible to create GUI layer in C# and rest of application in C++? If I am not wrong one of antyvirus software had GUI made in Delphi. How it could be done ?
You have several options for doing it, including:
Use P/Invoke to call into the C++ DLL from C#.
Expose a COM interface from the native code, and call it from C# using COM interop.
Write a native Windows service and call into it from managed code.
Use C++/CLI to write a managed library in C++, which you can easily link to from C#.
If you're starting from scratch, option 4 is probably your best option. (Aside from just writing the whole thing in C#, that is.) The first three options all involve some additional wrangling and overhead, and probably aren't worth the hassle if you don't have a compelling reason such as needing to interact with an existing native library or having some need for a service-oriented architecture.
If you write your business logic in C++/CLI, and your UI in C#, it shouldn't be a problem. If you want to write in pure ANSI C++, you might have to write C++/CLI wrappers around the objects you want to expose to C#.
write the app logic in c++ dll, then use pinvoke from c# to talk to the dll.
See this answer. It seems to answer your problem
Related
I want to use both C++ and C# in my application.
C# for GUI design and C++ for processing.
But I don't have any knowledge about this. How to communicate between them.
I don't know where I have to begin and research.
Someone can tell me the overview about this technology? And if someone have document about this topic, please give it to me.
I'm using Visual Studio 2010 for development.
Many thanks,
T&TGroup
The technology you are looking for is C++/CLI, a proprietary language extension for C++, that allows interaction with .Net code.
The basic idea is this: You write your C++ libraries as ever in portable ISO C++. Then you add a thin wrapper in C++/CLI for those C++ components you want to call from C# (or any other .Net language for that matter).
Just be aware that C++/CLI is only intended to write code for interaction with .Net. Don't be tempted to write the implementation in CLI as well, as you will end up with code that is not portable and probably a lot harder to maintain than the pure C++ version.
It depends on what the architecture of your application should be, you can for example create two different application one that is the core and another that is the GUI and communicate through messaging.
On Windows you can use Windows message queue for example, to let the two end point communicate with each other.
You can either use C++ CLI or native C++. C++ CLI is managed code and native c++ will be unmanaged by the CLR. The choice between the two depends on your usage. There are certain limitations with C++ CLI.
It depends totally on the architecture and requirement as well. You can write processing instructions in C++ (lib) use them in GUI. Can be done in VS 2010 as well easily
this might be a simple question, but as I have zero experience in C++ and my boss has given me a C++ solution work with I need to ask this question.
I have a C++ solution that needs to be turned into a .NET class library which can then be used in another .NET solution done in C#.
Is this even possible???
Cheers
You have to wrap all C++ classes that should be visible in .NET in C++/CLI classes.
Read this
There are also some automated tools to do this, but I never used these (I don't really trust them). Depends on how many C++ classes do you have, and how complex they are. Writing the wrappers is mostly straight-forward once you get used to some oddities of C++/CLI.
It depends of the output of the C++ solution. Is it an application (*.exe) or a library (*.dll)?
If it is a library (*.dll) you can use Platform Invoke Tutorial to call its functions out of managed code. A lot of examples of calling Win32 native functions can be found at http://pinvoke.net/.
If your result is an application it could be possible, that it has a COM interface. In that case you could use the COM interop to communicate with your application.
Last but not least you could write with C++/CLI a managed wrapper around your C++ functionality. But this is a lot of work and has many pitfalls.
I was wondering if I can use a library -written in C++- in C#
The problem is that library has its own structures and classes..
Will I be able to use it in C#?
Thanks
EDIT This library is open source..
so if my requirements needs something special in C++ code, I will be able do it...
You cannot directly use C++ classes in managed code. The chief problems are not being able to use the same memory allocator as used by the C++ code and not being able to invoke the constructor and destructor easily. A Microsoft employee posted a blog post to show that it is not impossible. I would not recommend doing this.
COM is a solution but that invariably requires a fairly big rewrite and good COM programming skillz. A managed class wrapper in the C++/CLI language is usually the best solution. You could take a peek at the SWIG tool to consider auto-generating those wrapper classes. Beware however that this tool can easily create more problems than it solves.
There are two ways, both using an Adapter (which maps C++ classes to .NET classes):
C++/CLI
COM
The former avoids going via COM, and much of the C++ code might be able to be just compiled with the correct switches.
Additional: In theory P/Invoke might be possible, but all the C++ semantics would be lost, you would need to handle C++ object lifetime manually (and instance references as IntPtr). Plus of course you would need to call the mangled names...
Another option is to write a managed wrapper in C++/CLI. I prefer that instead of using P/Invoke.
Is it possible to share references to C# objects between C# and C++ code without massive complexity? Or is this generally considered a bad idea?
The best solution for sharing a C# object between native and managed code is to use COM interop. This allows you to essentially share an interface of an object between managed code and it's equivalent signature in C++.
As for the complexity side of things. The majority of COM interop scenarios are straight forward and really are no more complex than good old COM programming. On the managed side it looks really no different than a normal interface.
Once you introduce multiple threads or start playing around between COM apartments though, things can get a bit tricky.
In my experience, the easiest way to get this working is the following.
Define an interface in C# that you wish to use in C++
Mark the interface with the ComVisible(true) attrbute
Run tlbexp on the assembly which generates a TLB file
Import the TLB into your native project
This will get the interface definition into both of your projects. How to pass that between the projects requires a bit more detail into your architecture.
Another solution I can recommend, from personal experience, is to use a managed C++ interface between the two if the C++ code you want to access is too large or too complex.
For example, I am using the RakNet C++ network library in a C# project. The solutions are to either create a massive wrapper class in C# to access the required C++ functions, create a C++ wrapper around those functions which can than be used as a COM interop or use Managed C++ (Visual C++/CLI).
I chose the latter which allows me to use C++ to access the RakNet library, but the classes created can be used directly in another .NET project as if. So the main logic has been created in those Managed C++ classes, which also allow me to use the .NET framework and some of its wonderful features. In my C# project I simply need to call the Managed C++ library which provides me with all in all 20 functions I need to perform everything.
Is there a best practice for accessing C++ native COM functions to interop from C#?
For example, if I have 100 C++ methods (basically a native library) that interacts with a core window component.
I want to basically make a wrapper for these C++ methods in C#, so all my newly hired employees can use that instead of C++, etc. The C++ code is legacy and scares me, so I want to deal with it just once. Is the approach here for each method to have a corresponding C# method? In fact, is there another way of doing this?
Can I have some sort of wrapper subsystem. How do you people generally do this?
Also, are there any performance considerations, etc.?
If your C++ methods are in a COM object, then you can use COM interop from C#. See CLR Inside Out: Introduction to COM Interop for a good introduction.
If those C++ methods are more like traditional API calls, then you'll want to use Platform Invoke (i.e. PInvoke). That entails creating managed prototypes in C# for the unmanaged (C++ functions). A good place to start is the Platform Invoke Tutorial.
As far as performance considerations go, there typically won't be much to worry about. Calling from C# might be fractionally slower than calling directly from C++, in large part due to marshaling data. Unless the code you're calling is in a critical loop, you're not going to notice any difference.
It really depends on what those native functions do. The more you have to share data between the unmanaged and managed worlds, the more difficult the process becomes. Without more information about your specific functions, it's difficult to say where you might encounter problems.
Use COM Interop to wrap the library. Then you can more or less treat the C++ code as .NET native code that can be called in the normal fashion.