Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Getting a handle on LoadImage or LoadIcon API

Posted on 2001-08-13
7
Medium Priority
?
633 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 28

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 28

Accepted Solution

by:
Ark earned 300 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 28

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

885 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