By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,509 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,509 IT Pros & Developers. It's quick & easy.

Calling Managed functions from unmanaged class

P: n/a
Hi All

I have a VC++ 2005 MFC application with all classes defined as
unmanaged classes. I want to write my application data in xml format.
Since ADO.NET has buit in functions available for this, I want to use
it. Is it possible to call Managed class functions from Unmanaged
class? How to do it?

I did something like this.
I declared a managed class (in C++ CLI) called as MyManagedClass whose
public interface has functions to write application data in XML format.
It internally uses ADO.NET classes to achieve this. This class is
declared in a seperate dll compiled with '\clr' option.

Now I created an unmanaged wrapper class which wraps the managed class
(defined in the same dll as that of managed class). The unmanaged
wrapper, call it as MyUnmanagedWrapper, has a data member declared as

gcroot<Object^handleToManagedClass;

This is declared in the top of the class. It has wrapper methods whose
implementation is something like this:

void MyUnmanagedWrapper::Method1()
{
(*this)->Method1();
}

and then I have...

class MyApplicationClass
{
MyUnmanagedWrapperInstance.Method1();
}

Everything complies well. However, when the above method is run, I get
Access Violation error. It does not even go inside the method.

Can anyone tell why there is access violation?

Best regards
Amit Dedhia

Oct 11 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Your question is off topic in comp.lang.c++

Oct 11 '06 #2

P: n/a

"Amit Dedhia" <am********@yahoo.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi All

I have a VC++ 2005 MFC application with all classes defined as
unmanaged classes. I want to write my application data in xml format.
Since ADO.NET has buit in functions available for this, I want to use
it. Is it possible to call Managed class functions from Unmanaged
class? How to do it?

I did something like this.
I declared a managed class (in C++ CLI) called as MyManagedClass whose
public interface has functions to write application data in XML format.
It internally uses ADO.NET classes to achieve this. This class is
declared in a seperate dll compiled with '\clr' option.

Now I created an unmanaged wrapper class which wraps the managed class
(defined in the same dll as that of managed class). The unmanaged
wrapper, call it as MyUnmanagedWrapper, has a data member declared as

gcroot<Object^handleToManagedClass;
Should be
gcroot<MyManagedClass^handleToManagedClass;
>
This is declared in the top of the class. It has wrapper methods whose
implementation is something like this:

void MyUnmanagedWrapper::Method1()
{
(*this)->Method1();
Should be
handleToManagedClass->Method1();
}

and then I have...

class MyApplicationClass
{
MyUnmanagedWrapperInstance.Method1();
}

Everything complies well. However, when the above method is run, I get
Access Violation error. It does not even go inside the method.

Can anyone tell why there is access violation?
Where is handleToManagedClass initialized? Initially it will hold a
nullptr. Testing a gcroot for nullptr is a pain, try:
(handleToManagedClass.operator->() != nullptr)
>
Best regards
Amit Dedhia

Oct 11 '06 #3

P: n/a

"Noah Roberts" <ro**********@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Your question is off topic in comp.lang.c++
I mostly prefer to call "unmanaged" native.
Unless, of course, you prefer to not manage your
code unless MS does it for you. <g>
Oct 11 '06 #4

P: n/a

Ben Voigt wrote:
"Amit Dedhia" <am********@yahoo.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi All

I have a VC++ 2005 MFC application with all classes defined as
unmanaged classes. I want to write my application data in xml format.
Since ADO.NET has buit in functions available for this, I want to use
it. Is it possible to call Managed class functions from Unmanaged
class? How to do it?

I did something like this.
I declared a managed class (in C++ CLI) called as MyManagedClass whose
public interface has functions to write application data in XML format.
It internally uses ADO.NET classes to achieve this. This class is
declared in a seperate dll compiled with '\clr' option.

Now I created an unmanaged wrapper class which wraps the managed class
(defined in the same dll as that of managed class). The unmanaged
wrapper, call it as MyUnmanagedWrapper, has a data member declared as

gcroot<Object^handleToManagedClass;

