Solved

Getting a handle on LoadImage or LoadIcon API

Posted on 2001-08-13
7
579 Views
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)))
      '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
0
Comment
Question by:Caseys95
7 Comments
 
LVL 27

Expert Comment

by:Ark
ID: 6382331
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6382353
listening...
0
 
LVL 27

Accepted Solution

by:
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.

Cheers
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 14

Expert Comment

by:wsh2
ID: 6382672
<ping>
0
 
LVL 2

Author Comment

by:Caseys95
ID: 6383904
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
 
LVL 2

Author Comment

by:Caseys95
ID: 6391077
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
 
LVL 27

Expert Comment

by:Ark
ID: 6391103
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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I need help formating a date variable in my VB.Net application 2 51
Excel VBA, find a string in a column, update a cell 7 77
How to Add / Edit Windows Menu 4 59
MsgBox 4 59
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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now