Using Resource Dll created with Resource Hacker

Share your advanced PureBasic knowledge/code with the community.
tatanas
Enthusiast
Enthusiast
Posts: 204
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Using Resource Dll created with Resource Hacker

Post by tatanas »

Hi,

Because I don't like .ico, .gif, .png, ... stuff everywhere inside my program folder, I search how I could store those files inside one file.
Resource file bumped up and ... Resource Hacker !
I could easily add icons to a dll resource file with this soft and extract them with ExtractIcon_() WinApi.
The problem was with gif image which are stored as RCData.

Here is the code to use them : (you can use CatchImage() too instead of creating a file)

Code: Select all

Macro MAKEINTRESOURCE(Value)
	(Value & $FFFF)
EndMacro

Macro IS_INTRESOURCE(Val)
	Bool(Val & ~$FFFF)
EndMacro

#LOAD_LIBRARY_AS_IMAGE_RESOURCE = $20
#LOAD_LIBRARY_AS_DATAFILE = $2

Dim a_ImgResource(1) ; this array will stock the result of GetImagePointerFromResource()
							; a_ImgResource(0) = pointer to the image
							; a_ImgResource(1) = size of the image


; Load an image (gif, png, ...) from Dll Resource file created with Resource Hacker
; Depending on the destination of the image inside the Dll, Choose a known ResourceType like #RT_RCDATA or a custom one like "PNG"
;
; -- Parameters --
; a_ImgResource(1)			: Array containing pointer and size of the image in resource file
; DLL_Path 						: Path to the Dll Resource file
; Image_Name 					: Name of the image inside the Dll (defined when you import the file with Resource Hacker)
; ResourceType 				: Resource Type (https://docs.microsoft.com/en-us/windows/win32/menurc/resource-types)
; CustomResourceTypeName 	: Custom Name of the resource type (for example : if you import a png with Resource Hacker, it creates a "PNG" resource type)
;
; -- Return --
; 0 if error
; 1 if success and a_ImgResource() filled
Procedure.i GetImagePointerFromResource(Array a_ImgResource(1), DLL_Path.s, Image_Name.s, ResourceType = #RT_RCDATA, CustomResourceTypeName.s = "")

	Protected hModule ; dll module handle
	Protected hResource ; handle to the specified resource's information block
	Protected hData ; handle to the data associated with the resource
	Protected *pResource ; pointer to the first byte of the resource
	Protected SizeofResource ; number of bytes in the resource
	
	hModule = LoadLibraryEx_(DLL_Path, #Null, #LOAD_LIBRARY_AS_IMAGE_RESOURCE|#LOAD_LIBRARY_AS_DATAFILE)
	If hModule = #Null
		MessageRequester("Information", "LoadLibraryEx Error")
		ProcedureReturn 0
	EndIf
	
	If CustomResourceTypeName = ""
		hResource = FindResource_(hModule, Image_Name, MAKEINTRESOURCE(ResourceType))
	Else
		hResource = FindResource_(hModule, Image_Name, CustomResourceTypeName)
	EndIf
	If hResource = #Null
		MessageRequester("Information", "FindResource Error")
		ProcedureReturn 0
	EndIf
	
	hData = LoadResource_(hModule, hResource)
	If hData = #Null
		MessageRequester("Information", "LoadResource Error")
		ProcedureReturn 0
	EndIf
	
	*pResource = LockResource_(hData)
	If *pResource = #Null
		MessageRequester("Information", "LockResource Error")
		ProcedureReturn 0
	EndIf
	
	SizeofResource = SizeofResource_(hModule, hResource)
	
	a_ImgResource(0) = *pResource
	a_ImgResource(1) = SizeofResource
	
	ProcedureReturn 1
	
EndProcedure


; Example :

; If GetImagePointerFromResource(a_ImgResource(), "resources.dll", "myGifName")
If GetImagePointerFromResource(a_ImgResource(), "resources.dll", "myPNGName", #RT_RCDATA, "PNG")
	
	; If CreateFile(0,"test.gif")	
	If CreateFile(0,"test.png")
		WriteData(0, a_ImgResource(0), a_ImgResource(1))
		CloseFile(0)
	Else
		MessageRequester("Error", "Could not create file")
	EndIf
	
EndIf
Windows 10 Pro x64
PureBasic 6.04 x64
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Using Resource Dll created with Resource Hacker

Post by Lunasole »

Interesting. I also did the same with ResHacker once time ago. But generally anyway for PB it's not much needed, much better is to use "IncludeBinary" to embed anything.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
jacdelad
Addict
Addict
Posts: 1478
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Using Resource Dll created with Resource Hacker

Post by jacdelad »

@Lunasole: Depends on whether these resources are meant be kept hidden or maybe shall be usable for other programs.

BTW: I've been using resource hacker for years now. Seems like the development has stopped long ago.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Using Resource Dll created with Resource Hacker

Post by Kwai chang caine »

A "little bit" late but not see this code :oops:
Good idea :idea:
Thanks at Tatanas for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply