Tuesday, February 23, 2010

Smallest C++ windowed application

Interesting to see this is the smallest windowed Windows application:

#include <windows.h> 

LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShow)
{
    MSG msg;

    WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, 
    WndProc, 0, 0, hInstance, NULL, NULL, (HBRUSH)(COLOR_WINDOW+1), 
    NULL, L"DX9_TUTORIAL1_CLASS", NULL}; 

    RegisterClassEx(&wc);

    HWND hMainWnd = CreateWindow(L"DX9_TUTORIAL1_CLASS", 
    L"DirectX 9 Bare Bones Tutorial 1", 
    WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, 
    NULL, NULL, hInstance, NULL);
    ShowWindow(hMainWnd, nShow);
    UpdateWindow(hMainWnd);

    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return(0);
} 

LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_DESTROY:
        PostQuitMessage(0);
        return(0);
    }

    return(DefWindowProc(hwnd, msg, wParam, lParam));
}

Thursday, February 18, 2010

Using minidumps in .NET 4.0

.NET 4.0 provides for post mortem debugging of minudumps - but only if they include a full memory dump (which makes them not so 'mini' any more). This post list tips for using these dumps.

ProcDump can be used to make dumps in various scenarios, including high CPU usage, unhandled exception, etc.

Tuesday, February 16, 2010

Setting user environment variables for non-administrators with UAC

It is not possible to use the System Properties to set the environment variables for a non-administrative user. Launching this dialog requires elevation, and this means running as Administrator.

To correctly set the variables using a GUI, use User Accounts in the control panel.

To use the command line, use setx.

Wednesday, February 3, 2010

Converting SecureString to String

This blog shows the correct way to convert a SecureString to a String for the many APIs that require it.

Copied here in case the link breaks sometime in the future:
public static string ConvertToUnsecureString(this SecureString securePassword)
{
    if (securePassword == null)
        throw new ArgumentNullException("securePassword");

    IntPtr unmanagedString = IntPtr.Zero;
    try
    {
        unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(securePassword);
        return Marshal.PtrToStringUni(unmanagedString);
    }
    finally
    {
        Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
    }
}

public static SecureString ConvertToSecureString(this string password)
{
    if (password == null)
        throw new ArgumentNullException("password");

    unsafe
    {
        fixed (char* passwordChars = password)
        {
            var securePassword = new SecureString(passwordChars, password.Length);
            securePassword.MakeReadOnly();
            return securePassword;
        }
    }
}

Tuesday, February 2, 2010

Trying a new keyboard layout

I am trying the Colemak keyboard layout because it seems to be a lot smarter than qwerty. Requirements of keyboards have moved on since the days of typrewriters...

Colemak seems to be a good candidate because it minimises the number of changed keys to make it easier to switch between the ubiquitous qwerty and Colemak.

To test my current speeds as a benchmark, I used an online speed test.

On 2 February, 2010, My qwerty speed was 61 WPM, and my Colemak speed was 9 WPM, while using the on-screen keyboard as a guide.

On 3 February, 2010, my Colemak speed was 10 WPM, without looking at the keys.

On 7 February, 2010, my Colemak speed was 15 WPM.

On 8 February, 2010, my Colemak speed was 19 WPM (with one mistake: missing the second 'e' of Hernecastle).

Monday, February 1, 2010

Preserving the complete stack trace when rethrowing an exception

Even throw; loses the top level of the stack trace. Use this 'hack' to work around the problem:

static void PreserveStackTrace(Exception exception)
{
    MethodInfo preserveStackTrace = typeof(Exception).GetMethod("InternalPreserveStackTrace",
        BindingFlags.Instance | BindingFlags.NonPublic);
    preserveStackTrace.Invoke(exception, null);
}

(from here)