Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

96 views
Skip to first unread message

Frank Lopez

unread,
Jun 13, 2003, 12:47:16 AM6/13/03
to
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 debugger 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 [VCPP MSFT]

unread,
Jun 20, 2003, 2:20:45 PM6/20/03
to
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-

--------------------
> From: franklo...@yahoo.com (Frank Lopez)
> 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://groups.google.com/
> Lines: 70
> Message-ID: <b68e0841.03061...@posting.google.com>
> 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)
> X-Complaints-To: groups...@google.com
> 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
> Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:25008
> X-Tomcat-NG: microsoft.public.dotnet.languages.vc


--
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.microsoft.com/info/cpyright.htm

Jeff McKelvey

unread,
Jun 26, 2003, 2:18:26 AM6/26/03
to
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

"Ulzii Luvsanbat [VCPP MSFT]" <ba...@online.microsoft.com> wrote in message
news:a9ZRti1N...@cpmsftngxa06.phx.gbl...

Jeff McKelvey

unread,
Jun 27, 2003, 12:15:39 AM6/27/03
to
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

"Jeff McKelvey" <je...@mckelveyweb.com> wrote in message
news:u3YkIr6O...@TK2MSFTNGP11.phx.gbl...

0 new messages