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

PB/Win 7.0 User-Defined Resource Demo

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

  • PB/Win 7.0 User-Defined Resource Demo

    RESOURCE SCRIPT FOLLOWS IN NEXT MESSAGE
    Code:
    ' =======================================================================
    ' FILE: EZ_RES.bas
    ' 1/26/03
    ' Using user-defined resources to store strings and getting them back easily.
    ' Returned string can be used for anything.
    ' 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 7.0
    ' Win32API.INC  Date : May 9, 2002
    ' Author             : Michael Mattias, Racine WI USA
    ' Copyright/Use      : Placed in the public domain by the author 1/26/03
    ' ========================================================================
    
    #COMPILE EXE
    #REGISTER NONE
    #DEBUG ERROR ON
    #RESOURCE "EZ_RES.PBR"
    '=====[Windows API Header Files] ============================
    '  If you don't need all of the functionality supported by these APIs
    '  (and who does?), you can selectively turn off various modules by putting
    '  the following constants in your program BEFORE you #include "win32api.inc":
    '
    '  %NOGDI = 1     ' no GDI (Graphics Device Interface) functions
    '  %NOMMIDS = 1   ' no Multimedia ID definitions
    
    %NOMMIDS = 1
    %NOGDI   = 1      ' this program does not use any GDI, but that is a rare "SDK" program
    #INCLUDE "WIN32API.INC"
    '==[End Windows API Header Files]============================
    
    ' UTILITY FUNCTION.. used in main and in ResourceAsString
    FUNCTION IS_INTRESOURCE(BYVAL wVal AS DWORD) AS LONG
    ' Must return TRUE  if all bits are zero except least 16
    ' Must return FALSE if any bit 17+ is on
      LOCAL W AS DWORD
      W =  (wVal < &h010000)  ' if this is true, all bits 17+ are zero
      FUNCTION = W
    END FUNCTION
    
    ' -----------------------------------------------------------------------------
    ' GET RESOURCE DATA FROM AN EXECUTABLE FILE  AS A DYNAMIC STRING
    ' Returns: Non-zero length string on success, null string on failure.
    ' I have commented out - but left in - most of the 'ELSE' clauses in the interest of
    ' showing the various error checks which could be added if desired.
    ' -----------------------------------------------------------------------------
    FUNCTION ResourceAsString (szSourceFile AS ASCIIZ,_
                               BYVAL dwResourceType AS DWORD, _
                               BYVAL dwResourceName AS DWORD) AS STRING
                               
      LOCAL LoadLibExhFile AS LONG, LoadLibExFlags AS LONG
      LOCAL ErrFlag AS LONG
    
      LOCAL hSource AS LONG, hResFind AS LONG, hResLoad AS LONG, pResData AS BYTE PTR, ResSize AS LONG
      LOCAL lResourceName AS DWORD,  lResourceType AS DWORD
      LOCAL sResourceName AS STRING, sResourceType AS STRING
      LOCAL w AS STRING, pAZ AS ASCIIZ PTR
     
      ' 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!
    
      IF DIR$(szSourceFile) > "" THEN
          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.
          ' But that's OK in this function, because the resource
          ' for which we search is obviously not in this file!
          ' 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 IS_IntResource(dwResourceType) THEN
                 lResourceType   = dwResourceType
            ELSE
                pAZ              = DwResourceType
                sResourceType    = @pAZ
                lResourceType    = STRPTR(sresourceType)
            END IF
    
            IF IS_IntResource(dwResourceName) 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
                   MSGBOX "Could not load Resource, should not happen. Ever. "
               END IF
               ' 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
                   w        =  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
       ' ELSE
            ' cannot loadlibrary on specified target file. Might be "resourceless" file
            ' whilst running on Win 9x/ME
        END IF
     ' ELSE
          ' Specified target file not found
      END IF
      ' clean up. It is not necessary to unlock the resource as per SDK
      IF hSource THEN
          FreeLibrary hSource
      END IF
      
      IF ErrFlag THEN    ' was set to failure at start, set to zero only on success
          W = ""
      END IF
      ' return a null string on failure, the resource data on success.
      FUNCTION = W
    
    END FUNCTION
    ' ===================================================================================
    '  DEMO PROGRAM ENTRY POINT
    ' Use function to return user-defined resources in the form of a string. Note that we
    ' always set the names and types, even if null or zero. Less confusing that way
    ' ===================================================================================
    FUNCTION PBMAIN() AS LONG
    
       LOCAL dwResourceType AS DWORD  , dwResourceName AS DWORD
       LOCAL rcTypeNum      AS LONG   , szTypeName   AS ASCIIZ * 128
       LOCAL rcNumber       AS LONG   , szRcName     AS ASCIIZ * 128
       LOCAL w              AS STRING
       LOCAL szSourceFile   AS ASCIIZ * %MAX_PATH
       
      ' For this demo we'll look inside ourself for the target resources
       GetModuleFileName BYVAL %NULL, szSourceFile, SIZEOF(szSourceFile)
       
       ' get resource type "TYPE1", number 100
       rcTypeNum            = %NULL
       szTypeName           = "TYPE1"
       rcNumber             = 100&
       szRcName             = ""
       dwResourceType       = IIF(RcTypeNum,RcTypeNum, VARPTR(szTypeName))
       dwResourceName       = IIF(RcNumber ,RcNumber , VARPTR(szRcName))
       W                    = ResourceAsString (szSourceFile, dwResourceType, dwResourceName)
       MSGBOX                 w, , "Named Type, numbered ID"
       
       ' get resource type "TYPE1", name "TYPE1NAME"
       rcTypeNum            = %NULL
       szTypeName           = "TYPE1"
       rcNumber             = %NULL
       szRcName             = "TYPE1NAME"
       dwResourceType       = IIF(RcTypeNum,RcTypeNum, VARPTR(szTypeName))
       dwResourceName       = IIF(RcNumber ,RcNumber , VARPTR(szRcName))
       W                    = ResourceAsString (szSourceFile, dwResourceType, dwResourceName)
       MSGBOX                 w, , "Named Type, Named ID"
       
       ' get resource type 300, number 201
       rcTypeNum            = 300&
       szTypeName           = ""
       rcNumber             = 201&
       szRcName             = ""
       dwResourceType       = IIF(RcTypeNum,RcTypeNum, VARPTR(szTypeName))
       dwResourceName       = IIF(RcNumber ,RcNumber , VARPTR(szRcName))
       W                    = ResourceAsString (szSourceFile, dwResourceType, dwResourceName)
       MSGBOX                 w, , "Numbered Type, Numbered ID"
       
       ' get resource type 300, name "TYPE300NAME"
       rcTypeNum            = 300&
       szTypeName           = ""
       rcNumber             = %NULL
       szRcName             = "TYPE300NAME"
       dwResourceType       = IIF(RcTypeNum,RcTypeNum, VARPTR(szTypeName))
       dwResourceName       = IIF(RcNumber ,RcNumber , VARPTR(szRcName))
       W                    = ResourceAsString (szSourceFile, dwResourceType, dwResourceName)
       MSGBOX                 w, , "Numbered Type, Named ID"
    
    END FUNCTION   ' pbmain
    ' ENDS

    ------------------
    Michael Mattias
    Tal Systems Inc.
    Racine WI USA
    mailto:[email protected][email protected]</A>
    www.talsystems.com
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

  • #2
    RESOURCE SCRIPT FILE
    Code:
    //EZ_RES.RC
    // Demo Resource Script.
    // 1/25/03
    // Public Domain Courtesy Michael Mattias
    // Note that NUMBERED user-defined resource TYPEs must use a number
    // in the range 256-65535 (decimal), as 1-255 are reserved by Windows(r).
    // User-defined resource ID numbers must be in the range 1-65535
    
    100            TYPE1
     BEGIN
      "Named Type 'TYPE1', Numbered ID = 100"
     END
    
    TYPE1NAME      TYPE1
     BEGIN 
       "Named Type 'TYPE1', Named ID = 'TYPE1NAME'"
     END
    
    201           300
     BEGIN
      "Numbered Type=300, Numbered ID = 201"
     END
    
    TYPE300NAME   300
     BEGIN
      "Numbered Type=300, Named ID = 'TYPE300NAME'"
     END
    //  ENDS //
    MCM


    ------------------
    Michael Mattias
    Tal Systems Inc.
    Racine WI USA
    mailto:[email protected][email protected]</A>
    www.talsystems.com

    [This message has been edited by Michael Mattias (edited January 27, 2003).]
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Thanks. That looks useful.

      Barry

      Comment


      • #4
        Deleted. See Programming forum.
        Last edited by Egbert Zijlema; 28 Jan 2010, 04:55 AM. Reason: Text deleted, wrong forum

        Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
        http://zijlema.basicguru.eu
        *** Opinions expressed here are not necessarily untrue ***

        Comment

        Working...
        X