Good day! There is a task to find a window in WINDOWS, and attach this window to the UI (WPF). I use already working library which worked under WIN7. Now I write under WIN10 and get an error.

There is a code:

[DllImport("user32.dll", SetLastError = true)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetParent(IntPtr hwndChild, IntPtr hwndNewParent); [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags uFlags); [DllImport("user32.dll")] public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool MoveWindow(IntPtr hwnd, int x, int y, int width, int height, bool repaint); m_hwnd = FindWindow(null, ExternalWindowName); if (m_hwnd == (IntPtr)0) return; IntPtr prn = SetParent(m_hwnd, this.Handle); //prn == 0 // GetLastError() возырвщает код ошибки 5.. 

What can be wrong?

Thank..

  • if you mean calling SetParent for a window from another process, this is not a supported mode that is usually not recommended. especially in combination with WPF, it is generally a perversion. in fact, earlier in the documentation for the SetParent function, there was a remark that it was not recommended to do so, but then for some reason it was removed. in Windows 10 most likely tightened restrictions on access to other people's windows, and therefore stopped working. if the application is run with elevated privileges, also does not work? - MSDN.WhiteKnight

0