?
Solved

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

Posted on 2005-05-02
6
Medium Priority
?
522 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:dharmesh_amity
  • 3
  • 3
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13910060
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
 
LVL 86

Expert Comment

by:jkr
ID: 13910110
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
 
LVL 6

Author Comment

by:dharmesh_amity
ID: 13910124
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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
LVL 6

Author Comment

by:dharmesh_amity
ID: 13910162
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
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13910336
>>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
 
LVL 6

Author Comment

by:dharmesh_amity
ID: 13910532
This is all good stuff.

Thanks a lot
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question