Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Retrieve JPEG From REsource (Updated code from MM)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Retrieve JPEG From REsource (Updated code from MM)

    Michael Mattais is the author, he did a superb job - this code was shortened and updated for specifically JPEG's in resource files -

    The JPEG will be in STRING format, you can send it over the internet or write it to a file...

    Michael - awesome code- thank you -

    'Resource should look like this:
    #define BBSDOWN 1050
    BBSDOWN JPEG BBSDown.JPG


    Code:
    ' =======================================================================
    ' FILE: image.bas
    ' 10/20/08
    ' Using user-defined resources to store strings and getting them back easily.
    ' Returned string can be used for anything. This example specifically pulls a JPEG from the resource
    ' Note also: The "Resource Type" can be any one of the predefined Windows
    ' resource types, e.g., %RT_MENU, %RT_STRING, %RT_GROUP_ICON, %RT_RCDATA, etc.
    ' The values 1 through 255 are reserved by Windows for future "predefined"
    ' resource types. Individual 'numbered' resource IDs must be in the range 1-65535
    ' --------------------------------------------------------------------------------
    ' COMPILER           : PowerBASIC PB/Win version 8.4
    ' Win32API.INC  Date : August 12, 2008
    ' Author             : Michael Mattias, Racine WI USA
    ' Copyright/Use      : Placed in the public domain by the author 1/26/03
    ' Update Editor      : Scott Turchin
    ' Updated            : Updated for specific use to pull JPEG from resource 10/20/08 PB version 8.4
    ' ========================================================================
    
    #Compile Exe
    #Register None
    #Debug Error On
    #Include "Win32api.inc"
    #Resource "RESOURCE/BBSDOWN.PBR"
    
    Declare Function RetrieveInternalJPEG(ByVal rcNumber As Long) As String
    
    '============================================<WINMAIN>==================================================================
    Function WinMain (ByVal hInstance     As Long, _
                      ByVal hPrevInstance As Long, _
                      ByVal lpCmdLine     As Asciiz Ptr, _
                      ByVal iCmdShow      As Long) As Long
    Local sTmp As String
    Local hFile As Long
    Local rcNumber As Long
    
    rcNumber = 1050 'Assigned in my Resource file
    sTmp = RetrieveInternalJPEG(ByVal rcNumber)
    
    MsgBox sTmp 'Will be gibberish
    'Stow the file to a temporary jpg
    hFile = FreeFile
    Open "Test.jpg" For Binary As #hFile
    Put$ #hFile, sTmp
    Close #hFile
    End Function
    '==========================================================================================
    Function RetrieveInternalJPEG(ByVal rcNumber As Long) As String
    Local LoadLibExhFile    As Long
    Local LoadLibExFlags    As Long
    Local hSource           As Long
    Local hResFind          As Long
    Local hResLoad          As Long
    Local ResSize           As Long
    Local ErrFlag           As Long
    
    Local lResourceName     As Dword
    Local lResourceType     As Dword
    Local dwResourceType    As Dword
    Local dwResourceName    As Dword
    Local szTypeName        As Asciiz * 128
    Local szRcName          As Asciiz * 128
    Local szSourceFile      As Asciiz * %MAX_PATH
    
    Local pResData          As Byte Ptr
    Local pAZ               As Asciiz Ptr
    
    Local St                As String
    Local sResourceName     As String
    Local sResourceType     As String
    
    GetModuleFileName ByVal %NULL, szSourceFile, SizeOf(szSourceFile)
    'get resource type "JPEG", number rcNumber (1050 in my resource)
    szTypeName           = "JPEG"
    dwResourceType       = IIf(%NULL,%NULL, VarPtr(szTypeName))
    dwResourceName       = IIf(RcNumber ,RcNumber , VarPtr(szRcName))
    ' constants for LoadLibraryEx
    LoadLibExhFile      = %NULL                         ' SDK says make this null. I obey.
    LoadLibExFlags      = %LOAD_LIBRARY_AS_DATAFILE     ' Eliminates the need to resolve any externals (imports) used by target file.
    ErrFlag             = %TRUE                         ' default to fail, set success only on, well, success!
    
    hSource = LoadLibraryEx (szSourceFile, LoadLibExhFile, LoadLibExFlags)
    ' NOTE: LoadLibraryEx with %LOAD_LIBRARY_AS_DATAFILE will fail on Win9x/ME
    ' if the subject file does not contain any resources.
    ' both lResourceType and lResourceName may be either an integer or
    ' a pointer to a null-terminated string based on return of IS_INTRESOURCE.
    If hSource Then
        If dwResourceType < &h010000 Then
             lResourceType   = dwResourceType
        Else
            pAZ              = DwResourceType
            sResourceType    = @pAZ
            lResourceType    = StrPtr(sresourceType)
        End If
    
        If dwResourceName < &h010000 Then
           lResourceName   = dwResourceName
        Else
           pAZ             = DwResourceName
           sResourceName   = @pAZ
           lResourceName   = StrPtr(sResourceName)
        End If
            ' Look for the requested resource in the target file..
        hResFind = FindResource(hSource, ByVal lResourceName, ByVal lResourceType)
        If hResFind Then ' we found it!
           hResLoad        = LoadResource (hSource, hResFind)   ' load it...
           If hResLoad     = %NULL Then Exit Function
           ' Get a pointer to the resource data, and get its size:
           ResSize      =  SizeOfResource (hSource, hResFind)  ' requires the "find" handle
           pResData     =  LockResource   (hResLoad)           ' requires the "load" handle
           ' put the resource data into a dynamic string and set success code.
           If pResData  <> %NULL And ResSize <> %NULL Then
               St       =  Peek$(pResData, ResSize)
               ErrFlag  =  %FALSE   '  SUCCESS!!!
         '  ELSE    ' could not lock resource. Should never happen
           End If
        'ELSE                 ' Requested resource not found in the specified target file
        End If
    End If
    ' clean up. It is not necessary to unlock the resource as per SDK
    If hSource Then FreeLibrary hSource
    If IsTrue ErrFlag Then Function = "" Else Function = St
    End Function
    '==========================================================================================
    '==========================================================================================
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi
Working...
X