473,729 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 MyUnmanagedWrap per, has a data member declared as

gcroot<Object^h andleToManagedC lass;

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

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

and then I have...

class MyApplicationCl ass
{
MyUnmanagedWrap perInstance.Met hod1();
}

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

Oct 11 '06 #2

"Amit Dedhia" <am********@yah oo.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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 MyUnmanagedWrap per, has a data member declared as

gcroot<Object^h andleToManagedC lass;
Should be
gcroot<MyManage dClass^handleTo ManagedClass;
>
This is declared in the top of the class. It has wrapper methods whose
implementation is something like this:

void MyUnmanagedWrap per::Method1()
{
(*this)->Method1();
Should be
handleToManaged Class->Method1();
}

and then I have...

class MyApplicationCl ass
{
MyUnmanagedWrap perInstance.Met hod1();
}

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 handleToManaged Class initialized? Initially it will hold a
nullptr. Testing a gcroot for nullptr is a pain, try:
(handleToManage dClass.operator->() != nullptr)
>
Best regards
Amit Dedhia

Oct 11 '06 #3

"Noah Roberts" <ro**********@g mail.comwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.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********@yah oo.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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 MyUnmanagedWrap per, has a data member declared as

gcroot<Object^h andleToManagedC lass;

Should be
gcroot<MyManage dClass^handleTo ManagedClass;

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

void MyUnmanagedWrap per::Method1()
{
(*this)->Method1();
Should be
handleToManaged Class->Method1();
}

and then I have...

class MyApplicationCl ass
{
MyUnmanagedWrap perInstance.Met hod1();
}

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 handleToManaged Class initialized? Initially it will hold a
nullptr. Testing a gcroot for nullptr is a pain, try:
(handleToManage dClass.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 MyUnmanagedWrap per. 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.dllan d 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.dl l>
#using <System.Xml.dll >

using namespace System;
using namespace System::Text;
using namespace System::IO;
using namespace System::Data;
using namespace System::Data::S qlClient;
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********@yah oo.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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 MyUnmanagedWrap per, has a data member declared as
>
gcroot<Object^h andleToManagedC lass;
Should be
gcroot<MyManage dClass^handleTo ManagedClass;
>
This is declared in the top of the class. It has wrapper methods whose
implementation is something like this:
>
void MyUnmanagedWrap per::Method1()
{
(*this)->Method1();
Should be
handleToManaged Class->Method1();
}
>
and then I have...
>
class MyApplicationCl ass
{
MyUnmanagedWrap perInstance.Met hod1();
}
>
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 handleToManaged Class initialized? Initially it will hold a
nullptr. Testing a gcroot for nullptr is a pain, try:
(handleToManage dClass.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 MyUnmanagedWrap per. 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********@yah oo.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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 MyUnmanagedWrap per, has a data member declared as

gcroot<Object^h andleToManagedC lass;
Should be gcroot<MyManage dClass^handleTo ManagedClass;
>
This is declared in the top of the class. It has wrapper methods whose
implementation is something like this:

void MyUnmanagedWrap per::Method1()
{
(*this)->Method1();
Should be handleToManaged Class->Method1();

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

and then I have...

class MyApplicationCl ass
{
MyUnmanagedWrap perInstance.Met hod1();
}

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 handleToManaged Class or leave it
as nullptr?
>
Best regards
Amit Dedhia

Oct 24 '06 #8
Adityanand Pasumarthi <Ad************ ******@discussi ons.microsoft.c om>
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.com wrote in message
news:nt******** *************** *********@4ax.c om...
Adityanand Pasumarthi <Ad************ ******@discussi ons.microsoft.c om>
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
741
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 managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
9
18043
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. The DLLs are compiled in MS Visual C++ Version 6 and the C# applications are written in MS VS C#.NET 2003. I am using the specifier _declspec(dllexport) for a class in the DLL and I am using DllImport(...) for each method imported into the C#...
1
9459
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 file but it cant call a managed C# dll. I need to create an unmanaged C++.net wrapper for a Managed C# dll. Does anyone know how to do this? can you please give me a simple example of what the C++.Net code would look like to do this Take for...
1
7158
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 Initialize function in the unmanaged library) crashes everytime I call it. Initialize calls another function 'GetInstance' (a static member function of a class) which looks something like: CLibConfig& CLibConfig::GetInstance()
1
2908
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 written in C#. When the app calls my unmanaged functions, they work fine. But as soon as my unmanaged functions call managed functions (in the same source file!), the app reports an "unknown exception" error.
1
2603
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, System::EventArgs * e) { MyDLLInit(MyAppDisplayFunction); }
6
3366
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 overrides the virutal one in the base. In my managed class, I have the following private: CUnmanaged __nogc* m_pUnManaged
5
3343
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 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
3
4711
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 application. Furthermore, I use a pointer to an unmanaged function to jump back into the managed dll. The managed part is basically a remoting enhancement which asynchronly
0
8928
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8766
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9428
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9217
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3244
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2171
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.