Getting a handle on LoadImage or LoadIcon API

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)))
      'Else
         m_hIcon = LoadIcon(m_hModule, ID)
      'End If
     
      If m_hIcon Then
         Me.Caption = "Icon: " & ID
         Me.Show
      Else
         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)
      Else
         m_FreeLib = True
      End If
   End If
   
   ' Cache module handle and return success
   m_hModule = hModule
   LoadModule = (hModule <> 0)
End Function

TIA
LVL 2
Caseys95Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ArkConnect With a Mentor Commented:
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.

Cheers
0
 
ArkCommented:
Hi

>>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 http://www.planet-source-code.com/xq/ASP/txtCodeId.25890/lngWId.1/qx/vb/scripts/ShowCode.htm

(and vote if you like it :))

Cheers
0
 
Richie_SimonettiIT OperationsCommented:
listening...
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
wsh2Commented:
<ping>
0
 
Caseys95Author Commented:
Ark:

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

All:

  Found this at MSDN
<a href=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_icons.asp</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.
0
 
Caseys95Author Commented:
Ark,

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.' ?
0
 
ArkCommented:
Hi
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.