Share via


Sample Code to Override an API in the Microsoft Layer for Unicode

The following sample code demonstrates how to override the LoadCursor function in the Microsoft Layer for Unicode.

// forward declare of override
HCURSOR __stdcall UserLoadCursorW(HINSTANCE hInstance, LPCWSTR lpCursorName);

// override
extern FARPROC Unicows_LoadCursorW = (FARPROC)&UserLoadCursorW;

HCURSOR __stdcall
UserLoadCursorW(HINSTANCE hInstance, LPCWSTR lpCursorName)
{
    // Only convert real strings, not user atoms or NULLs
    if ((ULONG_PTR)(lpCursorName) > 0xffff)
    {
        // Get the size of the Unicode string 
        size_t cchCursorName = wcslen(lpCursorName);

        // Allocate enough space for the Ansi string
        LPSTR lpCursorNameA = _alloca((cchCursorName + 1) * 2);

        // Convert the string from Unicode
        WideCharToMultiByte(CP_ACP, 
                            0, 
                            lpCursorName, 
                            cchCursor, 
                            lpCursorNameA, 
                            cchCursor * 2, 
                            NULL, 
                            NULL);

        return(LoadCursorA(hInstance, lpCursorNameA);
    }
    else
    {
        // This is not actually a string, so it is okay to call 
        // straight through without converting the parameter
        return(LoadCursorA(hInstance, (LPSTR)lpCursorName);
    }
}