473,406 Members | 2,451 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,406 software developers and data experts.

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 1793
"Robert Ginsburg" <ro*************@ver3.com> wrote in message
news:O8*************@TK2MSFTNGP15.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**************@TK2MSFTNGP09.phx.gbl...
"Robert Ginsburg" <ro*************@ver3.com> wrote in message
news:O8*************@TK2MSFTNGP15.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****************@TK2MSFTNGP11.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 "complaining" 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_Explicit( /* 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_explicit( /* 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\ijwtest\managedkaboom.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\ijwtest\ManagedKaboom.h 4
Error 3 error C2059: syntax error : 'public' c:\my
code\ijwtest\ijwtest\ManagedKaboom.h 11
Error 4 error C2143: syntax error : missing ';' before '{' c:\my
code\ijwtest\ijwtest\ManagedKaboom.h 12
Error 5 error C2447: '{' : missing function header (old-style formal list?)
c:\my code\ijwtest\ijwtest\ManagedKaboom.h 12

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

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

the module is marked with /clr , here it is
#pragma managed
#include "ManagedKaboom.h"
String^ IJWTest::ManagedKaboom::HelloWorld()
{
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(HelloWorld)(BSTR* pVal);

Thanks Again
robert

#"William DePalo [MVP VC++ ]" <wi***********@mvps.org> wrote in message
news:eV**************@TK2MSFTNGP10.phx.gbl...
"Robert Ginsburg" <ro*************@ver3.com> wrote in message
news:%2****************@TK2MSFTNGP11.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 "complaining" 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_Explicit( /* 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_explicit( /* 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::HelloWorld(BSTR* pVal)
{
gcroot<IJWTest::ManagedKaboom^> mKaboom = gcnew IJWTest::ManagedKaboom();
return S_OK;
}
And the compiler says

Error 1 error C3821: 'IJWTest::ManagedKaboom': managed type or function
cannot be used in an unmanaged function c:\My
Code\IJWTest\IJWTest\Kaboom.cpp 16

-Robert

"William DePalo [MVP VC++ ]" <wi***********@mvps.org> wrote in message
news:eV**************@TK2MSFTNGP10.phx.gbl...
"Robert Ginsburg" <ro*************@ver3.com> wrote in message
news:%2****************@TK2MSFTNGP11.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 "complaining" 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_Explicit( /* 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_explicit( /* 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****************@TK2MSFTNGP15.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\ijwtest\managedkaboom.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 "Configuration" 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
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...
5
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++...
0
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...
9
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...
8
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...
8
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...
2
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...
8
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...
0
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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,...
0
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...

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.