Click here to Skip to main content
15,887,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We have a program written in C that is calling functions in a 15 year old C++ exe. I would like to rewrite this C++ exe using C# that exposes the same functions that the C++ exe exposes. Not clear in anything I have seen so far as to how this can be done. Note that the C# app must be an exe, not a dll, as it needs to be running in the background.
Posted
Comments
R. Giskard Reventlov 26-Apr-12 13:11pm    
If it ain't broke why do you want to fix it? Do you mean running as a windows service?
Member 3027075 26-Apr-12 13:24pm    
The 15 years old C++ exe has some bugs need to be fixed and it has been a tough one for us since it was not well written in term of comments, error checking...
Thus, we decide to rewrite it.
It should be running as window service, just in the background.
R. Giskard Reventlov 26-Apr-12 13:29pm    
Why not just rewrite it in C++? Surely that has to be the simplest course of action.
Sergey Alexandrovich Kryukov 26-Apr-12 14:41pm    
There are several alternatives to be considered -- I listed what I can immediately see in my answer -- please see.
--SA
Sergey Alexandrovich Kryukov 26-Apr-12 13:22pm    
Not clear what exactly you want. In particular, why not using old code? Why not using P/Invoke?
--SA

Please see my comment to the question. You should think about it first.

The whole idea of "conversion" from C++ to C# is wrong. There is no such thing as "conversion". The languages are different in their nature, work on different platforms, on different types of platforms, using very different concepts. One could not define what is a "conversion" or even a translation. However, from the standpoint of a particular application, you can always somehow translate the code, or re-implement it using old code as a reference; it totally depends on the application and requirements.

In all cases, before doing so, you should think about the following alternatives: 1) use C++ code, 2) use combination of C++ code, export some function and use them in your C# project via P/Invoke, 3) translate your C++ code into C++/CLI and use it as the .NET assembly; 4) translate your C++ code into mixed-mode (managed + unmanaged) project using C++ and C++/CLI, re-use most of your old C++ code, wrap the methods or even classes you need in C++/CLI managed "ref" classes and reference this as a regular .NET assembly in any other .NET projects.

Don't tell us "it must be" before you really understand all the alternatives and the techniques involved. You cannot consider your decision as valid or even relevant before you understand it.

In you need to learn P/Invoke, please see:
http://en.wikipedia.org/wiki/Platform_Invocation_Services[^],
http://msdn.microsoft.com/en-us/library/Aa712982[^].

This CodeProject tutorial article can also be useful:
Essential P/Invoke[^].

Information on C++/CLI:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^].

—SA
 
Share this answer
 
v3
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Apr-12 13:38pm    
Frankly, the whole idea of service is based on a shaky ground here: on the OP's notion of "working in the background". I'll vote 4 for useful references, but we don't really know what OP needs (and, as it often happens, I am not sure OP really understands it). I provided more general set of alternatives, only about languages and interoperability options -- something to be considered first. Please see my answer.
--SA
[no name] 26-Apr-12 14:02pm    
This is actually much clearer than his first attempt. The whole notion that he has a C program calling functions in a C++ exe should tell you something.
Sergey Alexandrovich Kryukov 26-Apr-12 14:40pm    
Yes, it does tell something. :-)
--SA

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