Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load an animated cursor at runtime from memory

I want to load an animated cursor stored in .ani format, which is described to be a RIFF archive/container, from memory without writing the memory to a temporary file. Thus far I am able to parse the .ani file structure and load the individual frames as a normal icon with the aid of CreateIconFromResourceEx LookupIconIdFromDirectoryEx

One of the problems that proofs difficult is the actual composition of these frames and the animation data (jiffy-rate etc) as there appears to be no entries in the Windows API to do so. Documentation or written knowledge over the subject seems to be limited to loading non-animated icons/cursors from memory.

Similar questions such as 'Load an embedded animated Cursor from the Resource' express the desire to load an animated cursor from an embeddable resource. However i am not able to reproduce a workable solution from that either. Partly because the resource compiler in visual studio 2008 & 2010 doesn't support .ani (only ico and cur) files and therefor embedding it simply results in a 1:1 copy of the bytes as they were in the original file as opposed to .cur and .ico files which get decomposed into multiple resources. The subsequent call to CreateIconFromResource as shown in both answers does not work because the data it expects is the icon/cursor data of a single directive in a icon archive and not a RIFF-based file structure.

To summarize I'm interested in any information regarding the structures of animated cursors (in memory) or otherwise relevant pointers that could assist me in pursuing my goal.

like image 820
Lawrence Kok Avatar asked Jul 31 '12 17:07

Lawrence Kok


Video Answer


2 Answers

After re-evaluating loading an animated cursor from an embeddable resource as pointed out by mfc I have found a solution that allows me to load the cursors from an arbitrary memory address.

Data from an embeddable resource and data read from a file into memory are exactly identical. Therefor it suggests that CreateIconFromResource should work on normal regular memory as well. However there is one fundamental difference. Memory of the embeddable resources reside in special sections in the executable which are often padded to the nearest 4096-byte boundary. Memory allocated at runtime contains garbage values.

Now the solution that I found working exploits this by simply allocating a guard-band of zero-filled bytes. In my own test cases I have found that 8 is the minimum which also happens to be the size of a chunk in the riff container. Coincidence? What i suspect is that this is a bug and the algorithm happens to work for embeddable resources due to it's alignment restrictions within the dll/executable.

const int guardbandSize = 8;
FILE* fs = fopen("action.ani", "rb");
fseek(fs, 0,SEEK_END); int dwSize = ftell(fs); fseek(fs, 0,SEEK_SET);   
char* memory = new char[dwSize + guardbandSize];
fread(memory, 1, dwSize, fs); memset(memory + dwSize, 0, guardbandSize);
fclose(fs);
cursor = (HCURSOR)CreateIconFromResource((PBYTE)memory,dwSize,FALSE,0x00030000);        
delete memory;

Here is an overview of various ways to load an animated cursors.

#include <Windows.h>
#include <stdio.h>
#include "resource2.h"

void* hWnd;
bool  visible = true;
bool  running = true;
LRESULT CALLBACK WndProcInternal(   HWND hWnd, UINT uMsg, WPARAM    wParam, LPARAM  lParam) ;
HCURSOR cursor = 0;

