Search code examples
c#c++wpfwinapicom-interop

Extending C++ Win32 application with a C# WPF component


I need to add a new component to a C++ Win32 application (no CLR support) and I would like to implement the new component in C# and use WPF. This new component is basically a window with some controls, which I need to launch quickly from an option in the menu of the Win32 application. The new component and the existing application share some int values and both can modify these values and the other should be notified of changes.

What would be the best way to achieve this?

Thanks


Solution

  • If you want to do this without changing your C++ compilation you might look at calling the .NET assembly as via COM. MSDN describes how to expose .NET Framework Components to COM.

    The way I've done this is basically:

    1. Make a COM friendly class and interface in C#
    2. Export the TLB from the DLL
    3. GAC the DLL
    4. Register the DLL using regasm.exe
    5. import the TLB into my C++ code

    6. CoCreateInstance using the __uuidof my C# class

    However I haven't tried to do this with a UI component. If the WPF form is a modal dialog you should be OK. If not then I don't know if WPF will be happy with your message loop; you may need to give it a seperate thread so it can run its own loop.