Link to home
Start Free TrialLog in
Avatar of Caseys95
Caseys95

asked on

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
Avatar of Ark
Ark
Flag of Russian Federation image

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
listening...
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation 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
Avatar of wsh2
wsh2

<ping>
Avatar of Caseys95

ASKER

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.
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.' ?
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