Support Forums > WinFBX - Windows Framework for FreeBASIC

AfxExtractResourceToFile

(1/3) > >>

Paul Squires:
Hi Josť,

I couldn't find a function in WinFBX that extracts a resource to a file so I stole some of your other code and put together the code below. Maybe you'll find it useful and include it in the library at some point.

I needed such a function because as you've noted in your CImageCtx control some image types can't be loaded from a resource. I created a PictureBox control for the WinFBE visual designer and it extracts the image to a temp file and uses the LoadImageFromFile function that works well loading the different file types.


--- Code: ---function AfxExtractResourceToFile( byval hInstance as HINSTANCE, _
                                   byref wszResourceName as wstring, _
                                   byref wszFileName as wstring, _
                                   byval lResourceType as LPWSTR _
                                   ) as boolean
   ' Default that the function is successful
   dim AS LONG lResult = true
   
   ' // Find the resource and lock it
   DIM hResource AS HRSRC = FindResourceW(hInstance, wszResourceName, CAST(LPCWSTR, lResourceType))
   IF hResource = NULL THEN RETURN false
   
   DIM imageSize AS DWORD = SizeofResource(hInstance, hResource)
   IF imageSize = 0 THEN RETURN false
   
   DIM pResourceData AS LPVOID = LockResource(LoadResource(hInstance, hResource))
   IF pResourceData = NULL THEN RETURN false
   
   ' // Allocate memory to hold the image
   DIM hGlobal AS HGLOBAL = GlobalAlloc(GMEM_MOVEABLE, imageSize)
   IF hGlobal THEN
      ' // Lock the memory
      DIM pGlobalBuffer AS LPVOID = GlobalLock(hGlobal)
      IF pGlobalBuffer THEN
         
         ' // Copy the image from the resource file to global memory
         memcpy pGlobalBuffer, pResourceData, imageSize

         AfxDeleteFile( wszFileName )
         dim as long f = freefile
         if open( wszFileName, for binary, as #f ) = 0 then
            dim pBuffer as byte ptr = pGlobalBuffer
            put #f, , pBuffer[0], imagesize       
            close #f
         else
            return true
         end if
         ' // Unlock the memory
         GlobalUnlock pGlobalBuffer
      END IF
      ' // Free the memory
      GlobalFree hGlobal
   END IF

   function = lResult
end function

--- End code ---

Josť Roca:
Which values are you passing in the lResourceType parameter?

Josť Roca:
> I needed such a function because as you've noted in your CImageCtx control some image types can't be loaded from a resource.

But the note is for the LoadBitmapFromResource method. Have you tried with LoadImageFromResource?

Paul Squires:

--- Quote from: Josť Roca on January 21, 2019, 11:12:42 AM ---> I needed such a function because as you've noted in your CImageCtx control some image types can't be loaded from a resource.

But the note is for the LoadBitmapFromResource method. Have you tried with LoadImageFromResource?


--- End quote ---

Hi Josť, yes I did try LoadImageFromResource but it failed for JPEG (at least, it did fail for the image jpg image file that I used for testin).

Paul Squires:

--- Quote from: Josť Roca on January 21, 2019, 11:00:01 AM ---Which values are you passing in the lResourceType parameter?


--- End quote ---

Any of these:
https://docs.microsoft.com/en-us/windows/desktop/menurc/resource-types

Navigation

[0] Message Index

[#] Next page

Go to full version