473,795 Members | 3,323 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a managed Dll from an unmanaged Win32 .EXE

Hello,

i plan to use a managed dll containing WinForm classes in an unmanaged
win32 exe.

Starting from NOTHING, how do i have to proceed ?

I have found the following tutorial on microsoft.com :

"Converting Managed Extensions for C++ Projects from Pure Intermediate
Language to Mixed Mode" :

http://msdn.microsoft.com/library/de...omixedmode.asp
I've tried to create an Win32 application + a managed c++ dll, and use
the dll in the exe, following step by step the tutorial.

All compiles and links, but....the application starts with an
uncontinuable exception 0x000005 (acces violation i presume).

What i am missing ? Anyone has a sample solution ?

I am using VS 2003 v 7.1.3088 with XP 2002 Professional.

Many Thanks !!

Nov 17 '05 #1
5 2807
"CharlesHen ri" <gd************ @yahoo.fr> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
i plan to use a managed dll containing WinForm classes in an unmanaged
win32 exe.

Starting from NOTHING, how do i have to proceed ?


I should tell you that while I am familiar with interop I don't _do_
Winforms. I seem to recall a presentation in which it was mentioned that
VS.Net 2005 was going to include features that would make allow for some
easier MFC and Winforms interoperabilit y. That's an option if you are
willing to use the beta in advance of the release.

Nevertheless, I can tell you that it is possible now to make a .Net class
appear to native code as a COM object. If you do that it is fairly easy to
call methods in managed classes from native code.

So, the first step is to give your class a dual interface:

using System;
using System.Runtime. InteropServices *;

namespace ClassLibrary1
{
[ClassInterface( ClassInterface* Type.AutoDual)]
public class Class1
{

public Class1()
{
}

public void SayHello()
{
Console.WriteLi ne("C# says hello!");
}

}
}

Then you can register the assembly and create a type library from it with
the
command

regasm /tlb:ClassLibrar y1Lib.tlb ClassLibrary1.d ll

That done, you can take advantage of the native compiler's ability to import
a type library and to create a C++ wrapper class from it:

#include <windows.h>
#import "mscorlib.t lb"

using namespace mscorlib;
#import "ClassLibrary1L ib.tlb"
using namespace ClassLibrary1;

int main()
{
CoInitialize(0) ;

_Class1Ptr class1(__uuidof (Class1));

class1->SayHello();

CoUninitialize( );
return 0;
}

Finally, you put the assembly in the same directory as the executable et
voila.

Regards,
Will
Nov 17 '05 #2
Thanks very much Will,

i was aware of this solution, but i think it is quite impossible to use
it in the context of the application i manage to migrate.

from what i found on other forums i undestood that there are 3 main
solutions :

