Remove titlebar from window. Window process started by different user


I am using this code (on windows 2003) to remove and resize window:

Process process = Process.GetProcessById(12121);

IntPtr mwh = process.MainWindowHandle;
SetWindowLong(mwh, GWL_STYLE, WS_VISIBLE);
ShowWindowAsync(mwh, 3);
SetWindowPos(mwh, new IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);

And declarations:

private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

static readonly int GWL_STYLE = -16;
static readonly int SWP_NOMOVE = 0x2;
static readonly int SWP_NOSIZE = 0x1;
static readonly int SWP_FRAMECHANGED = 0x20;
static readonly int WS_VISIBLE = 0x10000000;

Everything works correct, when I am resizing window associated with process started by me. But when I want to do this with other users windows, then it does nothing. How to make it works for other users windows?

asked on Stack Overflow Apr 1, 2016 by resterek • edited Jan 8, 2020 by James Hill

2 Answers


The behavior is by design in Windows Vista and later, due to the User Interface Privilege Isolation (UIPI). You could solve it if you have access to the source code of the controlled applications.

Read this answer for more:

answered on Stack Overflow Apr 1, 2016 by Francesco Martire • edited May 23, 2017 by Community

process.MainWindowHandle is a .NET concept, there is no such thing as a main window in native desktop apps and therefore might not always work correctly on other processes. You should check if mwh is IntPtr.Zero, if it is then you need to use EnumWindows + GetWindowThreadProcessId + IsWindowVisible to find the applications window.

Calling SetWindowLong(mwh, GWL_STYLE, WS_VISIBLE); on a window you did not create is not OK. You need to call GetWindowLong first the get the existing style, remove all the styles you don't want and add WS_POPUP. You might want to remove some of the extended styles as well.

answered on Stack Overflow Feb 2, 2017 by Anders • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0