Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2005

Tip: Looking for answers? Try searching our database.

Using a managed Dll from an unmanaged Win32 .EXE

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
CharlesHenri - 27 Jun 2005 15:43 GMT
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/default.asp?url=/library/en-us/vcmex/html/vcco
nconvertingmanagedextensionsforcprojectsfrompureintermediatelanguagetomixedmode.
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 !!
William DePalo [MVP VC++] - 27 Jun 2005 17:31 GMT
> 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
CharlesHenri - 28 Jun 2005 21:16 GMT
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 :
> > i plan to use a managed dll containing WinForm classes in an unmanaged
> > win32 exe.
[quoted text clipped - 67 lines]
> Regards,
> Will
Ronald Laeremans [MSFT] - 28 Jun 2005 23:33 GMT
> Hello,
>
[quoted text clipped - 21 lines]
>
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.