I have a legacy application written in C , and i would like to gradually move some code to c#. But before being able to rewrite everything i will need to have just few components writen in c# first that are going to be used from C.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I assume your C# class is a static class. You need to create an interop layer in C++/CLI before you can use it in pure C. Create a C++/CLI class to wrap your C# class. Once that is done use the export function to export the specific C functions. C++/CLI will manage the interop on your behalf. The rule of thumb is if you class/function has any CLI it WILL BE CLI. So your extern functions should only return native datatypes.
Here is an article to help you get started. It converts C++ to C# but the process is reversed in your case. CodeProject Unfortunately there is no convenient reverse PInvoke for pure C.
Unfortunately I have never gone from C# to C. It sounds like an interesting project. Good luck!
Ok If you have not figured it out yet i have a quick sample for you.
C# CSLibrary.Math.cs
Cpp/C++ Project CPPCLibrary.h (Compiled with C++/CLI Option with project dependencies)
C Project CTest.c (Compiled as C Code)
All files are in the same solution but different projects. I have confirmed this has worked. I hope this helps anyone who comes across it.
Cheers!