I am trying to use a WPF window from a legacy c++ unmanaged gtk gui application. Is it possible to package the WPF window (including the xaml file) and use it in the c++ gui application as a regular com object. Do you foresee any issues or problems with this approach?
If possible any links or tutorials or any suggestions on how to do it will be very helpful. Thanks.
I'm not aware of any tutorials online for doing this; but it shouldn't be a big problem at all. I've tried implementing smth like this and it worked fine for me, below is sequence if steps I've done:
1.add a "wpf user control" or "wpf custom control" library to your solution.
2.add a new WPF Window class (Add->Window->...) into the new project. Then add what ever wpf controls you like to your new window just to check if it works later
3.add new class and interface to the library project and define it like an example below:
[ComVisible(true)]
[Guid("694C1820-04B6-4988-928F-FD858B95C881")]
public interface ITestWPFInterface
{
[DispId(1)]
void TestWPF();
}
[ComVisible(true)]
[Guid("9E5E5FB2-219D-4ee7-AB27-E4DBED8E123F"),
ClassInterface(ClassInterfaceType.None)]
public class TestWPFInterface : ITestWPFInterface
{
public void TestWPF()
{
Window1 form = new Window1();
form.Show();
}
}
4.make your assembly com visible (Register For COM interop key in the Build tab of the project properties) and assign a strong name to it (see signing tab); generate key with sn utility
5.Once all above done you going to have a your_wpf_lib.tlb file generated in the debug\release folder
6.In your c++ application (I guess you have sources for it and can recompile), add following line:
import "C:\full_path_to_your_tlb\your_wpf_lib.tlb"
this should generate appropriate tlh file in the your win32 project debug output folder.
7.now you can call your form from the c++ code:
TestWPFForms::ITestWPFInterfacePtr comInterface(__uuidof(TestWPFForms::TestWPFInterface));
comInterface->TestWPF();
this should show your wpf form.
Also I believe links below might be useful for you:
Calling Managed .NET C# COM Objects from Unmanaged C++ Code
WPF and Win32 Interoperation Overview
hope this helps, regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With