473,659 Members | 3,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mixed mode dll, intellecutal exercise

I have a DLL that is "mixed mode" in that it is both an ATL com server and a
reference managed C++ class. For the most part it works, however the problem
I have (which may simply not be solvable) is that I want to call managed
code from the unmanaged (ATL classes). It is quite easy to go the other way
from managed to unmanaged, but is there any way I can expose the managed
class to the unmanaged caller.

I came into this while upgrading a ATL project, I do realize that it will be
"better" to simply recreate the class as a managed class and expose the COM
server using interop, but the question remains. Can you call from unmanaged
to managed in a mixed mode dll ?

-Robert
Dec 16 '05 #1
6 1813
"Robert Ginsburg" <ro************ *@ver3.com> wrote in message
news:O8******** *****@TK2MSFTNG P15.phx.gbl...
I have a DLL that is "mixed mode" in that it is both an ATL com server and
a reference managed C++ class. For the most part it works, however the
problem I have (which may simply not be solvable) is that I want to call
managed code from the unmanaged (ATL classes). It is quite easy to go the
other way from managed to unmanaged, but is there any way I can expose the
managed class to the unmanaged caller.


Yes, of course.

With Managed C++ (VS2003) or C++/CLI (VS2005) it is possible to include
modules that call on both the Win32 API and .Net's base class library.

One way to do what you want is to use what used to be called "it just works"
and which is now called "C++ interop". You compile the modules that contain
the managed functions with the /clr switch (which shows up as "use managed
extensions") The compiler does most of the grunt work.

Take a look at these articles to get started:

http://msdn.microsoft.com/library/de...nunmancode.asp

http://msdn.microsoft.com/msdnmag/is...5/VisualC2005/

If any of it is not clear post again with more detail.

Regards,
Will
Dec 16 '05 #2
Hopefully I am not being overly dense, but I cant seem to make it work. Here
is a specific example,
1) Create a new ATL project, make it a DLL, add an ATL class to it add a
method to it, it all works fine.
2) Now add CLR support and recompile it, still works fine
3) Now add a .NET class and a method, still works
4) Now how do you call the .net Method class from the COM component module ?

If I include the .NET class definition header, the compiler complains about
the format of it, and wont compile
If I dont include the header, the compiler complains that it is not defined
If I try to use the gcroot template, it does not know the name of the .NET
class.
Thanks in advance

-Robert
"William DePalo [MVP VC++ ]" <wi***********@ mvps.org> wrote in message
news:eg******** ******@TK2MSFTN GP09.phx.gbl...
"Robert Ginsburg" <ro************ *@ver3.com> wrote in message
news:O8******** *****@TK2MSFTNG P15.phx.gbl...
I have a DLL that is "mixed mode" in that it is both an ATL com server and
a reference managed C++ class. For the most part it works, however the
problem I have (which may simply not be solvable) is that I want to call
managed code from the unmanaged (ATL classes). It is quite easy to go the
other way from managed to unmanaged, but is there any way I can expose the
managed class to the unmanaged caller.


Yes, of course.

With Managed C++ (VS2003) or C++/CLI (VS2005) it is possible to include
modules that call on both the Win32 API and .Net's base class library.

