472,364 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 2693
"CharlesHenri" <gd************@yahoo.fr> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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 interoperability. 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.WriteLine("C# says hello!");
}

}
}

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

regasm /tlb:ClassLibrary1Lib.tlb ClassLibrary1.dll

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

using namespace mscorlib;
#import "ClassLibrary1Lib.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 :
"CharlesHenri" <gd************@yahoo.fr> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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 interoperability. 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.WriteLine("C# says hello!");
}

}
}

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

regasm /tlb:ClassLibrary1Lib.tlb ClassLibrary1.dll

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

using namespace mscorlib;
#import "ClassLibrary1Lib.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 :
"CharlesHenri" <gd************@yahoo.fr> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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 interoperability. 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.WriteLine("C# says hello!");
}

}
}

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

regasm /tlb:ClassLibrary1Lib.tlb ClassLibrary1.dll

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

using namespace mscorlib;
#import "ClassLibrary1Lib.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
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...
0
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...
2
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...
0
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...
1
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...
5
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...
6
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...
2
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...
5
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: ...
9
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...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.