"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