Link to home
Start Free TrialLog in
Avatar of justin_robledo
justin_robledo

asked on

Need help using ExtractIcon API.

I am writting an applications that extracts the icon from a file. When I
open a file and use the name to extract the icon with the icon index= -1, so
I can get the total number of icons in the file, the return value is NULL.
The following is the code I am using:

   pWin->hInst ==== The global instance
   pWin->szFile === The path and name of the file, which is taken from the
opendlg.

   // Get number of icons in file
   lstIcons = ExtractIcon(pWin->hInst, pWin->szFile, -1);

  // Display messagebox if there are no icons in file
     if (lstIcons==NULL)
         MessageBox(hwnd,"File doesn't have an icon",
                                 "No Icon", MB_ICONINFORMATION);

Does anyone know if I am doing something wrong. I am using straight C, no
MFC or OWL.

Eliud
ASKER CERTIFIED SOLUTION
Avatar of alexo
alexo
Flag of Antarctica image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, what was the culprit?
Avatar of justin_robledo
justin_robledo

ASKER

Your suggestion worked to a certain point. I declared the global instance after creating the main window and passed the file name without parsing out the path. Now I can only extract the icons in my own program. When I try opening any other program, I still get a NULL value. Can you help me some more.
What does GetLastError() say?
I don't know if this is right but this is the code that I used to get the error code:
            DWORD e;
            e=GetLastError();
            SetDlgItemInt(pWin->hdlg,IDC_E1, e, FALSE);
And the value is 32.

Are you using GetLastError() IMMEDIATELY after the call that fails?
Error 32 is SHARING_VIOLATION.  How do you open the file?  What is the sharing mode?  What is the access (read/write)?
I initialize an open dialog box with the following code,
        // Initialize OPENFILENAME
      pWin->ofn.lStructSize = sizeof(OPENFILENAME);
      pWin->ofn.hwndOwner = hwnd;
      pWin->ofn.lpstrFile = pWin->szFile;
      pWin->ofn.nMaxFile = sizeof(pWin->szFile);
      pWin->ofn.lpstrFilter = "Executable Files\0*.EXE\0Dll Files\0*.DLL\0Icon Files\0*.ICO\0All Files\0*.*\0";
      pWin->ofn.nFilterIndex = 1;
      pWin->ofn.lpstrFileTitle = NULL;
      pWin->ofn.nMaxFileTitle = 0;
   pWin->ofn.lpstrTitle = NULL;
      pWin->ofn.lpstrInitialDir = NULL;
      pWin->ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

then I call GetOpenFileName(&pWin->ofn). I then pass pWin->szFile to the ExtractIcon API function and then call GetLastError().
Try using the OFN_SHAREAWARE flag.
I found the problem. It was that I was opening the file when all I needed is just the path and file name. I did not have to open the file to use the ExtractIcon API function. I did not figure that opening the file would give me an error. Thanks for all your help.