One way to do what you want is to use what used to be called "it just
works" and which is now called "C++ interop". You compile the modules that
contain the managed functions with the /clr switch (which shows up as "use
managed extensions") The compiler does most of the grunt work.

Take a look at these articles to get started:

http://msdn.microsoft.com/library/de...nunmancode.asp

http://msdn.microsoft.com/msdnmag/is...5/VisualC2005/

If any of it is not clear post again with more detail.

Regards,
Will

Dec 16 '05 #3
"Robert Ginsburg" <ro************ *@ver3.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
If I include the .NET class definition header, the compiler complains
about the format of it, and wont compile
If I dont include the header, the compiler complains that it is not
defined
If I try to use the gcroot template, it does not know the name of the .NET
class.


There are some here who are good at answering questions in the abstract. I'm
not among them.

In the concrete, I'd need to know why the compiler "complainin g" about the
header? Is the module that includes it compiled with the /clr switch?

In the abstract, I'm not sure how much this will help, but it is my story
....

In an application of mine where there is an unmanaged function which needs
to create an instance of a managed class, I have a module which has both
managed and unmanaged sections. I delimit the sections with

#pragma managed

...

#pragma unmanaged

In the unmanaged function I call a managed helper function which creates an
instance of the managed class. I use the Alloc member function of the
GCHandle class to get a handle to the object. I convert that to an integer
with

GCHandle::op_Ex plicit( /* handle goes here */).ToInt32();

It is that integer which I hold on the native side of my application to
refer to the managed object. Of course, it has no meaning except in managed
code. There I use

GCHandle::op_ex plicit( /* integer goes here */) ;

to convert the integer to a GC handle. That handle can be used to get an
object with the get_target() function of the GCHandle class.

Note that I am not claiming this is a "best practice". It was simply the
most expedient way to add support for .Net plugins to an application of mine
two years ago. You may find it easier and more elegant to use the gcroot
template.

Regards,
Will
Dec 16 '05 #4
Will,
If my managed header includes a #using <mscorlib.dll >, then the compiler
breaks on the unmanaged class with a
Error 1 fatal error C1190: managed targeted code requires a '/clr' option
c:\my code\ijwtest\ij wtest\managedka boom.h 2

If I dont have that, of course it cant understand the module syntax and
reports the following
When I include the managed header in the unmanaged module the compiler
reports the following errors
Error 2 error C2871: 'System' : a namespace with this name does not exist
c:\my code\ijwtest\ij wtest\ManagedKa boom.h 4
Error 3 error C2059: syntax error : 'public' c:\my
code\ijwtest\ij wtest\ManagedKa boom.h 11
Error 4 error C2143: syntax error : missing ';' before '{' c:\my
code\ijwtest\ij wtest\ManagedKa boom.h 12
Error 5 error C2447: '{' : missing function header (old-style formal list?)
c:\my code\ijwtest\ij wtest\ManagedKa boom.h 12

ManagedKaboom.H
#pragma once
//#using <mscorlib.dll >
using namespace System;
namespace IJWTest {

public ref class ManagedKaboom
{
public:
ManagedKaboom(v oid)
{
}
~ManagedKaboom( )
{
}
String^ HelloWorld();
};
}

the module is marked with /clr , here it is
#pragma managed
#include "ManagedKaboom. h"
String^ IJWTest::Manage dKaboom::HelloW orld()
{
return "Managed Howdy";
}

So using your technique what would a simple module look like that called
this from unmanaged code (my current prototype is below)
STDMETHOD(Hello World)(BSTR* pVal);

Thanks Again
robert

#"William DePalo [MVP VC++ ]" <wi***********@ mvps.org> wrote in message
news:eV******** ******@TK2MSFTN GP10.phx.gbl...
"Robert Ginsburg" <ro************ *@ver3.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
If I include the .NET class definition header, the compiler complains
about the format of it, and wont compile
If I dont include the header, the compiler complains that it is not
defined
If I try to use the gcroot template, it does not know the name of the
.NET class.


There are some here who are good at answering questions in the abstract.
I'm not among them.

In the concrete, I'd need to know why the compiler "complainin g" about the
header? Is the module that includes it compiled with the /clr switch?

In the abstract, I'm not sure how much this will help, but it is my story
...

In an application of mine where there is an unmanaged function which needs
to create an instance of a managed class, I have a module which has both
managed and unmanaged sections. I delimit the sections with

#pragma managed

...

#pragma unmanaged

In the unmanaged function I call a managed helper function which creates
an instance of the managed class. I use the Alloc member function of the
GCHandle class to get a handle to the object. I convert that to an integer
with

GCHandle::op_Ex plicit( /* handle goes here */).ToInt32();

It is that integer which I hold on the native side of my application to
refer to the managed object. Of course, it has no meaning except in
managed code. There I use

GCHandle::op_ex plicit( /* integer goes here */) ;

to convert the integer to a GC handle. That handle can be used to get an
object with the get_target() function of the GCHandle class.

Note that I am not claiming this is a "best practice". It was simply the
most expedient way to add support for .Net plugins to an application of
mine two years ago. You may find it easier and more elegant to use the
gcroot template.

Regards,
Will

Dec 16 '05 #5
So while just poking aroud I added this to the header of my unmanaged class:
#include "stdafx.h"
#include "Kaboom.h"

#using < mscorlib.dll >
#include < vcclr.h >
#include "ManagedKaboom. h"
#pragma unmanaged

and this in the actual function call
STDMETHODIMP CKaboom::HelloW orld(BSTR* pVal)
{
gcroot<IJWTest: :ManagedKaboom^ > mKaboom = gcnew IJWTest::Manage dKaboom();
return S_OK;
}
And the compiler says

Error 1 error C3821: 'IJWTest::Manag edKaboom': managed type or function
cannot be used in an unmanaged function c:\My
Code\IJWTest\IJ WTest\Kaboom.cp p 16

-Robert

"William DePalo [MVP VC++ ]" <wi***********@ mvps.org> wrote in message
news:eV******** ******@TK2MSFTN GP10.phx.gbl...
"Robert Ginsburg" <ro************ *@ver3.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
If I include the .NET class definition header, the compiler complains
about the format of it, and wont compile
If I dont include the header, the compiler complains that it is not
defined
If I try to use the gcroot template, it does not know the name of the
.NET class.


There are some here who are good at answering questions in the abstract.
I'm not among them.

In the concrete, I'd need to know why the compiler "complainin g" about the
header? Is the module that includes it compiled with the /clr switch?

In the abstract, I'm not sure how much this will help, but it is my story
...

In an application of mine where there is an unmanaged function which needs
to create an instance of a managed class, I have a module which has both
managed and unmanaged sections. I delimit the sections with

#pragma managed

...

#pragma unmanaged

In the unmanaged function I call a managed helper function which creates
an instance of the managed class. I use the Alloc member function of the
GCHandle class to get a handle to the object. I convert that to an integer
with

GCHandle::op_Ex plicit( /* handle goes here */).ToInt32();

It is that integer which I hold on the native side of my application to
refer to the managed object. Of course, it has no meaning except in
managed code. There I use

GCHandle::op_ex plicit( /* integer goes here */) ;

to convert the integer to a GC handle. That handle can be used to get an
object with the get_target() function of the GCHandle class.

Note that I am not claiming this is a "best practice". It was simply the
most expedient way to add support for .Net plugins to an application of
mine two years ago. You may find it easier and more elegant to use the
gcroot template.

Regards,
Will

Dec 16 '05 #6
"Robert Ginsburg" <ro************ *@ver3.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
If my managed header includes a #using <mscorlib.dll >, then the compiler
breaks on the unmanaged class with a
Error 1 fatal error C1190: managed targeted code requires a '/clr' option
c:\my code\ijwtest\ij wtest\managedka boom.h 2
Right. As they might say in Brooklyn - C++ doesn't know from pound sign
using. It is a Microsoft extension.

You need to tell the compiler that it is not ISO standard C++ that you
target. From the IDE's menu choose Project->Properties. In the left pane
"open" the "Configurat ion" folder.Select "General". In the right pane find
the box labelled "Use managed extensions" and set its value to "Yes".
Thanks Again


You are welcome.

Regards,
Will
Dec 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2193
by: Mike Kamzyuk | last post by:
Hello all. Basically, I need to call a mixed-mode dll's function (which uses managed code) from a native or mixed-mode dll function (which does not use managed code). I'm wondering if this could be accomplished and how. Here's the problem. We have a third party app (TPA) capable of loading native and mixed-mode dlls somehow (we don't know how). It loads our native dll (OND) and allows us to use our code inside the app (that is, we...
5
2944
by: Adam McKee | last post by:
We are using Visual Studio.NET 2003 in our project with .NET framework 1.1. One of our libraries is a mixed-mode dll assembly consisting of one managed C++ library, and several unmanaged C++ libraries. We are using managed C++ as a bridge between managed .NET code and unmanaged C++ code, which I'm sure is a fairly common practice. The managed C++ library is compiled with /CLR whereas all other libraries are compiled without /CLR because...
0
1671
by: Edward Diener | last post by:
I have some questions about the instructions for creating a mixed mode DLL in the MSDN topic "Converting Managed Extensions for C++ Projects from Pure Intermediate Language to Mixed Mode" in the "Managed Extensions for C++ Reference". 1) The first instruction in converting to mixed mode is to link with /NOENTRY. This occurs despite the fact that a pure mode DLL is already set up with this option in the linker, and the previous...
9
2586
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and explain it. I have some questions about the instructions for creating a mixed mode DLL in the MSDN topic "Converting Managed Extensions for C++ Projects from Pure Intermediate Language to Mixed Mode" in the "Managed Extensions for C++ Reference"....
8
3501
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I cannot load/unload/reload extensions into my large and slow-to-load application during development without restarting the process then the disadvantages may outweigh the advantages. I've got a mixed-mode program in which I create a new AppDomain...
8
1992
by: Nadav | last post by:
Hi, I am writing a performence critical application, this require me to stick to unmanaged C++ as performance is much better using unmanaged C++ ( about 33% better ), Still, I am trying to avoid the usage of old style COM, my alternative is to expose my unmanaged interface through the CLI, to achieve that I have created a mixed mode DLL in which my unmanaged class are defined. When referencing the DLL just described in another mixed mode EXE...
2
1637
by: Doug Belkofer | last post by:
We have created a fairly complex mixed-mode DLL that we want to use from VB.NET. The mixed-mode DLL is written in C++, and does use the standard C runtime libraries. An unusual thing is happening in that when we look at this DLL from the Object Browser in Visual Studio, it seems to be exporting several items related to the standard C runtime libraries. One example is that there is a namespace called "std" in the Object Browser, and in that...
8
2309
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR types. The problem: I have a number of mixed-mode functions which I want reuse. These functions revolve around converting a CLR String to a C++ std::string or
0
2996
by: emu | last post by:
Hi All, I have an unmanaged C++ application that references a mixed mode image DLL (mixed managed and unmanaged). Under .NET 1.1 we could trust the dll (the mixed mode dll) by running the following command line: caspol.exe -polchgprompt off -machine -addgroup 1 -url "file://<UNC path to dll>\mixedMode.dll" FullTrust ame "GroupName" -polchgprompt on
0
8428
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
8851
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...
0
8747
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
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
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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
2752
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
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.