managed C++ wrapper around unmanaged C++ classes: causing StackOverflow exception 
Author Message
 managed C++ wrapper around unmanaged C++ classes: causing StackOverflow exception

I have an unmanaged C++ class that I want to call/wrap from a managed
C++ class.  However, when I use the new operator to create the
unmanaged class from the managed C++ class I keep getting the
following error message

"An unhandled exception of type 'System.StackOverflowException'
occurred in my.dll"

I cannot find any recursive loops, the VS 2003 de{*filter*} just waits for
30 seconds or so after I hit F11 and then gives me the above dialog
box.

Question: Does anyone have a ZIP file of a complete example that
includes an actual unmanaged C++ class source code with a managed C++
class source code wrapper that I can use to test my environment?  How
about the actual source code files and project file for the example
from part-1 of the migration guide?

Here is what my current code looks like ... this is being compiled
into a C++ DLL that is being called by a managed C++ DLL that is being
called by C#.

...

#include "stdafx.h"

#pragma unmanaged

class CppClass
{

public:

// constructor
CppClass() {}

// destructor
~CppClass() {}

// methods
void native_f() {}

Quote:
};

#pragma managed

#using <mscorlib.dll>
using namespace System::Runtime::InteropServices;
using namespace System;

public __gc class m_MyClass
{
public:
m_MyClass()
{
m_session = new CppClass(); -- this function call is causing the
StackOverflow exception

Quote:
}

~m_MyClass()
{
delete m_session;

Quote:
}

private:

CppClass *m_session;

Quote:
};



Tue, 29 Nov 2005 12:47:16 GMT  
 managed C++ wrapper around unmanaged C++ classes: causing StackOverflow exception
I don't see anything wrong in your code.  Here's another working example of
wrapping:

Say you had an unmanaged C++ class like this:
#include < windows.h >
class UnmanagedClass {
public:
       LPCWSTR GetPropertyA();
       void   MethodB( LPCWSTR );

Quote:
};

To wrap this with Managed C++ you would do this:
#include < windows.h >
#using < mscorlib.dll >
#using < System.dll >
#include < vcclr.h >

using namespace System;

class UnmanagedClass {
public:
       LPCWSTR GetPropertyA();
       void   MethodB( LPCWSTR );

Quote:
};

public __gc class ManagedClass {
public:
       ManagedClass()
              : m_Impl( new UnmanagedClass ) {}
       ~ManagedClass() {
             delete m_Impl;
       }
       __property String*  get_PropertyA() {
             return new String( m_Impl->GetPropertyA());
       }
       void   MethodB( String* theString ) {
             const WCHAR __pin*  str = PtrToStringChars( theString );
             m_Impl->MethodB( str );
       }
private:
       UnmanagedClass*            m_Impl;

Quote:
};

Thanks,
Ulzii-

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

Quote:

> Newsgroups: microsoft.public.dotnet.languages.vc
> Subject: managed C++ wrapper around unmanaged C++ classes: causing

StackOverflow exception
Quote:
> Date: 12 Jun 2003 21:47:16 -0700
> Organization: http://www.*-*-*.com/
> Lines: 70

> NNTP-Posting-Host: 192.138.150.250
> Content-Type: text/plain; charset=ISO-8859-1
> Content-Transfer-Encoding: 8bit
> X-Trace: posting.google.com 1055479636 29344 127.0.0.1 (13 Jun 2003
04:47:16 GMT)

> NNTP-Posting-Date: 13 Jun 2003 04:47:16 GMT
> Path:

cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!news-out.cwix.com!newsfeed.cwix.co
m!newsfeed.frii.net!newsfeed.frii.net!140.99.99.194.MISMATCH!newsfeed1.easyn
ews.com!easynews.com!easynews!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supern
ews.com!postnews1.google.com!not-for-mail

Quote:
> Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:25008
> X-Tomcat-NG: microsoft.public.dotnet.languages.vc

> I have an unmanaged C++ class that I want to call/wrap from a managed
> C++ class.  However, when I use the new operator to create the
> unmanaged class from the managed C++ class I keep getting the
> following error message

