Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
229 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 elease 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_tlbyour_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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...