3

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

rplusg
  • 3,418
  • 6
  • 34
  • 49

4 Answers4

2

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.

icecrime
  • 74,451
  • 13
  • 99
  • 111
0

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

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • "should have thought beforehand" --> unfortunately it is very big c# code base and was written\developed in my company long time back, i think, during this development, i was in college and playing CounterStrike :) – rplusg Nov 25 '10 at 13:30
0

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.

0

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?

Community
  • 1
  • 1
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298