> "An unhandled exception of type 'System.StackOverflowException'
> occurred in my.dll"

> I cannot find any recursive loops, the VS 2003 de{*filter*} just waits for
> 30 seconds or so after I hit F11 and then gives me the above dialog
> box.

> Question: Does anyone have a ZIP file of a complete example that
> includes an actual unmanaged C++ class source code with a managed C++
> class source code wrapper that I can use to test my environment?  How
> about the actual source code files and project file for the example
> from part-1 of the migration guide?

> Here is what my current code looks like ... this is being compiled
> into a C++ DLL that is being called by a managed C++ DLL that is being
> called by C#.

> ...

> #include "stdafx.h"

> #pragma unmanaged

> class CppClass
> {

> public:

> // constructor
> CppClass() {}

> // destructor
> ~CppClass() {}

> // methods
> void native_f() {}

> };

> #pragma managed

> #using <mscorlib.dll>
> using namespace System::Runtime::InteropServices;
> using namespace System;

> public __gc class m_MyClass
> {
> public:
> m_MyClass()
> {
> m_session = new CppClass(); -- this function call is causing the
> StackOverflow exception
> }

> ~m_MyClass()
> {
> delete m_session;
> }

> private:

> CppClass *m_session;

> };

--
Ulzii Luvsanbat, Microsoft Visual C++ Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.*-*-*.com/


Wed, 07 Dec 2005 02:20:45 GMT  
 managed C++ wrapper around unmanaged C++ classes: causing StackOverflow exception
I am also having the exact same problem. There does not appear to be
anything wrong with the code. Is there a project setting that needs to be
set to make this work? I'm using VS 2003.

Jeff McKelvey



Quote:
> I don't see anything wrong in your code.  Here's another working example
of
> wrapping:

> Say you had an unmanaged C++ class like this:
> #include < windows.h >
> class UnmanagedClass {
> public:
>        LPCWSTR GetPropertyA();
>        void   MethodB( LPCWSTR );
> };

> To wrap this with Managed C++ you would do this:
> #include < windows.h >
> #using < mscorlib.dll >
> #using < System.dll >
> #include < vcclr.h >

> using namespace System;

> class UnmanagedClass {
> public:
>        LPCWSTR GetPropertyA();
>        void   MethodB( LPCWSTR );
> };

> public __gc class ManagedClass {
> public:
>        ManagedClass()
>               : m_Impl( new UnmanagedClass ) {}
>        ~ManagedClass() {
>              delete m_Impl;
>        }
>        __property String*  get_PropertyA() {
>              return new String( m_Impl->GetPropertyA());
>        }
>        void   MethodB( String* theString ) {
>              const WCHAR __pin*  str = PtrToStringChars( theString );
>              m_Impl->MethodB( str );
>        }
> private:
>        UnmanagedClass*            m_Impl;
> };

> Thanks,
> Ulzii-

> --------------------

> > Newsgroups: microsoft.public.dotnet.languages.vc
> > Subject: managed C++ wrapper around unmanaged C++ classes: causing
> StackOverflow exception
> > Date: 12 Jun 2003 21:47:16 -0700
> > Organization: http://www.*-*-*.com/
> > Lines: 70

> > NNTP-Posting-Host: 192.138.150.250
> > Content-Type: text/plain; charset=ISO-8859-1
> > Content-Transfer-Encoding: 8bit
> > X-Trace: posting.google.com 1055479636 29344 127.0.0.1 (13 Jun 2003
> 04:47:16 GMT)

> > NNTP-Posting-Date: 13 Jun 2003 04:47:16 GMT
> > Path:

cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!news-out.cwix.com!newsfeed.cwix.co
m!newsfeed.frii.net!newsfeed.frii.net!140.99.99.194.MISMATCH!newsfeed1.easyn
ews.com!easynews.com!easynews!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supern

- Show quoted text -

Quote:
> ews.com!postnews1.google.com!not-for-mail
> > Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:25008
> > X-Tomcat-NG: microsoft.public.dotnet.languages.vc

