AddResource and how to access it?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Rollo007
Posts: 17
Joined: 24 Mar 2021, 08:23

AddResource and how to access it?

15 May 2021, 18:58

Hey,
can anybody tell me how to access a resource which was added to the compiled exe via AddResource, especially if it is a bitmap picture but not a icon?
(Have tried a million ways with "LoadPicture")

Thanks in advance.

Kind Regards
Rolo
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: AddResource and how to access it?

15 May 2021, 19:30

Code: Select all

% A_ScriptFullPath, # ; Replace # with 6 or higher
Rollo007
Posts: 17
Joined: 24 Mar 2021, 08:23

Re: AddResource and how to access it?

16 May 2021, 08:04

Thanks for your support, but it do not work.

Cause of the "% " I guessed it would work directly (without a handle) like

Code: Select all

Gui, Add, Picture, w500 h-1, % A_ScriptFullPath, 6
Then I tried with

Code: Select all

LogoHandle := LoadPicture( A_ScriptFullPath, "6")
(which I had tried before already)

But all those code only shows the first icon of the exe.
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: AddResource and how to access it?

16 May 2021, 08:39

@Rollo007
Show the code you use to add the resource.
Rollo007
Posts: 17
Joined: 24 Mar 2021, 08:23

Re: AddResource and how to access it?

16 May 2021, 09:53

Code: Select all

;@Ahk2Exe-AddResource C:\Program Files\MyApp\Logo.bmp, LOGO1
I have checked that the size of the exe is growing similar to the size of the bmp. So adding the bmp-resource should work fine.
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: AddResource and how to access it?

16 May 2021, 10:52

Try this:

Code: Select all

;@Ahk2Exe-AddResource C:\Program Files\MyApp\Logo.bmp, LOGO1
if !A_IsCompiled
   hBitmap := LoadPicture("C:\Program Files\MyApp\Logo.bmp")
else {
   hModule := DllCall("GetModuleHandle", "Str", A_ScriptFullPath, "Ptr")
   hBitmap := DllCall("LoadImage", "Ptr", hModule, "Str", "LOGO1", "UInt", 0, "Int", 0, "Int", 0, "UInt", 0, "Ptr")
}
Gui, Add, Pic,, HBITMAP: %hBitmap%
Gui, Show
Return

GuiClose:
   ExitApp
Rollo007
Posts: 17
Joined: 24 Mar 2021, 08:23

Re: AddResource and how to access it?

16 May 2021, 16:30

teadrinker wrote:
16 May 2021, 10:52
Try this:

Code: Select all

   hModule := DllCall("GetModuleHandle", "Str", A_ScriptFullPath, "Ptr")
   hBitmap := DllCall("LoadImage", "Ptr", hModule, "Str", "LOGO1", "UInt", 0, "Int", 0, "Int", 0, "UInt", 0, "Ptr")

Thanks so much Teadrinker, it works perfect.

What is about the first answer from Ecimeric? Just bullshit?
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: AddResource and how to access it?

16 May 2021, 18:04

Perhaps he just didn't quite understand the question. His answer seems to be about displaying the tray icon.
Rollo007
Posts: 17
Joined: 24 Mar 2021, 08:23

Re: AddResource and how to access it?

16 May 2021, 21:35

I thought similar fist, but the "6 or higher" makes me wondering.

Do you know a way how to access the bmp if the module is not loaded? (by another compiled script for example)
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: AddResource and how to access it?

17 May 2021, 01:57

Code: Select all

modulePath := "D:\CompiledScriptPath.exe"
resourceName := "LOGO1"

hLib := DllCall("LoadLibraryEx", "Str", modulePath, "Ptr", 0, "UInt", LOAD_LIBRARY_AS_IMAGE_RESOURCE := 0x20, "Ptr")
hBitmap := DllCall("LoadImage", "Ptr", hLib, "Str", resourceName, "UInt", 0, "Int", 0, "Int", 0, "UInt", 0, "Ptr")
DllCall("FreeLibrary", "Ptr", hLib)

Gui, Add, Pic,, HBITMAP: %hBitmap%
Gui, Show
Return

GuiClose:
   ExitApp
Rollo007
Posts: 17
Joined: 24 Mar 2021, 08:23

Re: AddResource and how to access it?

17 May 2021, 07:15

You are FANTASTIC !!

Thanks so much.
Rollo007
Posts: 17
Joined: 24 Mar 2021, 08:23

Re: AddResource and how to access it?

17 May 2021, 12:39

I guess it would be necessary to add a

Code: Select all

DllCall("FreeLibrary", "Ptr", hModule)
after the LoadImage.
What do you think?
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: AddResource and how to access it?

17 May 2021, 12:43

Nope.
GetModuleHandle wrote:The GetModuleHandle function returns a handle to a mapped module without incrementing its reference count. However, if this handle is passed to the FreeLibrary function, the reference count of the mapped module will be decremented. Therefore, do not pass a handle returned by GetModuleHandle to the FreeLibrary function.
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: AddResource and how to access it?

17 May 2021, 12:51

However, GetModuleHandle can be called like this:

Code: Select all

hModule := DllCall("GetModuleHandle", "Ptr", 0, "Ptr")
Rollo007
Posts: 17
Joined: 24 Mar 2021, 08:23

Re: AddResource and how to access it?

17 May 2021, 14:03

Missunderstanding. I wrote this regarding the LoadLibraryEx function
Rollo007
Posts: 17
Joined: 24 Mar 2021, 08:23

Re: AddResource and how to access it?

17 May 2021, 14:46

Ha ha nooo -it was not a misunderstanding by me. :D
The misunderstanding was that you thought I mentioned FreeLibrary regarding GetModuleHandle,
BUT I wrote it regarding the LoadLibraryEx function.
And the LoadLibraryEx do incrementing the reference counter.
So it is necessary to call a FreeLibrary after loading the pic.
Or am i wrong?
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: AddResource and how to access it?

17 May 2021, 15:03

But after calling LoadLibraryEx I do call FreeLibrary with the handle which LoadLibraryEx returns (hLib):

Code: Select all

hLib := DllCall("LoadLibraryEx", "Str", modulePath, "Ptr", 0, "UInt", LOAD_LIBRARY_AS_IMAGE_RESOURCE := 0x20, "Ptr")
hBitmap := DllCall("LoadImage", "Ptr", hLib, "Str", resourceName, "UInt", 0, "Int", 0, "Int", 0, "UInt", 0, "Ptr")
DllCall("FreeLibrary", "Ptr", hLib)
Rollo007
Posts: 17
Joined: 24 Mar 2021, 08:23

Re: AddResource and how to access it?

17 May 2021, 15:51

OMG
Unbelievable, but I've just overlooked this. :facepalm:

Okay, your code were perfect.

Just one more question:
I tried to free the image like

Code: Select all

e:= DllCall("DeleteObject", "Ptr", hLogo)
but it do not work. (e = 0)
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: AddResource and how to access it?

17 May 2021, 16:14

If your hLogo is the same as my hBitmap, you don't need to free it explicitly, it will be automatically deleted after the control is destroyed.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, Google [Bot] and 376 guests