It is currently Wed Jan 27, 2021 5:36 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Using Resource Dll created with Resource Hacker
PostPosted: Sat Jan 25, 2020 12:17 pm 
Offline
User
User

Joined: Wed Nov 06, 2019 10:28 am
Posts: 86
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:
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Majestic-12 [Bot] and 16 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye