Converting exsisting C++ code to Managed C++ which I can use it from ASP.NET Application

Hi experts,

I have an application which is written in C++ MFC. We have another application written in C#.NET and we need to merge these applications. I want to reuse most of the code from C++. I do not have any idea currently what is that going to involve. Please send me some pointers where I can get help of how should I do this, and I expect as little code change as possible. The C++ application is not done in a code reuse in mind. There are no COM dlls created. Everything is done using plain C++ classes.

Thanks
LVL 6
dharmesh_amityAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
>>I wanted to know if there is a technique by which I can create wrapper classes in Managed C++ and call the
>>unmanaged C++ code

Oh, yes, there is - see http://msdn.microsoft.com/library/en-us/dndotnet/html/manunmancode.asp?frame=true ("An Overview of Managed/Unmanaged Code Interoperability") and http://msdn.microsoft.com/library/en-us/vcmex/html/vcconfeaturesofmanagedextensionsforc.asp ("Managed Extensions for C++ Development Scenarios") , with http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_overview.asp ("Introduction to Wrapping C++ Classes") being probably what you're looking for. There's even some sample code at http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_appendixsamplecode.asp 

// wrappingClass1.cpp
// compile with: /clr /LD
#using <mscorlib.dll>
#include <atlsecurity.h>

using namespace System;
using System::Runtime::InteropServices::Marshal;

public __gc class ManagedCSid : public ICloneable
{
public:
   ManagedCSid(String *, String *);
   ManagedCSid(String *);
   ManagedCSid() {m_pCSid = 0;}
   LPCTSTR GetUnManagedString(String *);
   void init(String *, String *);

   static ManagedCSid * op_Explicit(String * pszAccountName)
   {
      ManagedCSid * t = new ManagedCSid(pszAccountName);
      return t;
   }

   ManagedCSid * FromString(String * pszAccountName)
   {
      ManagedCSid * t = new ManagedCSid(pszAccountName);
      return t;
   }

   ~ManagedCSid()
   {
      m_pCSid -> ~CSid();
   }

   __property String * get_AccountName()
   {
      return m_pCSid -> AccountName();
   }

   __property String * get_Domain()
   {
      return m_pCSid -> Domain();
   }

   bool Equals(Object * rhs)
   {
      ManagedCSid * s = dynamic_cast<ManagedCSid *>(rhs);
      if (!s) return false;
      return (s -> m_pCSid == m_pCSid);
   }

   static bool op_Equality(ManagedCSid * lhs, ManagedCSid * rhs)
   {
      return (lhs -> Equals(rhs));
   }

   int GetHashCode()
   {
      return reinterpret_cast<int>(m_pCSid -> GetPSID());
   }

   virtual Object * Clone()
   {
      ManagedCSid * m = new ManagedCSid;
      m -> m_pCSid = m_pCSid;
      return m;
   }

protected:
   virtual ManagedCSid * Assign(ManagedCSid * rhs)
   {
      if(this != rhs)
      {
         m_pCSid = rhs -> m_pCSid;
      }
      return this;
   }

private:
   CSid __nogc * m_pCSid;
};


ManagedCSid::ManagedCSid(String * pszAccountName, String * pszSystem)
{
   init(pszAccountName, pszSystem);
}

ManagedCSid::ManagedCSid(String * pszAccountName)
{
   init(pszAccountName, String::Empty);
}

LPCTSTR ManagedCSid::GetUnManagedString(String * s)
{
   LPCTSTR lstr = 0;
   try
   {
     
      lstr = static_cast<LPCTSTR>(const_cast<void*>(static_cast<const void*>(Marshal::StringToHGlobalAuto(s))));
   }
   catch(ArgumentException * e)
   {
   // handle the exception
   }
   catch (OutOfMemoryException * e)
   {
// handle the exception
   }
   return lstr;
}

void ManagedCSid::init(String * pszAccountName, String * pszSystem)
{
   LPCTSTR _pszAccountName = ManagedCSid::GetUnManagedString(pszAccountName);
   LPCTSTR _pszSystem = 0;

   if (!(String::Compare(pszSystem, String::Empty)))
   {
      LPCSTR _pszSystem = GetUnManagedString(pszSystem);
   }
   m_pCSid = new CSid(_pszAccountName, _pszSystem);
      Marshal::FreeHGlobal(static_cast<IntPtr>(const_cast<void*>(static_cast<const void*>(_pszAccountName))));
   _pszAccountName = 0;

   if (!(String::Compare(pszSystem, String::Empty)))
   {
      Marshal::FreeHGlobal(static_cast<IntPtr>(const_cast<void*>(static_cast<const void*>(_pszSystem))));
      _pszSystem = 0;
   }
}
0
 
jkrCommented:
I'd suggest to start with http://msdn.microsoft.com/library/en-us/dndotnet/html/win32map.asp ("Microsoft Win32 to Microsoft .NET Framework API Map")
0
 
jkrCommented:
Actually, http://msdn.microsoft.com/library/en-us/vcmex/html/vcorimigratingyourapplications.asp ("Migrating Your Applications") miht be the better way to start - using the other link as a reference.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
dharmesh_amityAuthor Commented:
jkr,

This one shows mapping between Win32 to Microsoft .NET Framework API. My C++ classes written in MFC and also I do not intend to change the code. I wanted to know if there is a technique by which I can create wrapper classes in Managed C++ and call the unmanaged C++ code. The exsisting code works and I want as little change in that code as possible.

Thanks
0
 
dharmesh_amityAuthor Commented:
jkr,

Sorry about the comment before. That was based on your first comment. I will check this one out. I will keep this question open for today to see if I get some more thoughts.

Thanks
0
 
dharmesh_amityAuthor Commented:
This is all good stuff.

Thanks a lot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.