Results 1 to 5 of 5

Thread: Resource File Trouble (.bat)

  1. #1

    Thread Starter
    Fanatic Member cid's Avatar
    Join Date
    Nov 2002
    Location
    Fort Worth, Texas
    Posts
    854

    Question Resource File Trouble (.bat)

    I want to insert a batch file into my vb project. So I thought a resource file would be easiest. The thing is I dont know the code for a .bat resource file. Please enlighten me on how to insert a .bat resource file.

    www.google.com - Pay Tribute.

    Always Listening To: Thrice

  2. #2
    Hyperactive Member Simon Caiger's Avatar
    Join Date
    Aug 2000
    Location
    Rugby, England
    Posts
    377
    Something a bit like this :

    1. Create a text file containing the script for your batch file.

    2. Put your text file in the resource file under the Custom directory

    3. In your program retrieve the file from the resource file using LoadTextResource or just SaveResItemtoDisc and the following functions.

    VB Code:
    1. Public Function LoadTextResource(ByVal ResourceID As String, ByVal sResourceType As String, Optional TempFile) As String
    2.     'Returns a text file from a resource file.
    3.     'EXAMPLE CALL:
    4.     'Text1 = LoadTextResource(101, "Custom", "C:\temp\temp.tmp")
    5.    
    6.     Dim sFilename As String
    7.     Dim iFileNum As Integer
    8.     Dim sText As String
    9.    
    10.     'Check if the TempFile Name has been specified
    11.     If IsMissing(TempFile) Then
    12.         'Create a temp file name such as "~res1234.tmp"
    13.         GetTempFile "", "~rs", 0, sFilename
    14.     Else
    15.         'Use the specified temp file name
    16.         sFilename = TempFile
    17.     End If
    18.    
    19.     'Save the resource item to disk
    20.     If SaveResItemToDisk(ResourceID, sResourceType, sFilename) = 0 Then
    21.         iFileNum = FreeFile     'Get free file handle
    22.         Open sFilename For Input As iFileNum    'Open the file
    23.         LoadTextResource = Input(LOF(iFileNum), iFileNum)  'Read all of the text from the file
    24.         Close iFileNum
    25.         Kill sFilename          'Delete the temp file
    26.     End If
    27.    
    28. End Function
    29.  
    30. Public Function SaveResItemToDisk(ByVal iResourceNum As String, ByVal sResourceType As String, ByVal sDestFileName As String) As Long
    31.     'Saves a resource item to disk
    32.     'Returns 0 on success, error number on failure
    33.     'Example Call:
    34.     'iRetVal = SaveResItemToDisk(101, "CUSTOM", "C:\myImage.gif")
    35.    
    36.     Dim bytResourceData()   As Byte
    37.     Dim iFileNumOut         As Integer
    38.    
    39.     On Error GoTo SaveResItemToDisk_err
    40.    
    41.     'Retrieve the resource contents (data) into a byte array
    42.     bytResourceData = LoadResData(iResourceNum, sResourceType)
    43.    
    44.     'Get Free File Handle
    45.     iFileNumOut = FreeFile
    46.    
    47.     'Open the output file
    48.     Open sDestFileName For Binary Access Write As #iFileNumOut
    49.        
    50.         'Write the resource to the file
    51.         Put #iFileNumOut, , bytResourceData
    52.    
    53.     'Close the file
    54.     Close #iFileNumOut
    55.    
    56.     'Return 0 for success
    57.     SaveResItemToDisk = 0
    58.    
    59.     Exit Function
    60. SaveResItemToDisk_err:
    61.     'Return error number
    62.     SaveResItemToDisk = Err.Number
    63. End Function
    64.  
    65. Public Function GetTempFile(ByVal strDestPath As String, ByVal lpPrefixString As String, ByVal wUnique As Integer, lpTempFilename As String) As Boolean
    66.     ' Get a temporary filename for a specified drive and filename prefix
    67.     ' PARAMETERS:
    68.     '   strDestPath - Location where temporary file will be created.  If this
    69.     '                 is an empty string, then the location specified by the
    70.     '                 tmp or temp environment variable is used.
    71.     '   lpPrefixString - First three characters of this string will be part of
    72.     '                    temporary file name returned.
    73.     '   wUnique - Set to 0 to create unique filename.  Can also set to integer,
    74.     '             in which case temp file name is returned with that integer
    75.     '             as part of the name.
    76.     '   lpTempFilename - Temporary file name is returned as this variable.
    77.     ' RETURN:
    78.     '   True if function succeeds; false otherwise
    79.    
    80.     If strDestPath = "" Then
    81.         ' No destination was specified, use the temp directory.
    82.         strDestPath = String(255, vbNullChar)
    83.         If GetTempPath(255, strDestPath) = 0 Then
    84.             GetTempFile = False
    85.             Exit Function
    86.         End If
    87.     End If
    88.     lpTempFilename = String(255, vbNullChar)
    89.     GetTempFile = GetTempFilename(strDestPath, lpPrefixString, wUnique, lpTempFilename) > 0
    90.     lpTempFilename = StripTerminator(lpTempFilename)
    91. End Function
    92.  
    93. Public Function StripTerminator(ByVal strString As String) As String
    94.     ' Returns a string without any zero terminator.  Typically,
    95.     ' this was a string returned by a Windows API call.
    96.     '
    97.     ' IN: [strString] - String to remove terminator from
    98.     '
    99.     ' Returns: The value of the string passed in minus any
    100.     '          terminating zero.
    101.    
    102.     Dim intZeroPos As Integer
    103.  
    104.     intZeroPos = InStr(strString, Chr$(0))
    105.     If intZeroPos > 0 Then
    106.         StripTerminator = Left$(strString, intZeroPos - 1)
    107.     Else
    108.         StripTerminator = strString
    109.     End If
    110. End Function
    Simon Caiger

    Documentation is like sex; when it's good, it's very, very good, and when it's bad, it's better than nothing.

  3. #3
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    Working with Resource Files


    A resource file allows you to collect all of the version-specific text and bitmaps for an application in one place. This can include icons, screen text, and other material that may change between localized versions or between revisions or specific configurations.

    Adding Resources to a Project
    You can create a resource file using the Resource Editor add-in. The compiled resource file will have a .res file name extension. Each project can contain only one resource file.

    The actual file consists of a series of individual strings, bitmaps, or other items, each of which has a unique identifier. The identifier is either a Long or a String, depending on the type of data represented by the resource. Strings, for example, have a Long identifier, while bitmaps have a Long or String identifier. To retrieve resources in your code, learn the identifier for each resource. The function parameters referring to the resources can use the Variant data type.

    To add a new resource file to your project

    Choose Resource Editor from the Tools menu. An empty resource file will be opened in the Resource Editor window.
    Note The Resource Editor add-in must be installed. For information on installing add-ins, see "Using Wizards and Add-Ins" in "Managing Projects".

    Select the Save button on the Resource Editor toolbar to save the resource file. The file will be added to the Project Explorer under the Related Documents section.
    To add an existing resource file to your project

    Choose Add New Resource File from the Project menu. Any existing resource file in your project will be replaced.
    Caution If you make any modifications to an existing resource file it could affect other projects that use that resource file. Make sure that you save the file under a new filename.

    Note The Resource Editor add-in must be installed. For information on installing add-ins, see "Using Wizards and Add-Ins" in "Managing Projects."

    For More Information For more information on resource files, see "Using Resource Files for Localization" in "International Issues."

    Note Windows resource files are specific to 16-bit or 32-bit applications. Visual Basic will generate an error message if you try to add a 16-bit resource file to a project.

    Using Resources in Code
    Visual Basic provides three functions for retrieving data from the resource file for use in code.

    Function Description
    LoadResString Returns a text string.
    LoadResPicture Returns a Picture object, such as a bitmap, icon, or cursor.
    LoadResData Returns a Byte array. This is used for .wav files, for example.


    For More Information See the appropriate function topic.


    From help files



    Seahag

  4. #4

    Thread Starter
    Fanatic Member cid's Avatar
    Join Date
    Nov 2002
    Location
    Fort Worth, Texas
    Posts
    854

    I have no clue how to use that code

    Do you think you could give me an attachment with an example for me to go off of so I know what Im doing?
    Have the batch file do this:
    @echo off
    echo VB
    pause > nul Press any key to succeed with this batch file...
    cls

    www.google.com - Pay Tribute.

    Always Listening To: Thrice

  5. #5
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width