> > I have an unmanaged C++ class that I want to call/wrap from a managed
> > C++ class.  However, when I use the new operator to create the
> > unmanaged class from the managed C++ class I keep getting the
> > following error message

> > "An unhandled exception of type 'System.StackOverflowException'
> > occurred in my.dll"

> > I cannot find any recursive loops, the VS 2003 de{*filter*} just waits for
> > 30 seconds or so after I hit F11 and then gives me the above dialog
> > box.

> > Question: Does anyone have a ZIP file of a complete example that
> > includes an actual unmanaged C++ class source code with a managed C++
> > class source code wrapper that I can use to test my environment?  How
> > about the actual source code files and project file for the example
> > from part-1 of the migration guide?

> > Here is what my current code looks like ... this is being compiled
> > into a C++ DLL that is being called by a managed C++ DLL that is being
> > called by C#.

> > ...

> > #include "stdafx.h"

> > #pragma unmanaged

> > class CppClass
> > {

> > public:

> > // constructor
> > CppClass() {}

> > // destructor
> > ~CppClass() {}

> > // methods
> > void native_f() {}

> > };

> > #pragma managed

> > #using <mscorlib.dll>
> > using namespace System::Runtime::InteropServices;
> > using namespace System;

> > public __gc class m_MyClass
> > {
> > public:
> > m_MyClass()
> > {
> > m_session = new CppClass(); -- this function call is causing the
> > StackOverflow exception
> > }

> > ~m_MyClass()
> > {
> > delete m_session;
> > }

> > private:

> > CppClass *m_session;

> > };

> --
> Ulzii Luvsanbat, Microsoft Visual C++ Team
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> Use of included script samples are subject to the terms specified at
> http://www.*-*-*.com/



Mon, 12 Dec 2005 14:18:26 GMT  
 managed C++ wrapper around unmanaged C++ classes: causing StackOverflow exception
I've discoverd the problem and am posting here for the benifit of anyone who
reads this.
My problem is that my project is linking with the c run-time library as well
as containing managed code. This is what Microsoft refers to as a Mixed Mode
Project. When doing this, the DLL is set up to have no entry point and thus
no way to setup static variables that are required by the CRT. You have to
initialize and shutdown the CRT yourself. There is a good article in MSDN
that explains all this. The article is titled, "Converting Managed
Extensions for C++ Projects from Pure Intermediate Language to Mixed Mode".
After putting in the init and shutdown code required for the CRT, everything
works fine! The other solution would be to not use the CRT but that was not
an option for me.

Jeff McKelvey


Quote:
> I am also having the exact same problem. There does not appear to be
> anything wrong with the code. Is there a project setting that needs to be
> set to make this work? I'm using VS 2003.

> Jeff McKelvey


message

> > I don't see anything wrong in your code.  Here's another working example
> of
> > wrapping:

> > Say you had an unmanaged C++ class like this:
> > #include < windows.h >
> > class UnmanagedClass {
> > public:
> >        LPCWSTR GetPropertyA();
> >        void   MethodB( LPCWSTR );
> > };

> > To wrap this with Managed C++ you would do this:
> > #include < windows.h >
> > #using < mscorlib.dll >
> > #using < System.dll >
> > #include < vcclr.h >

> > using namespace System;

> > class UnmanagedClass {
> > public:
> >        LPCWSTR GetPropertyA();
> >        void   MethodB( LPCWSTR );
> > };

> > public __gc class ManagedClass {
> > public:
> >        ManagedClass()
> >               : m_Impl( new UnmanagedClass ) {}
> >        ~ManagedClass() {
> >              delete m_Impl;
> >        }
> >        __property String*  get_PropertyA() {
> >              return new String( m_Impl->GetPropertyA());
> >        }
> >        void   MethodB( String* theString ) {
> >              const WCHAR __pin*  str = PtrToStringChars( theString );
> >              m_Impl->MethodB( str );
> >        }
> > private:
> >        UnmanagedClass*            m_Impl;
> > };

