473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Calling Managed functions from unmanaged class

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
9 3086
Your question is off topic in comp.lang.c++

Oct 11 '06 #2

"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

"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

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
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
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

"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
9
by: Andrew Cumming | last post by:
I am having trouble using Platform Invoke to call C++ DLLs from C# windows applications. The DLLs seem to load, but I get run-time errors to the effect that the named entry points cannot be found....
1
by: Zapbbx | last post by:
I have a 3rd party application that can reference external dll's. The dll's have to be written in unmanaged code with an exported function I can reference and call. I would like it to call a C# dll...
1
by: Adam Clauss | last post by:
I have an unmanaged C++ library that I need to use through C#. I created a Managed C++ class library and through it wrote a wrapper class to call what I need. However, one of the methods (an...
1
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
6
by: guxu | last post by:
I have a managed C++ code which calls some methods in unmanaged C++ DLL. In my unmanaged DLL, I have a PROTECTED virutal function in a base class and derived class has a PRIVATE function which...
5
by: Amit Dedhia | last post by:
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...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.