Change the color of the title bar (caption) of a win32 application

You can change window title bar color / behaviour with DWMAPI’s DwmSetWindowAttribute function in Win32. NOTE: Might require Windows SDK 10.0.22000.0 (aka first Windows 11 SDK) as DWMWA_USE_IMMERSIVE_DARK_MODE|DWMWA_BORDER_COLOR|DWMWA_CAPTION_COLOR were not documented in Windows SDK 10.0.19041.0 (latest Windows 10 SDK). People have got DWMWA_USE_IMMERSIVE_DARK_MODE working before the public documentation of the variable by simply using dwAttribute … Read more

How can I invalidate the file system cache?

At least on Windows 7, it seems that attempting to open a volume handle without FILE_SHARE_WRITE sharing permissions causes the file system cache to be invalidated, even if the creation fails. Thus I made a program that simply calls CreateFile to this end. Download the program* from its Base64 version here: <!– Click “Run Snippet”, … Read more

How to check if the program is run from a console?

You can use GetConsoleWindow, GetWindowThreadProcessId and GetCurrentProcessId methods. First you must retrieve the current handle of the console window using the GetConsoleWindow function. Then you get the process owner of the handle of the console window. Finally you compare the returned PID against the PID of your application. Check this sample (VS C++) #include “stdafx.h” … Read more

How to send text to Notepad in C#/Win32?

[DllImport(“user32.dll”, EntryPoint = “FindWindowEx”)] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport(“User32.dll”)] public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam); private void button1_Click(object sender, EventArgs e) { Process [] notepads=Process.GetProcessesByName(“notepad”); if(notepads.Length==0)return; if (notepads[0] != null) { IntPtr child= FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), “Edit”, null); SendMessage(child, 0x000C, 0, … Read more

Marshal.AllocHGlobal VS Marshal.AllocCoTaskMem, Marshal.SizeOf VS sizeof()

A Windows program always has at least two heaps in which unmanaged memory is allocated. First is the default process heap, used by Windows when it needs to allocate memory on behalf of the program. The second is a heap used by the COM infrastructure to allocate. The .NET P/Invoke marshaller assumes this heap was … Read more

Removing window border?

In C/C++ LONG lStyle = GetWindowLong(hwnd, GWL_STYLE); lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU); SetWindowLong(hwnd, GWL_STYLE, lStyle); WS_CAPTION is defined as (WS_BORDER | WS_DLGFRAME). You can get away with removing just these two styles, since the minimize maximize and sytem menu will disappear when the caption disappears, but it’s best to … Read more