I'm just looking for a best way to re-use code written in c#, in my c++ projects. Creating a com\service doesn't look like a best option for my needs. How difficult it is to export c# code into a dll and use it in c++? can i get some suggestion or example? is this usual requirement or ? Please help me.
i use win7, VS2008, win7sdk
Thanks & Rgds, ~calvin
Executing managed code from an unnamaged executable is possible, though not quite easy. You can look into this article for an introduction and this book to go further.
I personally would avoid this kind of things in most cases and, if possible, switch the C++ project to C++/CLI to obtain an immediate compatibility with .Net assemblies for a minimal cost.
You can't export C# code into a native dll afaik. At least without very much pain in your buttocks. You should have thought beforehand and write the reusable part in C, thus creating a native DLL which could be used from all languages.
Also, you could try managed C++ - I personally hate it... but there you go
In case you need to use code written in C# C++, then you need to first see what all data types you would be passing from your C++ code to C# code.
1. Basic data types like int, enum etc can be passed from unmanaged to managed code.
2. in case you want to pass on class object, than you need to use marshalling.
If you can't use COM (if the .NET code is already written for example), then you can host the CLR, but this is a long road...
See these other articles
How to load CLR into process and Create a C# DLL That Can Be Imported in a Delphi App Using stdcall - Possible?
Related
I'm working on a C# project at the moment and I have zero experience with C#.
We have a set of DLLs written in C++, they have the same classes and same functionality, but they work differently. As an instance let's say our program converts BMP files. We have DLLs for converting BMP to JPG, BMP to PNG and so forth.
Our program which is written in C# somehow should wrap those DLLs, but not in a static way. We'd like to be able to change the DLL in use during runtime, and add new DLLs when need be (also during runtime). Like plugins, if you will.
What would your suggestion be to implement this approach?
This looks good: http://blogs.msdn.com/b/jonathanswift/archive/2006/10/03/dynamically-calling-an-unmanaged-dll-from-.net-_2800_c_23002900_.aspx
If you don't know what all of your DLLs are ahead of time, you will probably need to call the Win32 function "LoadLibrary" "GetProcAddress" and "FreeLibrary." As far as I know, c# does not have a way to do a sort of Dynamic PInvoke automatically. See this blog post for more info
Alternatively, you could create a C# wrapper for each of your DLLs using PInvoke and then use managed delegates to call the functions. Then, by changing the delegate, you can call a different function (presumably in a different DLL) at runtime. This would depend on you having a complete list of the DLLs you intend to use at compile time. It is quite a bit more robust than the first option though. It's also a thousand times easier to let the runtime handle all of the marshaling for you. See this MSDN article for more information.
If they are already in C++, I would opt to write the managed assembly in C++.
This walkthrough could be a starting point. Especially the C++ guy does not need to learn C#.P/Invoke is a little bit slower, but the deeper issue is for you the version management as P/Invoke will lead to runtime-exceptions and not compile errors in case of changes.
You could use extern and DLLImport like facilities provided by Microsoft, For more details Go here or Here
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
I need to create a VC++ wrapper in C#. Is there a way to automatically generate the code?
Edit: let me clarify a bit: I have a simple project with complicated math functions (computing magnetic declination) in c++. Its just three files, one header, one command line controller and the library.
I was looking at SWiG but I found it to be enigmatic :P. I'm taking a look at C++/CLI.
Any tips and pitfalls to watch for?
Take a look at: Using Unmanaged C++ Libraries (DLLs) in .NET Applications
Or you can use C++/CLI
SWiG supports C#. But a C++/CLI wrapper will be much more ".NET-like" than one automatically generated by SWiG.
You can have a look at this tutorial:
http://www.codeguru.com/csharp/csharp/cs_data/article.php/c4217
I think it's better if you make your own wrapper than using any tool (if it does exists). The reason is that you can create a better C# wrapper using the right philosophies instead of generating a list of function call from a DLL.
And for the pitfalls, the only thing I can say is that since you are going to mix manage and unmanaged class, be sure that your struct/parameters are matching (sizeof or types).
As for most short questions: It depends on your settings and requirements! ;-) If you have a more C style interface, you might be able to solve your problem just by using Interop. If "real" OO progamming and C++ are involved, you probably have to look at C++/CLI. That can be easy, but it can also become painful - depending on your classes. As far as I know, there's not automatic code generation tool.
I have a large application written in C++, For various reasons I am going to rewrite it in C#, I have done plenty of Delphi to C#, VB to C# but C++ to C# I have never done, although I am competent in C++ I want this to be as smooth a conversion as possible.
Mainly what I am asking is what pitfalls await me in this conversion is there any key areas I should be aware of or any advice you can provide me.
This article is quite good, but is there anything else I should be weary of?
http://msdn.microsoft.com/en-us/magazine/cc301520.aspx
Main pitfall - do not think it's an upgrade. These are DIFFERENT languages, and in many places you will need complete different approach to the problems. So you should think reimplementation with minimal code reuse.
This article is decent.
I'd advice you to pay attention to the objects lifecycle.
In C++ you destroy objects explicitly when you've done with them. In C# (.NET) you don't. It can happen that an object holds on to some important resource (file handle, database connection etc.). If it is an issue, make use of the using directive.
You need to translate the spirit of the code, but not the code itself. You need to leave behind all the things you had to do in C++ because that was how it was done there. Good translation is highly creative process, so be creative.
The handling of strings was a pitfall for me at the beginning. While I Visual C++ you use pointers the methods in C# have indeed return values.
dummy = dummy.Replace("a", "b");
If you have C++ dll and you want to use them in your C# project you can used them by pinvoke and DllImport
There will be lot of differences while you will try to convert or rewrite unmanaged code to managed one. Here is a C++ to C# converter which is quite good for converting your c++ code to C# , though you can not expect to convert the whole project using it.
I have a DLL made in C#, this DLL contains some clases like Creator.
I need to load this DLL and use Creator class in C++ unmanaged,
so Is there some way to create that instance or must I load just the functions exposed?
I need something like this:
CreatorInstance->Init();
Is this posible?
Most of what you need can be found here: http://msdn.microsoft.com/en-us/library/x0w2664k%28VS.80%29.aspx
Primarily, you need to learn about the /clr switch for C++ compilation. Then you need to understand the C++ extensions that Microsoft added to allow for mixed assemblies. (A C++ "pointer" to a managed class would use p^ instead of p*, and so on.)
John Fisher's approach using C++/CLI is by far the easiest means of handling this, but it is not the only means.
The other three options are:
1) Use COM interop to wrap the .NET class via COM
2) You can host the CLR in your native, unmanaged application, and call into it. For details, see this article.
3) You can host the Mono runtime, and use it to call your managed code. For details on this, see this page.
Option 2 and 3 are very similar, but IMO, 3 is easier than 2.
Here is an interesting article on how you should be able to accomplish this without using the /CLR option
http://www.codeproject.com/KB/cs/ManagedCOM.aspx
Works pretty well.
First of all, it is possible and you do not "have" to use CLI or the /clr switch. Using the good old COM architecture you can do it pretty easily http://msdn.microsoft.com/en-us/library/zsfww439.aspx. Understanding the way COM works might be the biggest challenge here, but it's usefull once you know it.