From: njoycoding on 7 May 2008 09:12 Thanks for the reply I forgot to inform that m using VS 2005 (C# dll ) and VS 6.0 for (C++ dll ) On May 7, 3:09 pm, "Giovanni Dicanio" <giovanni.dica...(a)invalid.com> wrote: > I uploaded a sample solution on the web, so you can download that file and > open in VS2008 and study and experiment with it: > > http://www.geocities.com/giovanni.dicanio/vc/csharp_mfc.zip > > A simple screenshot is here: > > http://www.geocities.com/giovanni.dicanio/vc/csharp_mfc.jpg > > HTH, > Giovanni > > "Giovanni Dicanio" <giovanni.dica...(a)invalid.com> ha scritto nel messaggionews:OTDGKlCsIHA.524(a)TK2MSFTNGP05.phx.gbl... > > > > > > > <njoycod...(a)gmail.com> ha scritto nel messaggio > >news:00b9e7ea-db4f-41bf-98ae-ea0d142a5099(a)v26g2000prm.googlegroups.com... > > >> How do I Communicate between C# module and C++ module > > >> I hav a c# dll and want to pass strings , call methods of this dll in > >> to a C++ dll (MFC ) > >> (2 way communication between C# dll and C++ dll ) > > >> How do I do this ? > > > Suppose that you have a C# class that has a read-write 'Text' property > > which is a String (C# .NET String). > > This class is called 'CSharpClass' and is in the namespace TestCSharpLib1. > > > * CSharpClass: > > - properties: > > + Text (String) (read/write) > > - methods: > > + SayHello > > > You can use the new marshal library in VS2008 to help you marshal strings > > between C++ and C#. > > > Suppose you have an MFC application (.exe) and you want to marshal the > > string from C++ (CStringW / const wchar_t *) to C#, and back. > > > First you must enable CLR support in the MFC .exe. > > You can do that selecting the properties menu of your MFC project (right > > click on project item in Solution Explorer, and select 'Properties' in the > > popup menu). > > > Then select 'Configuration Properties'. > > Then, in the item 'Common Language Runtime support: ', select the value: > > > 'Common Language Runtime support (/clr)' > > > Then you should include the following lines after the normal #include's > > lines in the .cpp source file where you are going to call the C# class. > > The purpose of these lines is to use the marshal library of VS2008: > > > <code> > > // Use the marshal library in VS2008 > > #include <msclr/marshal.h> > > using namespace msclr::interop; > > </code> > > > At this point, you can use the marshal_as<> template. > > > I posted a sample code from a project of mine, that does both directions > > marshaling: C++ -> C# and back C# --> C++. > > The code is commented, so you can read the comments for each step: > > > <code> > > > // > > // TEST of string marshaling between C# and C++ > > // > > void CMfcCallsCSharpDlg::OnBnClickedBtnCallCs() > > { > > // Create an instance of the C# class > > // on the managed heap (using gcnew) > > TestCSharpLib1::CSharpClass ^ csharpClass = gcnew > > TestCSharpLib1::CSharpClass(); > > > // Read user's text from edit control > > CStringW strText; > > m_txtInput.GetWindowText( strText ); > > > // Prepare marshal context for marshaling string between C# and C++ > > marshal_context ^ marshalCtx = gcnew marshal_context(); > > > // > > // Marshal string from C++ (CStringW - const wchar_t *) > > // to C# (System::String ^) > > // > > System::String ^ s = marshalCtx->marshal_as< System::String ^ >( > > static_cast< PCWSTR >( strText ) ); > > > // Set string property into C# object > > csharpClass->Text = s; > > > // Invoke test method of C# object > > csharpClass->SayHello(); > > > // > > // Inverse marshaling, from C# to C++ > > // > > > // Convert from C# string back to C++ string > > CStringW strFromCS; > > strFromCS = marshalCtx->marshal_as< PCWSTR >( csharpClass->Text ); > > > // Show the string from C# > > CStringW strMsg = L"String from C# : "; > > strMsg += strFromCS; > > AfxMessageBox( strMsg ); > > > // Cleanup marshal context > > delete marshalCtx; > > } > > > </code> > > > Another option to communicate between C# and C++ is to use COM, but I do > > prefer the C++/CLI extensions of VS2008 - I think that it is simpler. > > > However, if your C++ MFC project is built using Visual C++ 6, you must use > > COM, because there is no C++/CLI extension in Visual C++ 6. > > (C++/CLI extensions are one of the top reasons to move to VS2008, IMHO.) > > > HTH, > > Giovanni- Hide quoted text - > > - Show quoted text -
From: Giovanni Dicanio on 7 May 2008 10:35 <njoycoding(a)gmail.com> ha scritto nel messaggio news:9451395b-80a2-4709-8b3e-4f5bffff138d(a)j33g2000pri.googlegroups.com... > Thanks for the reply You're welcome. > I forgot to inform that m using VS 2005 (C# dll ) and VS 6.0 for (C++ > dll ) If you must use Visual C++ 6 as C++ compiler, the only way you can communicate between C# and C++ objects is COM. However, would it be possible for you to migrate your C++ code to VS2005? In that way you can use C++/CLI extensions, which IMHO are easier than COM... If you can't do that, and you must use VC6 for C++, I think that COM is really the way to go. You can export your C++ code using some COM interfaces, and you can call these interfaces from C# side. You can also wrap your C# objects in COM interfaces, and call them on the C++ side. More complex than C++/CLI, but works. Giovanni
From: Ajay Kalra on 7 May 2008 11:18 On May 7, 9:12 am, "njoycod...(a)gmail.com" <njoycod...(a)gmail.com> wrote: > Thanks for the reply > > I forgot to inform that m using VS 2005 (C# dll ) and VS 6.0 for (C++ > dll ) > It will a lot easier if you make your C+= module managed (cant use VC6) and then communicate with any other managed module (c#, vb.net etc). Otherwise you will be restricted to use COM. -- Ajay
From: njoycoding on 8 May 2008 07:46 Thanks for the reply I wish I could hav don that but entire project is done in VC 6.0 and expected to be continued in same way........ On May 7, 8:18 pm, Ajay Kalra <ajayka...(a)yahoo.com> wrote: > On May 7, 9:12 am, "njoycod...(a)gmail.com" <njoycod...(a)gmail.com> > wrote: > > > Thanks for the reply > > > I forgot to inform that m using VS 2005 (C# dll ) and VS 6.0 for (C++ > > dll ) > > It will a lot easier if you make your C+= module managed (cant use > VC6) and then communicate with any other managed module (c#, vb.net > etc). Otherwise you will be restricted to use COM. > > -- > Ajay
From: Giovanni Dicanio on 8 May 2008 11:03
> I wish I could hav don that but entire project is done in VC 6.0 and > expected to be continued in same way........ Yes, lots of projects are still developed using VC6 :) So, you must go through the COM path... I would suggest ATL as an helper library (it's very thin) to wrap your existing C++ objects into COM objects (so they can be called from C#). Giovanni |