Getting a handle on LoadImage or LoadIcon API

Posted on 2001-08-13
Last Modified: 2008-01-09
Nt or 98

I can't seem to get either of these api functions to load the resource icon so I can use it in my program. They are returning 0. I am also using loadbitmap and loadstring, clones of displayicon() , which work fine.

Is this a module handle vs instance handle issue?

This is part of the code ( ID is "#1"  and I know there is one Icon):

Public Sub DisplayIcon(ByVal FileName As String, ByVal ID As String)

   ' Try to load resource if we have module mapped.
   If LoadModule(FileName) Then
      'If InStr(ID, "#") = 1 Then
         m_hIcon = LoadIcon(m_hModule, CLng(Mid$(ID, 2)))
         m_hIcon = LoadIcon(m_hModule, ID)
      'End If
      If m_hIcon Then
         Me.Caption = "Icon: " & ID
         Debug.Print "LoadIcon error (" & Err.LastDllError;
         Debug.Print "): " & ApiErrorText(Err.LastDllError)
      End If
   End If

   ' Unload if we failed.
   If Me.Visible = False Then Unload Me
End Sub

Private Function LoadModule(ByVal FileName As String) As Boolean
   Dim hModule As Long
   ' Clear flag to free module
   m_FreeLib = False
   ' Check first to see if the module is already
   ' mapped into this process.
   hModule = GetModuleHandle(FileName)
   If hModule = 0 Then
      hModule = LoadLibraryEx(FileName, 0&, LOAD_LIBRARY_AS_DATAFILE)
      If hModule = 0 Then
         ' Problem --> can't load module!
         Debug.Print "LoadLibraryEx error (" & Err.LastDllError;
         Debug.Print "): " & ApiErrorText(Err.LastDllError)
         m_FreeLib = True
      End If
   End If
   ' Cache module handle and return success
   m_hModule = hModule
   LoadModule = (hModule <> 0)
End Function

Question by:Caseys95
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 28

Expert Comment

ID: 6382331

>>hModule = LoadLibraryEx(FileName, 0&, LOAD_LIBRARY_AS_DATAFILE)<<
Here it is!

LoadIcon doesn't work this way. Change it to LoadLibrary and everything will be OK.
BTW, if you're interesting on resources, take a look on my new code at

(and vote if you like it :))

LVL 16

Expert Comment

ID: 6382353
LVL 28

Accepted Solution

Ark earned 100 total points
ID: 6382405
Just an explanation.
LOAD_LIBRARY_AS_DATAFILE - you load library only as array of resources. You can get icons as structures and CreateIconIndirect or CreateIconFromResourceEx  from this structure.

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

LVL 14

Expert Comment

ID: 6382672

Author Comment

ID: 6383904

  Thanks for the comment, but it did not work for me. The array idea did put me on the right track.


  Found this at MSDN
<a href=</a>

It has come to my attention that I was trying to open a group icon (RT_GROUP_ICON). I have not yet tried this but will post the results.

Author Comment

ID: 6391077

I'm confused. I checked your code at the site were you have it listed. It uses the LoadLibraryEx as I was using. So why the comment 'LoadIcon doesn't work this way. Change it to LoadLibrary and everything will be OK.' ?
LVL 28

Expert Comment

ID: 6391103
LOAD_LIBRARY_AS_DATAFILE cause a problem, not LoadLibraryEx. You still can use LoadLibraryEx(sName, 0) or LoadLibraryEx(sName, 1) but this is almost same as LoadLibrary

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question