1

i'm exploring the idea of use .net as a scripting language for my program.

i found a lot of examples on internet that shows how to host .net and then, from c++ call functions from .net.

But i need to go on the other way too, from this code in c#, i need to be able to create/call c++'s objects from this c# script, so i need to expose functions/objects to control it from C#.

How can i do it?

Simple Example to show what i'm talking about I call my c++ method "CreateGUI". It'll call .net code: "InitializeGUI", and this "InitializeGUI" need to check if an object (for example, the texture) is instantiated inside C++.

Can someone help me?

Thanks!

Leandro Menezes
  • 103
  • 1
  • 4
  • 1
    Write managed wrappers for your C++ classes with the C++/CLI language. See http://stackoverflow.com/questions/2691325/c-cli-mixed-mode-dll-creation for an example. – Hans Passant Feb 20 '13 at 18:39
  • Hm, I'd love to know how do you want to use ".Net as a scripting language for your program..." – Paul Michalik Feb 20 '13 at 18:42

2 Answers2

0

If you use managed C++, you can create a managed C++ component which can be called from your C# code. If you use unmanaged C++, you can either create a C++ COM component and use it through C# via RCW (Runtime Callable Wrapper) mechanism or you can create a C++ dll and call it from C# via PInvoke.

  • OK, i tried it but not the right result.
    I created a singleton class into a static library, created a c++ managed dll to expose some methods from this static library, created a c# dll referencing this managed dll, then i crea
    – Leandro Menezes Feb 21 '13 at 05:12
  • OK, i tried it but not the expected result. I created a singleton class inside a static library, created a c++ managed dll to expose some methods from this static library, created a c# dll referencing and using this managed dll, then i created a c++ program that hosts .net and execute my .net dll. the problem is, when i call the methods from the singleton, the object is different when i call it directly from c++ and c#. how i can make both worlds see the same object? Thanks! – Leandro Menezes Feb 21 '13 at 05:18
  • OK, solved it, instead of calling the singleton i passed the pointer to the .net, this way i can call my original object. – Leandro Menezes Feb 21 '13 at 19:57
  • for reference, i used this page http://social.msdn.microsoft.com/forums/en-US/vcgeneral/thread/299da822-5539-4e5b-9ba7-b614e564c9f4/ and this http://code.msdn.microsoft.com/windowsdesktop/CppHostCLR-e6581ee0/view/SourceCode#content – Leandro Menezes Feb 21 '13 at 20:01
0

You might also investigate Mono, which is explicitly intended to be embedded and will run any compiled .NET assembly, meaning the full powers of C#, VB.NET, even F# will be available.

Ben
  • 6,023
  • 1
  • 25
  • 40
  • Thanks for your help. I thought about using it but can i debug mono from mixed applications? – Leandro Menezes Feb 20 '13 at 19:43
  • Depends on which debugger you use - of course you can debug with gdb or windbg. I've never tried to use VS on a program that embedded mono - or .NET, for the matter. – Ben Feb 20 '13 at 22:38