HwndSource.FromHwnd() return null

pol2095 1 Reputation point
2021-02-26T07:51:46.007+00:00

Hello,

why HwndSource.FromHwnd() return null ?

...
Hwnd handle = Process.GetCurrentProcess().MainWindowHandle;
...
parameters.ParentWindow = handle;
HwndSource src = new HwndSource(parameters);
...
src.RootVisual = view;
...
HwndSource src2 = HwndSource.FromHwnd(handle); // return null

Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,516 Reputation points
    2021-02-26T19:13:41.96+00:00

    Okay, if the form is a xaml file then it is WPF.

    What is Hwnd? Process.GetCurrentProcess().MainWindowHandle is an IntPtr but your code does not show the definition of Hwnd. That is probably not important to the problem but I am not sure.

    What is parameters? What type is it? a HwndSource can be created using a System.Windows.Interop.HwndSourceParameters but they do not have a ParentWindow member.

    What is view? I see no declaration of it.

    Note that the code you show here does not use src. So most of the code is not relevant.

    Do you know what the value of handle is?

    Also, you do not tell us where the code is at in the application. If the code executes before the main window is created then of course there is no handle.

    0 comments No comments

  2. pol2095 1 Reputation point
    2021-02-28T19:47:47.607+00:00

    Thanks, I finnaly created an Array of my HwndSource.

    0 comments No comments