Should be
gcroot<MyManagedClass^handleToManagedClass;

This is declared in the top of the class. It has wrapper methods whose
implementation is something like this:

void MyUnmanagedWrapper::Method1()
{
(*this)->Method1();
Should be
handleToManagedClass->Method1();
}

and then I have...

class MyApplicationClass
{
MyUnmanagedWrapperInstance.Method1();
}

Everything complies well. However, when the above method is run, I get
Access Violation error. It does not even go inside the method.

Can anyone tell why there is access violation?
Where is handleToManagedClass initialized? Initially it will hold a
nullptr. Testing a gcroot for nullptr is a pain, try:
(handleToManagedClass.operator->() != nullptr)
Ok Ben...I got the point

Regarding initialization of the object, I would have a static method in
MyManagedClass which returns an instance of iteself. I will call this
method in ctor of the MyUnmanagedWrapper. However, I still have one
question. I have both these classes defined in the same managed dll
(compiled with /clr option). How do I use this dll (and its classes)
from say an MFC dialog based application client?

>

Best regards
Amit Dedhia
Oct 12 '06 #5

P: n/a
Amit Dedhia wrote:
I have a VC++ 2005 MFC application with all classes defined as
unmanaged classes. I want to write my application data in xml format.
Since ADO.NET has buit in functions available for this, I want to use
it. Is it possible to call Managed class functions from Unmanaged
class? How to do it?
Is XML writing the only reason for using ADO.NET? If yes,
then it seems like tremendous overkill. It's sufficient to
#import <msxml4.dlland use perfectly native C++ classes to
do whatever XML requires.
Oct 12 '06 #6

P: n/a
Hi Amit,

You can use a managed class from any unmanaged C++ files directly. Here are
the steps.

1. Mark the unmanaged CPP as using managed extensions. This can be done from
the properties of the unmanaged CPP file in VS.Net 2003 or VS.Net 2005 IDE.

2. Pu the following declarations in your unmanaged CPP file where you want
to use the managed .Net ADO.Net and XML classes. Or better combine the
following declarations into a header file and use it in your unmanaged CPP
file

#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::Text;
using namespace System::IO;
using namespace System::Data;
using namespace System::Data::SqlClient;
using namespace System::Xml;

3. If you want to wrap your ADO.Net and XMl managed code into an easy to use
class simply create a new CPP/header file in your MFC applicaiton and mark
the CPP as using managed extensions as described in step-1. Then use these
managed classes from your unmanaged code directly.

4. If you want to maintain the managed class as data member of the unmanaged
class then use "int" for the data member and store the GCHandle of the
managed class. This way your unmanaged header will still remain unmanaged and
the unmanaged CPP file can convert the GCHandle back and forth from int to
your managed class object.

This is called mixed mode programming in Managed C++ terms and is an
unmatched technique to use unamanged and manged code within the same CPP
file. This is only possible with Managed C++ (Managed C++ designers are
really great).

Let me know if you have any further queries in my explanation. I regularly
work with mixed mode classes in our product development.

--
Regards,
Aditya.P
"Amit Dedhia" wrote:
>
Ben Voigt wrote:
"Amit Dedhia" <am********@yahoo.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi All
>
I have a VC++ 2005 MFC application with all classes defined as
unmanaged classes. I want to write my application data in xml format.
Since ADO.NET has buit in functions available for this, I want to use
it. Is it possible to call Managed class functions from Unmanaged
class? How to do it?
>
I did something like this.
I declared a managed class (in C++ CLI) called as MyManagedClass whose
public interface has functions to write application data in XML format.
It internally uses ADO.NET classes to achieve this. This class is
declared in a seperate dll compiled with '\clr' option.
>
Now I created an unmanaged wrapper class which wraps the managed class
(defined in the same dll as that of managed class). The unmanaged
wrapper, call it as MyUnmanagedWrapper, has a data member declared as
>
gcroot<Object^handleToManagedClass;
Should be
gcroot<MyManagedClass^handleToManagedClass;
>
This is declared in the top of the class. It has wrapper methods whose
implementation is something like this:
>
void MyUnmanagedWrapper::Method1()
{
(*this)->Method1();
Should be
handleToManagedClass->Method1();
}
>
and then I have...
>
class MyApplicationClass
{
MyUnmanagedWrapperInstance.Method1();
}
>
Everything complies well. However, when the above method is run, I get
Access Violation error. It does not even go inside the method.
>
Can anyone tell why there is access violation?
Where is handleToManagedClass initialized? Initially it will hold a
nullptr. Testing a gcroot for nullptr is a pain, try:
(handleToManagedClass.operator->() != nullptr)

