Skip to content

Instantly share code, notes, and snippets.

View michel-pi's full-sized avatar

Michel michel-pi

View GitHub Profile
@michel-pi
michel-pi / LazyType.cs
Created September 30, 2018 22:47
How Lazy<T> should have been implemented
using System;
using System.Threading;
namespace System
{
public class LazyType<T>
{
private static readonly Func<T> DefaultValueFactory = () => default(T);
private readonly object _lock;
@michel-pi
michel-pi / VirtualKeyCode.cs
Last active August 11, 2021 22:35
VirtualKeyCode enum with a c# friendly naming convention
using System;
namespace System.Input
{
/// <summary>
/// An enumeration of all VirtualKeyCodes on Windows
/// </summary>
public enum VirtualKeyCode : int
{
/// <summary>
@michel-pi
michel-pi / KeyboardState.cs
Created September 30, 2018 23:21
Retreives a buffer containing all keys state
using System;
using System.Runtime.InteropServices;
namespace System.Input
{
public class KeyboardState
{
[DllImport("user32.dll")]
private static extern int GetKeyboardState(byte[] buffer);
@michel-pi
michel-pi / DynamicImport.cs
Last active December 23, 2018 08:42
Import methods from .dll's at runtime
using System;
using System.ComponentModel;
using System.Runtime.Serialization;
namespace System.Runtime.InteropServices
{
public static class DynamicImport
{
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress", SetLastError = true, CharSet = CharSet.Ansi)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procname);
@michel-pi
michel-pi / IntPtrExtensions.cs
Last active December 23, 2018 08:50
Extension methods to validate IntPtr's and count with them
using System;
namespace System
{
public static class IntPtrExtensions
{
private static readonly bool _x86 = IntPtr.Size == 4;
public static ulong GetValue(this IntPtr ptr)
{
@michel-pi
michel-pi / AsyncKeyState.cs
Last active December 23, 2018 08:48
GetAsyncKeyState wrapper
using System;
using System.Runtime.InteropServices;
namespace System.Input
{
public static class AsyncKeyState
{
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int key);
@michel-pi
michel-pi / StaticRandom.cs
Created April 1, 2019 07:20
A static and thread safe pseudo-random number generator
using System;
using System.Threading;
namespace System
{
/// <summary>
/// Represents a static and thread safe pseudo-random number generator, which is a device that produces a sequence of numbers that meet certain statistical requirements for randomness.
/// </summary>
public static class StaticRandom
{
@michel-pi
michel-pi / SecureStaticRandom.cs
Created April 1, 2019 07:21
A static and thread safe cryptographic Random Number Generator (RNG)
using System;
namespace System.Security.Cryptography
{
/// <summary>
/// Represents a static and thread safe cryptographic Random Number Generator (RNG) using the implementation provided by the cryptographic service provider (CSP).
/// </summary>
public static class SecureStaticRandom
{
private const long MAX_EXCLUSIVE_UINT = 1 + (long)uint.MaxValue;
@michel-pi
michel-pi / SystemError.cs
Created April 5, 2019 15:34
Contains "all" system error codes
// use System.ComponentModel.Win32Exception instead
using System;
namespace Microsoft.Win32
{
/// <summary>
/// Contains all system error codes.
/// </summary>
public enum SystemError : uint
@michel-pi
michel-pi / NTStatusHelper.cs
Created April 5, 2019 15:37
Provides an up2date NTSTATUS enum and helper methods to use them.
using System;
namespace Microsoft.Win32
{
/// <summary>
/// Provides static helper methods to work with NTSTATUS values.
/// </summary>
public static class NTStatusHelper
{
/// <summary>