Page 3 of 3

Posted: Wed Nov 29, 2006 4:47 pm
by Shardik
If you want to see which cursors are contained as resources in a library (like .Exe, .DLL, .OCX or .Scr), try the following code example which displays all cursors of MSPaint.Exe:

Code: Select all

#RT_GROUP_CURSOR = 12

NewList ResNameList.S()

Procedure.l EnumResNameProc(ResHandle.L, ResType.L, ResName.L, AppParam.L)
  Shared ResNameList.S()

  AddElement(ResNameList())
  ResNameList() = "#" + Str(ResName)

  ProcedureReturn #True
EndProcedure


Procedure.L EnumResTypeProc(ResHandle.L, ResType.L, AppParam.L)
  If ResType = #RT_GROUP_CURSOR
    EnumResourceNames_(ResHandle, ResType, @EnumResNameProc(), 0)
  EndIf

  ProcedureReturn #True 
EndProcedure 


LibName.S = "MSPaint.Exe"
LibHandle.L = LoadLibrary_(@LibName)

If LibHandle = 0
  MessageRequester("Error", "Loading of " + LibName + " failed!", #MB_ICONERROR)
  End
EndIf

EnumResourceTypes_(LibHandle, @EnumResTypeProc(), 0) 

CreateImage(0, 135, 57) 
StartDrawing(ImageOutput(0)) 
Box(0, 0, 135, 57, #White) 
StopDrawing() 

OpenWindow(0, 0, 0, 160, 100, "Display lib's cursors", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
CreateGadgetList(WindowID(0)) 
ImageGadget(0, 10, 10, 135, 57, ImageID(0), #PB_Image_Border)
ButtonGadget(1, 10, 75, 140, 20, "Switch to another cursor")

ForEach ResNameList()
  CursorHandle = LoadImage_(LibHandle, ResNameList(), #IMAGE_CURSOR, 0, 0, #LR_DEFAULTCOLOR)

  If CursorHandle = 0
    MessageRequester("Error", "Loading of cursor " + ResNameList() + " from " + LibName + " failed!", #MB_ICONERROR)
    FreeLibrary_(LibHandle)
    End
  EndIf  

  SetClassLong_(GadgetID(0), #GCL_HCURSOR, CursorHandle) 

  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Gadget
        If EventGadget() = 1
          Break
        EndIf
      Case #PB_Event_CloseWindow
        DestroyIcon_(CursorHandle) 
        FreeLibrary_(LibHandle)      
        End
    EndSelect
  ForEver

  DestroyIcon_(CursorHandle)
Next

FreeLibrary_(LibHandle)

Posted: Wed Nov 29, 2006 5:00 pm
by srod
Great example thanks. I'll keep that one. :)

I answered my own question as to why ExtractIcon_() doesn't find the cursors. It extracts icons - not cursors! Doh! :oops:

Posted: Thu Sep 03, 2009 7:45 am
by Kwai chang caine
Yes the code of SHARDIK is great and so simple 8)
But KCC have again a fly in his milk :cry:

If i have insert the ressource in my exe with

Code: Select all

Import "Ressource.res"
EndImport
I can't use for obtain the handle :cry:

Code: Select all

LibHandle.L = LoadLibrary_(@LibName) 
And i want enumerate the ressource inside it
How can i obtain the handle of the exe who extract HIS ressources ????

In the GREAT code of DROOPY lib, he use, for extract HIS ressource:

Code: Select all

HandleResource= FindResource_(0,@Name,#RT_RCDATA)
But if i don't know the name of the ressource who is in my exe ???
I must enumerate all the ressource in my EXE, and choose the ressource i want to extract :roll:
How can i do ???

Posted: Thu Sep 03, 2009 9:25 am
by cas
EnumResourceNames_() or EnumResourceNamesEx_() ?

Posted: Thu Sep 03, 2009 9:40 am
by Kwai chang caine
Hello CAS :D
Thanks to answer at my question 8)
EnumResourceNames_() or EnumResourceNamesEx_() ?
In fact i don't know :oops:
My problem is for understand how an EXE can scan itself for ENUMERATE and extracting ressources inside him ???? :roll:

I found some codes, who use for access at the ressource.

Code: Select all

LibHandle.L = LoadLibrary_(@LibName) 
But me...the ressources is in the exe who ENUMERATE and extract it
I have include it with

Code: Select all

Import "Ressources.res"
EndImport
I don't know how the EXE obtain the handle of himself ??? :cry:

In the great code of DROOPY, the EXE scan itself and save ressource in a picture file, but i don't understand how ENUMERATE the ressources in this code

Code: Select all

; PureBasic 3.94 25/11/05

; Create a File with RC Extension And include it as RC_DATA Resource Type 
; Path can be absolute Or relative
; Exemple :
            ; REG RCData "C:\WINDOWS\REGEDIT.EXE"
            ; SON RCData "son.wav"
            ; Image RCData "image.bmp"

;/ SaveResourceAs(Name.s,File.s) Save the resource as a file
;/ GetResourcePointer(Name.s) is usefull with PureBasic CatchXXX functions (CatchImage / CatchSound ... )

ProcedureDLL SaveResourceAs(Name.s,File.s)
  
  ; (Use GetModuleHandle_ if you want to specify another file)
  
  HandleResource= FindResource_(0,@Name,#RT_RCDATA)
  If HandleResource
    HandleGlobalMemoryBlock=LoadResource_(0,HandleResource) 
    PointerFirstByteOfTheResource=LockResource_(HandleGlobalMemoryBlock)
    
    ; Get size of the resource
    Size= SizeofResource_(Handle,HandleResource)
    
    ; Save the file
    FileId=OpenFile(#PB_Any,File)
    If FileId
      WriteData(HandleGlobalMemoryBlock,Size)
      CloseFile(FileId)
    EndIf
    
    ; Test if the file is written
    If FileSize(File)=Size 
      ProcedureReturn 1
    EndIf
  EndIf
  
EndProcedure

ProcedureDLL GetResourcePointer(Name.s)
  
  ; (Use GetModuleHandle_ if you want to specify another file)
  
  HandleResource= FindResource_(0,@Name,#RT_RCDATA)
  If HandleResource
    HandleGlobalMemoryBlock=LoadResource_(0,HandleResource) 
    PointerFirstByteOfTheResource=LockResource_(HandleGlobalMemoryBlock)
    
    ProcedureReturn HandleGlobalMemoryBlock
  EndIf
EndProcedure

ProcedureDLL GetResourceSize(Name.s)
  
  ; (Use GetModuleHandle_ if you want to specify another file)
  
  HandleResource= FindResource_(0,@Name,#RT_RCDATA)
  If HandleResource
    HandleGlobalMemoryBlock=LoadResource_(0,HandleResource) 
    PointerFirstByteOfTheResource=LockResource_(HandleGlobalMemoryBlock)
    
    ; Return the size of the resource
    Size= SizeofResource_(Handle,HandleResource)
    ProcedureReturn Size
  EndIf
  
EndProcedure

;/ Test
CatchImage(0,GetResourcePointer("IMAGE"))
OpenWindow(0,0,0,240,240,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"ImageGadget")
ImageGadget(0,20,20,200,200,UseImage(0),#PB_Image_Border)  
SaveResourceAs("REG","c:\Picture.jpg")

Posted: Thu Sep 03, 2009 10:09 am
by Kwai chang caine
I have destroy the nice code of SHARDIK to made that...
That's works apparently :D

1/ I use "GetModuleHandle_(0)" but is it the good way ??? :roll:
2/ The line is it usefull ????

Code: Select all

FreeLibrary_(LibHandle)  
This is my cowpat ...

Code: Select all

Import "c:\Ressources.res"
EndImport
 
Procedure.l EnumResNameProc(ResHandle.L, ResType.L, ResName.L, AppParam.L) 
 Debug PeekS(ResName)
 ProcedureReturn #True 
EndProcedure 


Procedure.L EnumResTypeProc(ResHandle.L, ResType.L, AppParam.L)
  Debug ResType
  If ResType = #RT_RCDATA
    EnumResourceNames_(ResHandle, ResType, @EnumResNameProc(), 0) 
  EndIf 

  ProcedureReturn #True 
EndProcedure 

LibHandle.L = GetModuleHandle_(0)
EnumResourceTypes_(LibHandle, @EnumResTypeProc(), 0) 

Repeat 
 Delay(100)
ForEver

FreeLibrary_(LibHandle)  

Posted: Thu Sep 03, 2009 10:31 am
by eesau
Kwaï chang caïne wrote:1/ I use "GetModuleHandle_(0)" but is it the good way ??? :roll:
I think you can just pass #Null to EnumResourceTypes_ to get the same effect. I.e.:

Code: Select all

EnumResourceTypes_(0, @EnumResTypeProc(), 0) 
I can't test it right now though, so no guarantees.

Posted: Thu Sep 03, 2009 10:48 am
by eesau
Kwaï chang caïne wrote:2/ The line is it usefull ????

Code: Select all

FreeLibrary_(LibHandle)  
You shouldn't do that, here's an explanation from msdn:
msdn wrote:Use caution when calling FreeLibrary with a handle returned by GetModuleHandle. The GetModuleHandle function does not increment a module's reference count, so passing this handle to FreeLibrary can cause a module to be unloaded prematurely.

Posted: Thu Sep 03, 2009 11:05 am
by Kwai chang caine
I think you can just pass #Null to EnumResourceTypes_ to get the same effect. I.e.:
Thanks a lot EESAU 8)
You're right, i just replace the handle by "0" and that's works better fine :D
Just a "0" is the fault of all my worries :shock:
You shouldn't do that, here's an explanation from msdn:
Ok thanks too...
If EESAU and Mister CROSOFT say KCC don't use FREELIBRARY :?

Then KCC don't use..... :oops:
I promise to you :D

Thanks a lot again for your precious help
I wish you a very good day 8)

Posted: Thu Sep 03, 2009 11:07 am
by eesau
Kwaï chang caïne wrote:You're right, i just replace the handle by "0" and that's works better fine :D
Glad that it works :)