Ok Ben...I got the point

Regarding initialization of the object, I would have a static method in
MyManagedClass which returns an instance of iteself. I will call this
method in ctor of the MyUnmanagedWrapper. However, I still have one
question. I have both these classes defined in the same managed dll
(compiled with /clr option). How do I use this dll (and its classes)
from say an MFC dialog based application client?


>
Best regards
Amit Dedhia
>

Oct 24 '06 #7

P: n/a

"Amit Dedhia" <am********@yahoo.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi All

I have a VC++ 2005 MFC application with all classes defined as
unmanaged classes. I want to write my application data in xml format.
Since ADO.NET has buit in functions available for this, I want to use
it. Is it possible to call Managed class functions from Unmanaged
class? How to do it?

I did something like this.
I declared a managed class (in C++ CLI) called as MyManagedClass whose
public interface has functions to write application data in XML format.
It internally uses ADO.NET classes to achieve this. This class is
declared in a seperate dll compiled with '\clr' option.

Now I created an unmanaged wrapper class which wraps the managed class
(defined in the same dll as that of managed class). The unmanaged
wrapper, call it as MyUnmanagedWrapper, has a data member declared as

gcroot<Object^handleToManagedClass;
Should be gcroot<MyManagedClass^handleToManagedClass;
>
This is declared in the top of the class. It has wrapper methods whose
implementation is something like this:

void MyUnmanagedWrapper::Method1()
{
(*this)->Method1();
Should be handleToManagedClass->Method1();

Thought I saw this exact same question with the exact same broken code a
week ago.
}

and then I have...

class MyApplicationClass
{
MyUnmanagedWrapperInstance.Method1();
}

Everything complies well. However, when the above method is run, I get
Access Violation error. It does not even go inside the method.

Can anyone tell why there is access violation?
There is an access violation because in some code you have not shown us, you
are performing a bad cast and trying to use an object as a type it isn't.

Does your unmanaged class constructor set handleToManagedClass or leave it
as nullptr?
>
Best regards
Amit Dedhia

Oct 24 '06 #8

P: n/a
Adityanand Pasumarthi <Ad******************@discussions.microsoft.com>
wrote:
>
Hi Amit,

You can use a managed class from any unmanaged C++ files directly. Here are
the steps.
...
This is called mixed mode programming in Managed C++ terms and is an
unmatched technique to use unamanged and manged code within the same CPP
file. This is only possible with Managed C++ (Managed C++ designers are
really great).

Let me know if you have any further queries in my explanation. I regularly
work with mixed mode classes in our product development.
This was a fabulous explanation. My compliments to you. I've marked it as
"keep forever".
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 26 '06 #9

P: n/a

"Tim Roberts" <ti**@probo.comwrote in message
news:nt********************************@4ax.com...
Adityanand Pasumarthi <Ad******************@discussions.microsoft.com>
wrote:
>>
Hi Amit,

You can use a managed class from any unmanaged C++ files directly. Here
are
the steps.
...
This is called mixed mode programming in Managed C++ terms and is an
unmatched technique to use unamanged and manged code within the same CPP
file. This is only possible with Managed C++ (Managed C++ designers are
really great).

Let me know if you have any further queries in my explanation. I regularly
work with mixed mode classes in our product development.

This was a fabulous explanation. My compliments to you. I've marked it
as
"keep forever".
Only don't use "Managed C++" in VS2005, it's been replaced by C++/CLI.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.

Oct 26 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.