> > Thanks,
> > Ulzii-

> > --------------------

> > > Newsgroups: microsoft.public.dotnet.languages.vc
> > > Subject: managed C++ wrapper around unmanaged C++ classes: causing
> > StackOverflow exception
> > > Date: 12 Jun 2003 21:47:16 -0700
> > > Organization: http://www.*-*-*.com/
> > > Lines: 70

> > > NNTP-Posting-Host: 192.138.150.250
> > > Content-Type: text/plain; charset=ISO-8859-1
> > > Content-Transfer-Encoding: 8bit
> > > X-Trace: posting.google.com 1055479636 29344 127.0.0.1 (13 Jun 2003
> > 04:47:16 GMT)

> > > NNTP-Posting-Date: 13 Jun 2003 04:47:16 GMT
> > > Path:

cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!news-out.cwix.com!newsfeed.cwix.co
m!newsfeed.frii.net!newsfeed.frii.net!140.99.99.194.MISMATCH!newsfeed1.easyn
ews.com!easynews.com!easynews!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supern

- Show quoted text -

Quote:
> > ews.com!postnews1.google.com!not-for-mail
> > > Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:25008
> > > X-Tomcat-NG: microsoft.public.dotnet.languages.vc

> > > I have an unmanaged C++ class that I want to call/wrap from a managed
> > > C++ class.  However, when I use the new operator to create the
> > > unmanaged class from the managed C++ class I keep getting the
> > > following error message

> > > "An unhandled exception of type 'System.StackOverflowException'
> > > occurred in my.dll"

> > > I cannot find any recursive loops, the VS 2003 de{*filter*} just waits for
> > > 30 seconds or so after I hit F11 and then gives me the above dialog
> > > box.

> > > Question: Does anyone have a ZIP file of a complete example that
> > > includes an actual unmanaged C++ class source code with a managed C++
> > > class source code wrapper that I can use to test my environment?  How
> > > about the actual source code files and project file for the example
> > > from part-1 of the migration guide?

> > > Here is what my current code looks like ... this is being compiled
> > > into a C++ DLL that is being called by a managed C++ DLL that is being
> > > called by C#.

> > > ...

> > > #include "stdafx.h"

> > > #pragma unmanaged

> > > class CppClass
> > > {

> > > public:

> > > // constructor
> > > CppClass() {}

> > > // destructor
> > > ~CppClass() {}

> > > // methods
> > > void native_f() {}

> > > };

> > > #pragma managed

> > > #using <mscorlib.dll>
> > > using namespace System::Runtime::InteropServices;
> > > using namespace System;

> > > public __gc class m_MyClass
> > > {
> > > public:
> > > m_MyClass()
> > > {
> > > m_session = new CppClass(); -- this function call is causing the
> > > StackOverflow exception
> > > }

> > > ~m_MyClass()
> > > {
> > > delete m_session;
> > > }

> > > private:

> > > CppClass *m_session;

> > > };

> > --
> > Ulzii Luvsanbat, Microsoft Visual C++ Team
> > This posting is provided "AS IS" with no warranties, and confers no
> rights.
> > Use of included script samples are subject to the terms specified at
> > http://www.*-*-*.com/



Tue, 13 Dec 2005 12:15:39 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Managed C++ wrappers for unmanaged C++ classes

2. Referencing data from unmanaged code to managed code in C++ Wrapper class

3. Inherit unmanaged c++ classes from .Net platform (managed c++ or c#)

4. debugging unmanaged c++ from a managed c++ class library

5. Making managed wrapper for legacy unmanaged classes.

6. Help me!! wrap unmanaged class with managed c++

7. How to declare a managed class pointer from unmanaged c++

8. Managed C++ "wrapper classes" deployment problem

9. Expose managed classes from unmanaged C++?

10. Tutorial wanted for using a managed C++ class wrapper

11. Creating a managed C++ class wrapper

12. Performance of unmanaged C++ in a managed C++ app

 

 
Powered by phpBB® Forum Software