Skip to content

Instantly share code, notes, and snippets.

View smourier's full-sized avatar

Simon Mourier smourier

View GitHub Profile
@smourier
smourier / mallocspy.cs
Created April 10, 2024 12:55
C# implementation of IMallocSpy
public class ConsoleMallocSpy : MallocSpy
{
protected override void Log(object message, [CallerMemberName] string methodName = null) => Console.WriteLine(methodName + ": " + message);
private readonly ConcurrentDictionary<IntPtr, IntPtr> _blocks = new();
private IntPtr _allocRequest;
protected override IntPtr PreAlloc(IntPtr cbRequest)
{
@smourier
smourier / JobObject.cs
Created February 26, 2024 16:13
JobObject C# class with wait, support for ShellExecute, etc.
public class JobObject : IDisposable
{
private IntPtr _handle;
public JobObject(string name = null, bool terminateOnDispose = false)
{
Name = name;
TerminateOnDispose = terminateOnDispose;
_handle = CreateJobObject(IntPtr.Zero, name);
if (_handle == IntPtr.Zero)
@smourier
smourier / LoadNonloadedOverlayIdentifiers.cs
Last active February 3, 2024 10:40
Load Nonloaded Overlay Identifiers
[STAThread]
static void Main()
{
var mgr = (IShellIconOverlayManager)new CLSID_CFSIconOverlayManager();
Console.WriteLine(mgr.LoadNonloadedOverlayIdentifiers());
}
[ComImport, Guid("63B51F81-C868-11D0-999C-00C04FD655E1")]
public class CLSID_CFSIconOverlayManager { };
@smourier
smourier / SvgToPng
Created February 3, 2024 10:08
Converting SVG to PNG using WIC & Direct2D (through Wicnet & DirectN)
using System.Drawing;
using System.IO;
using DirectN;
using WicNet;
namespace ConsoleApp2
{
internal class Program
{
static void Main()
@smourier
smourier / ShellIconOverlayIdentifiers.cs
Created February 3, 2024 08:40
Dump the list of Shell Icon Overlay Identifiers
static void Main()
{
using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers", false))
{
foreach (var name in key.GetSubKeyNames())
{
Console.WriteLine("'" + name + "'");
using (var sub = key.OpenSubKey(name, false))
{
if (!(sub.GetValue(null) is string def) || !Guid.TryParse(def, out var clsid))
public static void RunAsDesktopUser(string fileName)
{
if (string.IsNullOrWhiteSpace(fileName))
throw new ArgumentException("Value cannot be null or whitespace.", nameof(fileName));
// To start process as shell user you will need to carry out these steps:
// 1. Enable the SeIncreaseQuotaPrivilege in your current token
// 2. Get an HWND representing the desktop shell (GetShellWindow)
// 3. Get the Process ID(PID) of the process associated with that window(GetWindowThreadProcessId)
// 4. Open that process(OpenProcess)
@smourier
smourier / ImmersiveSettingsCache.cs
Created January 5, 2024 15:46
Calling ImmersiveSettingsCache from C#
static void Main(string[] args)
{
var cache = (IImmersiveSettingsCache)new ImmersiveSettingsCache();
var hr = cache.OnSettingChanged(0);
}
[ComImport, Guid("a919ea73-490e-4d5c-9ba7-97cbc73119fe")] // CLSID_ImmersiveSettingsCache
public class ImmersiveSettingsCache
{
// don't put anything in there
@smourier
smourier / wicbitmaptests.cpp
Created December 22, 2023 06:48
Some WIC Bitmap tests
#include <windows.h>
#include <atlbase.h>
#include <stdio.h>
#include <wincodec.h>
#define HRASSERT(x) ATLASSERT(SUCCEEDED(x))
HRESULT ConvertBitmapTo256(PCWSTR input, PCWSTR output, REFGUID outputFormat)
{
HRESULT hr = S_OK;
@smourier
smourier / App.xaml.cs
Last active April 18, 2024 17:46
Create a WinUI3 Xaml window in another thread
using Microsoft.UI.Xaml;
namespace WinUIAppFx
{
public partial class App : Application
{
private Window m_window;
public App()
{
@smourier
smourier / UIA_NoFocusSetNotepadText.cs
Created August 26, 2023 09:42
Set notepad's text using UI automation w/o setting the focus on it first
static void Main()
{
// needs using UIAutomationClient;
// requires a reference to windows\system32\UIAutomationClient.dll
// and set Embed Interop Types to false for project reference
var root = new CUIAutomation8();
// we want to *keep* the focus
root.AutoSetFocus = 0; // false