troubleshooting Question

How to save a icon?(create ico file)

Avatar of nkraghu
nkraghu asked on
Microsoft Development
18 Comments1 Solution6188 ViewsLast Modified:
Hi everbody,
          I have a handle of an icon(HICON) which i got using SHGetFileInfo. I want to save this handle as an Icon file(.ico). I am using following procedure:

int  SaveMyIcon( HICON hIcon)
  int       fh, i, iResult;
  UINT      uiSize;
  DWORD     dwSize;
  LPSTR lpGMem;

    if (!hIcon)
      return FALSE;

    dwSize  = GlobalSize(hIcon);
    lpGMem  = (LPSTR)GlobalLock(hIcon);
    fh = OpenFile ("myicon.ico", &of, OF_WRITE | OF_CREATE);

    if (fh == -1)  // If NOT opened successfully.
     MessageBox(NULL, "Unable to create file", NULL, MB_OK );
     return FALSE;

    uiSize = _lwrite(fh, (LPSTR)lpGMem, (UINT)dwSize);

    if (uiSize == -1 || uiSize < (UINT)dwSize)
       MessageBox(NULL, "Unable to read file", NULL, MB_OK );
       return FALSE;
    else // Everything worked, return hGMem.
      return TRUE;

This procedure is not working. I am unable to create a ico file of the handle using the above procedure. How to save an icon file using the handle of an icon?

Thanks in advance.
Join our community to see this answer!
Unlock 1 Answer and 18 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 18 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros