Example of Setting a SCALAR_BINARY Scalar

Currently, only program item folders contain a scalar of type SCALAR_BINARY. The Icon scalar in a program item folder is a binary scalar that specifies an icon resource that will be used as the icon for the program item. The following example contains two functions (SetProgItemIcon and ReadICOFileImage) that load the icon resource and set the Icon scalar for a program item folder:

// Function to set the Icon scalar for a new program item folder.

SMS_STATUS  SetProgItemIcon(HANDLE hFolder, // Handle to program 
                                            //item folder handle.
                            const char* pszIcoFile // Path to 
                                            // icon file.
                            )

{
DWORD dwIconLen;
void* pImage = (void*) ReadICOFileImage(pszIcoFile, &dwIconLen);
if (pImage == NULL) {
    // The icon file could not be read.
    return(SMS_EMPTY);
}

SMS_STATUS stat;
SCALAR sc;
sc.pszName = "Icon";
sc.scType = SCALAR_BINARY;
sc.pValue = pImage;
sc.dwLen = dwIconLen;
stat = SmsSetScalar(hFolder, &sc);
return(stat);
}


// Function to read an .ICO file directly to an in memory image.
// Supports .ICO files that containonly one icon.

BYTE *ReadICOFileImage(const char *pFileName, // filename of .ICO file.
                   DWORD *pdwIconSize // Size of icon image.
                  )
// This function returns a pointer to the .ICO image in memory.
// If the function cannot load the .ICO image, it returns NULL.
{
*pdwIconSize = 0;
FILE *f = fopen(pFileName, "rb");
if (!f) return NULL;        // File missing
BYTE *pIconFileImage;
DWORD dwImageSize;
fseek(f, 0, SEEK_END);            // To determine file size
dwImageSize = (DWORD) ftell(f);   // Get file size
fseek(f, 0, SEEK_SET);            // Move back to start of file
pIconFileImage = (BYTE *) malloc(dwImageSize);

//pIconFileImage = new BYTE[dwImageSize];   // Set up icon buffer

DWORD dwResult = fread(pIconFileImage, sizeof(BYTE), dwImageSize, f);

// Verify that the whole file read into memory
if (dwResult != dwImageSize) {
    free (pIconFileImage);
    pIconFileImage = NULL;
}
fclose(f);
*pdwIconSize = dwImageSize;
return pIconFileImage;
}