could i call a c# dll function in a c++ project

  • Thread starter Thread starter emasno
  • Start date Start date
E

emasno

i want to know that whether i can integrate a c# project to a c++
project. i know that one of the way is to create a c# dll and call
the c# dll function in the c++ project. however, i dont know the
exact way to do it. Do any suggestion ?? thanks. :cry:
 
emasno said:
i want to know that whether i can integrate a c# project to a c++
project. i know that one of the way is to create a c# dll and call
the c# dll function in the c++ project. however, i dont know the
exact way to do it. Do any suggestion ?? thanks. :cry:



----------------------------------------------------------

----------------------------------------------------------
color]

You can't do this directly with C# afaik but you have a couple of
options...

1./ The simplest, and the recommended way is to use COM, export your C#
class as a COM class and call the method you need

2./ You can use a mechanism called reverse p/invoke, C# does not
support this directly, but it is a feature of the CLR, in order to do
it from C# you would need to write your C# code, disassemble it (using
ildasm or similar) , make some minor changes to the assembly manifest
and re-assemble (using ilasm or similar) this is a process refered to
as "creative round tripping" there is an interesting article here
www.blong.com called .NET interoperability : .NET <-> Win32 and the
book ".NET and COM the complete interoperability guide" also covers
this mechanism.

There is one other option available to you, which is, if you are not
tied to C#, you can export from a "Delphi for .NET" assembly, or from
managed C++

Rgds Tim.
 
I just did this and it wasn't too hard (although it was a little tricky). I had to create a managed C++ dll. I had a couple functions in the managed C++ dll that would make calls into the C# dll. So in your unmanaged C++ code, you call the managed C++ functions, which will then call the C# dll. I could be wrong, but I couldn't find a way to use a managed C++ class in unmanaged C++, I could only call individual global functions. Here's a rough example of what my managed C++ .cpp file looks like

#include "stdafx.h

#using <mscorlib.dll
#using <System.dll
#using <System.Windows.Forms.dll
#ifdef _DEBU
#using "..\debug\MyCSharpdll.dll
#els
#using "..\release\MyCSharpdll.dll
#endi
using namespace System
using namespace System::Windows::Forms
using namespace System::ComponentModel

#ifdef _DEBU
#define new DEBUG_NE
#endi

extern "C" __declspec(dllexport) void DoSomethingCool(

tr

// This is managed C++, so we have to override the macro that is define
// for the new operator in the debug configuration
#pragma push_macro("new"
#undef ne
MyCSharpdll::SelectCodeDlg __gc * pDlg = new MyCSharpdll::SelectCodeDlg()
#pragma pop_macro("new"
ASSERT(pDlg != NULL)
if (pDlg->ShowDialog() == DialogResult::OK

// do something her


catch (...

#pragma push_macro("MessageBox"
#undef MessageBo
MessageBox::Show("Error in DoSomethingCool().")
#pragma pop_macro("MessageBox"



Note the push_macro and pop_macro statements... these are here because in C++ there are all kinds of #defines for things like "new" and "MessageBox". But in this case you don't want to use those definitions of those keywords, you wan't the .NET Framework definitions. So the #push_macro statement will allow you to use the .NET version of the keyword

I'm sure there's good websites on this kind of stuff out there somewhere, but I got all this info from a book (don't remember the name, sorry). Hope this helps..

Jon
 
Back
Top