1- make the native exe and the dll communicating via COM (your
solution)
2- re-compile the whole solution (.exe + .dll) in the "It just works"
fashion
(see http://www.codeproject.com/managedcpp/nishijw01.asp as example)
3- use mixed mode

i want to be able to use .NET and Winforms from a HUGE existing
application (let's say 1500 classes, millions of code lines) that use
MFC, corba calls...etc

1. is excluded, 2. seems very difficult, few chance to compile and
link...that's why i was thinking of using the third solution.

Anyone have already done such a migration ?

sorry for poor english ;-)

William DePalo [MVP VC++] a écrit :
"CharlesHen ri" <gd************ @yahoo.fr> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
i plan to use a managed dll containing WinForm classes in an unmanaged
win32 exe.

Starting from NOTHING, how do i have to proceed ?


I should tell you that while I am familiar with interop I don't _do_
Winforms. I seem to recall a presentation in which it was mentioned that
VS.Net 2005 was going to include features that would make allow for some
easier MFC and Winforms interoperabilit y. That's an option if you are
willing to use the beta in advance of the release.

Nevertheless, I can tell you that it is possible now to make a .Net class
appear to native code as a COM object. If you do that it is fairly easy to
call methods in managed classes from native code.

So, the first step is to give your class a dual interface:

using System;
using System.Runtime. InteropServices *;

namespace ClassLibrary1
{
[ClassInterface( ClassInterface* Type.AutoDual)]
public class Class1
{

public Class1()
{
}

public void SayHello()
{
Console.WriteLi ne("C# says hello!");
}

}
}

Then you can register the assembly and create a type library from it with
the
command

regasm /tlb:ClassLibrar y1Lib.tlb ClassLibrary1.d ll

That done, you can take advantage of the native compiler's ability to import
a type library and to create a C++ wrapper class from it:

#include <windows.h>
#import "mscorlib.t lb"

using namespace mscorlib;
#import "ClassLibrary1L ib.tlb"
using namespace ClassLibrary1;

int main()
{
CoInitialize(0) ;

_Class1Ptr class1(__uuidof (Class1));

class1->SayHello();

CoUninitialize( );
return 0;
}

Finally, you put the assembly in the same directory as the executable et
voila.

Regards,
Will


Nov 17 '05 #3
CharlesHenri wrote:
Hello,

i plan to use a managed dll containing WinForm classes in an unmanaged
win32 exe.

Starting from NOTHING, how do i have to proceed ?

I have found the following tutorial on microsoft.com :

"Converting Managed Extensions for C++ Projects from Pure Intermediate
Language to Mixed Mode" :

http://msdn.microsoft.com/library/de...omixedmode.asp
I've tried to create an Win32 application + a managed c++ dll, and use
the dll in the exe, following step by step the tutorial.

All compiles and links, but....the application starts with an
uncontinuable exception 0x000005 (acces violation i presume).

What i am missing ? Anyone has a sample solution ?

I am using VS 2003 v 7.1.3088 with XP 2002 Professional.

Many Thanks !!

Hi,

You will need to debug the startup of the application to see exactly
what is going wrong. Post concrete findings here (like at least the call
stack and any investigation you mage to do) when you crash.

Ronald Laeremans
Visual C++ team
Nov 17 '05 #4
Thanks very much Will,

i was aware of this solution, but i think it is quite impossible to use
it in the context of the application i manage to migrate.

from what i found on other forums i undestood that there are 3 main
solutions :

1- make the native exe and the dll communicating via COM (your
solution)
2- re-compile the whole solution (.exe + .dll) in the "It just works"
fashion
(see http://www.codeproject.com/managedcpp/nishijw01.asp as example)
3- use mixed mode

i want to be able to use .NET and Winforms from a HUGE existing
application (let's say 1500 classes, millions of code lines) that use
MFC, corba calls...etc

1. is excluded, 2. seems very difficult, few chance to compile and
link...that's why i was thinking of using the third solution.

Anyone have already done such a migration ?

sorry for poor english ;-)

William DePalo [MVP VC++] a écrit :
"CharlesHen ri" <gd************ @yahoo.fr> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
i plan to use a managed dll containing WinForm classes in an unmanaged
win32 exe.

Starting from NOTHING, how do i have to proceed ?


I should tell you that while I am familiar with interop I don't _do_
Winforms. I seem to recall a presentation in which it was mentioned that
VS.Net 2005 was going to include features that would make allow for some
easier MFC and Winforms interoperabilit y. That's an option if you are
willing to use the beta in advance of the release.

Nevertheless, I can tell you that it is possible now to make a .Net class
appear to native code as a COM object. If you do that it is fairly easy to
call methods in managed classes from native code.

So, the first step is to give your class a dual interface:

using System;
using System.Runtime. InteropServices *;

namespace ClassLibrary1
{
[ClassInterface( ClassInterface* Type.AutoDual)]
public class Class1
{

public Class1()
{
}

public void SayHello()
{
Console.WriteLi ne("C# says hello!");
}

}
}

Then you can register the assembly and create a type library from it with
the
command

regasm /tlb:ClassLibrar y1Lib.tlb ClassLibrary1.d ll

That done, you can take advantage of the native compiler's ability to import
a type library and to create a C++ wrapper class from it:

#include <windows.h>
#import "mscorlib.t lb"

using namespace mscorlib;
#import "ClassLibrary1L ib.tlb"
using namespace ClassLibrary1;

int main()
{
CoInitialize(0) ;

_Class1Ptr class1(__uuidof (Class1));

class1->SayHello();

CoUninitialize( );
return 0;
}

Finally, you put the assembly in the same directory as the executable et
voila.

Regards,
Will


Nov 17 '05 #5
CharlesHenri wrote:
Hello,

i plan to use a managed dll containing WinForm classes in an unmanaged
win32 exe.

Starting from NOTHING, how do i have to proceed ?

I have found the following tutorial on microsoft.com :

"Converting Managed Extensions for C++ Projects from Pure Intermediate
Language to Mixed Mode" :

http://msdn.microsoft.com/library/de...omixedmode.asp
I've tried to create an Win32 application + a managed c++ dll, and use
the dll in the exe, following step by step the tutorial.

All compiles and links, but....the application starts with an
uncontinuable exception 0x000005 (acces violation i presume).

What i am missing ? Anyone has a sample solution ?

I am using VS 2003 v 7.1.3088 with XP 2002 Professional.

Many Thanks !!

Hi,

You will need to debug the startup of the application to see exactly
what is going wrong. Post concrete findings here (like at least the call
stack and any investigation you mage to do) when you crash.

Ronald Laeremans
Visual C++ team
Nov 17 '05 #6

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

Similar topics

1
1336
by: Fender Mussel | last post by:
Hi, We are in a situation where we have a Win32 application which is currently deployed to about 15000 desktop machines and want to rewrite the back end (business logic) in a modern language. At the moment there is no change for changing the front end (GUI) because it's far to complex to rewrite it now. Thus, we are currently evaluating several alternatives for programming languages and one very favourite candidate is C#.
0
3908
by: Frank Lopez | last post by:
Does anyone know if Microsoft generated a whitepaper on this topic? Does anyone know what the solution is? (meaning, eliminate the leak problem -- I am seeing three memory leaks from dllmodul.cpp(102) similar to what is mentioned below)... I am calling MFC as part of unmanaged code used by the managed code. +--------
2
1068
by: Bob Beauchaine | last post by:
I found out the hard way that I can't include an STL container in a form object, and spent a half day trying to locate authoritative documentation on exactly why. So far I've concluded from indirect evidence (no thanks to the online help) that what I want to do cannot be done. I have experimented with using pointers to STL containers as members of managed classes, and on the surface this seems to work. So what are people with more .NET...
0
1380
by: Gustavo L. Fabro | last post by:
Greetings! I found myself with a LNK2020 linking problem using VS.NET 2003 under a situation that I was able to replicate in a small project. I found one workaround for the problem too, but it's ugly and sure things shouldn't be like that. I'd like to know if that's a conceptual mistake by me, a compiler error, both, or none of them! ;-) To replicate the problem:
1
567
by: CharlesHenri | last post by:
Hello, i plan to use a managed dll containing WinForm classes in an unmanaged win32 exe. Starting from NOTHING, how do i have to proceed ? I have found the following tutorial on microsoft.com : "Converting Managed Extensions for C++ Projects from Pure Intermediate
5
7059
by: Michael D. Reed | last post by:
I have legacy DLL code written in C++ that is essentially C code, an implementation of a set of formulas used in our industry. There are no DB or GUI interfaces, just exposed methods. I would like to convert it to C# and if that is not possible to manage C++. I want to use the DLL in a larger VB.Net program. Is there a tool that will convert C++ to C#? Are there any articles/papers/examples on converting C++ to C#? Are there any...
6
1461
by: nicolas.hilaire | last post by:
Hi all, i'm not totally clear with some concepts about managed and unmanaged code. I'm asking myself some questions : - i've a MFC app, i want to use the framework dotnet by switching to /clr compilation mode. Is my app in CLR, or only the call to the
2
2981
by: interX | last post by:
Hi, I have a little problem with managed/unmanaged in Visual Studio 2005 (Compiler setting /clr). I need to overhand several function pointers from managed to unmanaged. These function pointers are stored in an unmanaged struct and this struct is then overhanded by reference to the unmanaged code (See code pieces below). I thought the way to go is to create a delegate, pin it via GCHandel::Alloc and use the function
5
2262
by: Richy | last post by:
Hi, This is a Direct3D-related question but I am posting it in here as it is more VB-specific. I have an effect file that I am trying to set the values for. For example: fx.SetValue("matView", _viewMatrix) fx.SetValue("matViewProjection", _projectionMatrix)
9
3567
by: =?Utf-8?B?RWR3YXJkUw==?= | last post by:
I would greatly appreciate some help on passing managed object into unmanaged code. I need to pass a reference (address of) of a managed class into unmanaged code (written by a thrid party). The 3rd party unmanaged DLL will pass this reference into standard Win32 unmanaged static callback function in my code. Inside this unmanaged callback function I need to cast this unmnaged pointer that I have received from 3rd party back into the...
0
9519
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,...
1
10163
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
9037
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
7538
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
5436
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...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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.