Page 1 of 2

AddResource and how to access it?

Posted: 15 May 2021, 18:58
by Rollo007
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

Re: AddResource and how to access it?

Posted: 15 May 2021, 19:30
by Ecimeric

Code: Select all

% A_ScriptFullPath, # ; Replace # with 6 or higher

Re: AddResource and how to access it?

Posted: 16 May 2021, 08:04
by Rollo007
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.

Re: AddResource and how to access it?

Posted: 16 May 2021, 08:39
by teadrinker
@Rollo007
Show the code you use to add the resource.

Re: AddResource and how to access it?

Posted: 16 May 2021, 09:53
by Rollo007

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.

Re: AddResource and how to access it?

Posted: 16 May 2021, 10:52
by teadrinker
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

Re: AddResource and how to access it?

Posted: 16 May 2021, 16:30
by Rollo007
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?

Re: AddResource and how to access it?

Posted: 16 May 2021, 18:04
by teadrinker
Perhaps he just didn't quite understand the question. His answer seems to be about displaying the tray icon.

Re: AddResource and how to access it?

Posted: 16 May 2021, 21:35
by Rollo007
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)

Re: AddResource and how to access it?

Posted: 17 May 2021, 01:57
by teadrinker

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

Re: AddResource and how to access it?

Posted: 17 May 2021, 07:15
by Rollo007
You are FANTASTIC !!

Thanks so much.

Re: AddResource and how to access it?

Posted: 17 May 2021, 12:39
by Rollo007
I guess it would be necessary to add a

Code: Select all

DllCall("FreeLibrary", "Ptr", hModule)
after the LoadImage.
What do you think?

Re: AddResource and how to access it?

Posted: 17 May 2021, 12:43
by teadrinker
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.

Re: AddResource and how to access it?

Posted: 17 May 2021, 12:51
by teadrinker
However, GetModuleHandle can be called like this:

Code: Select all

hModule := DllCall("GetModuleHandle", "Ptr", 0, "Ptr")

Re: AddResource and how to access it?

Posted: 17 May 2021, 14:03
by Rollo007
Missunderstanding. I wrote this regarding the LoadLibraryEx function

Re: AddResource and how to access it?

Posted: 17 May 2021, 14:11
by teadrinker
Do you understand now? :)

Re: AddResource and how to access it?

Posted: 17 May 2021, 14:46
by Rollo007
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?

Re: AddResource and how to access it?

Posted: 17 May 2021, 15:03
by teadrinker
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)

Re: AddResource and how to access it?

Posted: 17 May 2021, 15:51
by Rollo007
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)

Re: AddResource and how to access it?

Posted: 17 May 2021, 16:14
by teadrinker
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.