//*************************************************************************
//
// This function will register an Object in the OLE 2 database to
// to associate a default icon with the object's type.
//
// Keep in mind the CLSID is a globally unique identifier (guid)
// that identifies the object to any other component object.
//
// You can generate a guid by either running the GUIDGEN app that
// comes with the OLE2 SDK (requires a netcard) or by requesting 
// one from Microsoft.
//
// It is extremeley unsafe, rude, and socially unacceptable to
// use another object's class ID, just as you wouldn't use
// someone else's toothbrush.
//
//*************************************************************************

BOOL RegisterOLEIcon ( LPSTR szCLSIDObject,   // Class ID of the Object
                       LPSTR szObjectName,    // Human Readable Object Name
                       LPSTR szIconPath )     // Path and Index of Icon
                                              // i.e. "E:\\EXCEL\\EXCEL.EXE,2"
{
    HKEY hKey;
    LONG lRes = 0L;

    char lpszCLSID[]       = "CLSID";         // OLE Class ID Section of eg DB
    char lpszDefIcon[]     = "\\DefaultIcon"; // Default Icon Subkey
    char lpBuffer[64];                        // Working buffer
    
    // Open the reg db for the "CLSID" key, this top-level key
    // is where OLE2 and other OLE aware apps will look for
    // this information.

    lRes = RegOpenKey(HKEY_CLASSES_ROOT, lpszCLSID, &hKey);
    
    if (ERROR_SUCCESS != lRes)
      {
      OutputDebugString("RegOpenKey failed.\r\n");
      return FALSE;
      }                                             
    
    // Register the Object
                                        
    // Set the value of the CLSID Entry to the Human Readable 
    // name of the Object

    lRes = RegSetValue( hKey, 
                        szCLSIDObject, 
                        REG_SZ, 
                        szObjectName, 
                        lstrlen(szObjectName)
                      );
    
    if (ERROR_SUCCESS != lRes)  // bail on failure
      {     
      RegCloseKey(hKey);
      return FALSE;
      } 
    
    // Build "defaulticon" subkey string  "{ <class id> }\\DefaultIcon"
    lstrcpy (lpBuffer, szCLSIDObject);
    lstrcat (lpBuffer, lpszDefIcon);                                     
                                     
    // Set Object's default icon entry to point to the 
    // default icon for the object                                     

    lRes = RegSetValue( hKey, 
                        lpBuffer, 
                        REG_SZ, 
                        szIconPath, 
                        lstrlen(szIconPath)
                      );
    
    if (ERROR_SUCCESS != lRes)  // bail on failure
      {     
      RegCloseKey(hKey);
      return FALSE;
      }                 
                       
    // Close the reg db  

    RegCloseKey(hKey);   
    
    return TRUE;
}

//*************************************************************************
//
//  This is a little stub-o-code to demo the above function
//
//*************************************************************************

BOOL RegisterOLEIcons ( void )
{
    // This is a globally unique identifier (guid) for the FOO Object

    char lpszFooCLSID[]    = "{XXXXXXXXXX}";   
    char lpszFooName[]     = "My Application's Foo Object";
    char lpszFooIconPath[] = "c:\\myapp\\myapp.exe, 0";

    // This is a globally unique identifier (guid) for the BAR Object

    char lpszBarCLSID[]    = "{YYYYYYYYYY}";   
    char lpszBarName[]     = "My Application's Bar Object";
    char lpszBarIconPath[] = "c:\\myapp\\myapp.exe, 1";

    if (!RegisterOLEIcon ( lpszFooCLSID, lpszFooName, lpszFooIconPath ))
      OutputDebugString ( "Foo Object Failed to Register.\r\n" );

    if (!RegisterOLEIcon ( lpszBarCLSID, lpszBarName, lpszBarIconPath ))
      OutputDebugString ( "Bar Object Failed to Register.\r\n" );
}