void main()
{   
    //Setup configuration
    const int Width  = 640, Height = 480;
    const int Method = 4;

    //Setup window class 
    WNDCLASS wcd;
    wcd.style           = CS_PARENTDC | CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 
    wcd.lpfnWndProc     = (WNDPROC)WndProcInternal;         
    wcd.cbClsExtra      = 0;                                
    wcd.cbWndExtra      = 0;                                
    wcd.hInstance       = GetModuleHandle(NULL);            
    wcd.hIcon           = LoadIcon(NULL, IDI_WINLOGO);      
    wcd.hCursor         = LoadCursor(NULL, IDC_ARROW);      
    wcd.hbrBackground   = (HBRUSH)COLOR_BACKGROUND;                             
    wcd.lpszMenuName    = NULL;                             
    wcd.lpszClassName   = TEXT("AnimatedIcon");             

    //Register the window class
    if(!RegisterClass(&wcd)) 
    {
        MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error!"),MB_ICONEXCLAMATION | MB_OK);
        FatalExit(-1);
    }

    //Create a window
    if (!(hWnd=CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("AnimatedIcon"), TEXT("AnimatedIcon"), 
        WS_VISIBLE | WS_CAPTION | WS_MINIMIZEBOX | WS_THICKFRAME | WS_MAXIMIZEBOX | WS_SYSMENU,
        0, 0, Width, Height, NULL, NULL, NULL, NULL)))                          
    {
        MessageBoxA(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        FatalExit(-1);
    }   

    if( Method == 1 )
    {
        //Method 1: Load cursor directly from a file
        cursor = LoadCursorFromFileA("action.ani");
    }
    if( Method == 2 )
    {
        //Method 2: Load cursor from an resource section in the executable.
        cursor = LoadCursor(0, MAKEINTRESOURCE(IDR_ANICURSORS1));
    }
    if( Method == 3 )
    {
        //Method 3: Manually locate the resource section in the executable & create the cursor from the memory.
        HINSTANCE hInst=GetModuleHandle(0);
        HRSRC hRes=FindResourceA(hInst,MAKEINTRESOURCEA(IDR_ANICURSORS1),"ANICURSORS");
        DWORD dwSize=SizeofResource(hInst,hRes);
        HGLOBAL hGlob=LoadResource(hInst,hRes);
        LPBYTE pBytes=(LPBYTE)LockResource(hGlob);
        cursor = (HCURSOR)CreateIconFromResource(pBytes,dwSize,FALSE,0x00030000);
    }
    if( Method == 4 )
    {
        //Method 4: Load the cursor from a file into memory & create the cursor from the memory.
        const int guardbandSize = 8;
        FILE* fs = fopen("action.ani", "rb");
        fseek(fs, 0,SEEK_END); int dwSize = ftell(fs); fseek(fs, 0,SEEK_SET);   
        char* memory = new char[dwSize + guardbandSize];
        fread(memory, 1, dwSize, fs); memset(memory + dwSize, 0, guardbandSize);
        fclose(fs);
        cursor = (HCURSOR)CreateIconFromResource((PBYTE)memory,dwSize,FALSE,0x00030000);        
        delete memory;
    }

    //Set the cursor for the window and display it.
    SetClassLong((HWND)hWnd, GCL_HCURSOR, (LONG)cursor);        
    while (running)     
    {
        MSG wmsg;
        if (PeekMessage(&wmsg, (HWND)hWnd, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&wmsg);
            DispatchMessage(&wmsg);
        }           
    }   
}

LRESULT CALLBACK WndProcInternal(   HWND hWnd, UINT uMsg, WPARAM    wParam, LPARAM  lParam) 
{
    if( uMsg == WM_DESTROY )
    {
        PostQuitMessage(1); 
        running = false;
    }

    return (long)DefWindowProc((HWND)hWnd,uMsg,(WPARAM)wParam,(LPARAM)lParam);
}
like image 175
Lawrence Kok Avatar answered Oct 20 '22 11:10

Lawrence Kok


Import your animated cursor as a custom resource. Give it a text name, eg "MyType". Then load the cursor with:

HCURSOR hCursor = LoadAnimatedCursor(IDR_MYTYPE1, _T("MyType"));

 /* ======================================================== */
HCURSOR LoadAnimatedCursor(UINT nID, LPCTSTR pszResouceType)
{
    HCURSOR hCursor = NULL;
    HINSTANCE hInstance = AfxGetInstanceHandle();
    if (hInstance)
    {   HRSRC hResource = FindResource(hInstance, MAKEINTRESOURCE(nID), pszResouceType);
        DWORD dwResourceSize = SizeofResource(hInstance, hResource);
        if (dwResourceSize>0)
        {   HGLOBAL hRsrcGlobal = LoadResource(hInstance, hResource);
            if (hRsrcGlobal)
            {   LPBYTE pResource = (LPBYTE)LockResource(hRsrcGlobal);
                if (pResource)
                {   hCursor = (HCURSOR)CreateIconFromResource(pResource, dwResourceSize, FALSE, 0x00030000);
                    UnlockResource(pResource);
                }
                FreeResource(hRsrcGlobal);
            }
        }
    }
    return hCursor;
}
like image 33
mfc Avatar answered Oct 20 '22 11:10

mfc