Results 1 to 17 of 17

Thread: Q : How Can I Read TextFile From *.Res Without Save It Temporaly In Some Directory ?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    43

    Exclamation Q : How Can I Read TextFile From *.Res Without Save It Temporaly In Some Directory ?

    Q : How Can I Read TextFile From *.Res Without Save It Temporaly In Some Directory ?
    Want i Mean Is The Read TextFile To TextBox directly From Resource File .
    Note That I Can Do This But That Nead To Save And Delete It Temporarly ..!


    Thnx

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Q : How Can I Read TextFile From *.Res Without Save It Temporaly In Some Directory ?

    Yes you can.. loading the resource file is a byte array...
    I added a text file as a custom type and left it default names.. so its 101 in the "CUSTOM" section....

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim DNR() As Byte
    3.     DNR = LoadResData(101, "CUSTOM")
    4.     For x = 0 To UBound(DNR)
    5.         Text1 = Text1 & Chr(DNR(x))
    6.     Next
    7. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    43

    Resolved Thnx

    Thank You That Exactly What I Want !

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    43

    Forget To Mention ..

    I Forget To Mention That I Used
    ChrB() Function Instead Of Chr() Function Beacuse
    The Last One [Chr()] Does Not Work Corectly !

    Thnx

  5. #5
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: Q : How Can I Read TextFile From *.Res Without Save It Temporaly In Some Directory ?

    avoid large numbers of string concentrations. especially when this causes a control to repaint and fire events each time. use a temp string for the concentration. in this case StrConv() should be the fastest way.

    replace
    VB Code:
    1. For x = 0 To UBound(DNR)
    2.         Text1 = Text1 & Chr(DNR(x))
    3.     Next

    with
    VB Code:
    1. Text1 = StrConv(DNR,vbUnicode)

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    43

    Re: Q : How Can I Read TextFile From *.Res Without Save It Temporaly In Some Directory ?

    You're Right Angilaz In Case Of TextBox Direct Using
    But ..In The Case Of Using StrConv() Function Directly ..
    It Seems That It Does NOT Work With Me !

    Here Is The Code That I Use It ..
    Private Sub Command1_Click()
    Dim B() As Byte
    Dim S As String, I As Long
    B = LoadResData(102, "TEXTFILE")

    For I = 0 To UBound(B)
    S = S & ChrB(B(I))
    Next I
    Text1 = S
    End Sub

    It's Work Fine And The Most Important Thing Is
    Using ChrB() Function NOT Chr() !

    Thank You For You Reply Post !

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    43

    Re: Q : How Can I Read TextFile From *.Res Without Save It Temporaly In Some Director

    Also I Use This Function :

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Text1 = ReadResTextFile(103, "TEXTFILE")
    End Sub
    
    Function ReadResTextFile(ID As Long, Optional Folder As String = "CUSTOM") As String
    '****************************************************************
    '  Name     : ReadResTextFile
    '  Date     : 2006-06-24
    '  Type     : Function
    '  By       : Muhammed_KSA [Muhammed Al Ansari]
    '****************************************************************
    '  This Function Is To Read Any TextFile Directly [*.txt;*.CSV;..]
    '  From Resource File !
    '****************************************************************
    
    'Declaration
    Dim B() As Byte
    Dim I As Long
    Dim BufferText As String
    'Definition
    B = LoadResData(ID, Folder)
    For I = 0 To UBound(B)
    BufferText = BufferText & StrConv(ChrB(B(I)), vbUnicode)
    Next I
    ReadResTextFile = BufferText
    End Function

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    43

    Smile GIF Or JPG

    I Want A Code But This Time Is To Show The JPG Directly
    On PictureBox ?

  9. #9
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: Q : How Can I Read TextFile From *.Res Without Save It Temporaly In Some Directory ?

    there's a good solution on Planet Source Code:

    Planet Source Code


  10. #10
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: GIF Or JPG

    Quote Originally Posted by Muahmed_KSA
    I Want A Code But This Time Is To Show The JPG Directly
    On PictureBox ?
    You can extract a jpg from a resource file but you have to store it in a temp.jpg to show it. Load a jpg as a Custom and give it a JPG name and a number, in this case its 101.
    VB Code:
    1. Option Explicit
    2.  
    3. Dim Ap As String
    4. Dim ResFile As String
    5.  
    6. Private Sub Form_Load()
    7.  
    8. Dim bArr() As Byte
    9. Dim FileNum As Integer
    10.  
    11. If Right$(App.Path, 1) = "\" Then
    12.     Ap = App.Path
    13. Else
    14.     Ap = App.Path & "\"
    15. End If
    16. ResFile = Ap & "Temp.jpg"
    17. FileNum = FreeFile()
    18. Open ResFile For Binary As #FileNum
    19. bArr = LoadResData(101, "JPG") 'change this to reflect the correct ID
    20. Put #FileNum, , bArr
    21. Close #FileNum
    22. Picture1.Picture = LoadPicture(ResFile)
    23.  
    24. End Sub
    25.  
    26. Private Sub Form_Unload(Cancel As Integer)
    27.  
    28. Kill ResFile
    29. End
    30.  
    31. End Sub
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  11. #11

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    43

    Re: Q : How Can I Read TextFile From *.Res Without Save It Temporaly In Some Directory ?

    This Is Good Example And Can Be Used For Any File Type
    But I Want A Way That NOT Use Tem File At All..
    As I Do With TextFile !
    Can You Help ?

  12. #12

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    43

    Re: Q : How Can I Read TextFile From *.Res Without Save It Temporaly In Some Directory ?

    The Link On Planet Source Code Moved To This Link ..
    http://www.planet-source-code.com/vb...29004&lngWId=1

    Thank You Agilaz !

  13. #13
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Q : How Can I Read TextFile From *.Res Without Save It Temporaly In Some Director

    Did you get it .. i just got it to work by appending an image to my EXE then extracting it when the EXE is loaded .. it doesnt require any file ..

  14. #14
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Q : How Can I Read TextFile From *.Res Without Save It Temporaly In Some Director

    Here it is .. modified from the link you sent and some code i found elsewhere.

    You will need to compile both the AppendImage project and the prjImage Project .. prjImage makes an exe called Test.exe .. this is the main program.

    Once that is done .. click the Append_Image.exe and that will add the image "garbage.jpg" to the Test.exe file ..

    Now you can copy test.exe anywhere and it will load the image from itself ..

    Note that you can add multiple files to the exe .. and extract them all either in this method or just extract to files somewhere else .. Im working on compressing files added now ..

    Rory
    Last edited by rory; Jun 24th, 2006 at 05:25 PM.

  15. #15

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    43

    Re: Q : How Can I Read TextFile From *.Res Without Save It Temporaly In Some Directory ?

    I Developed A Class To Read Images & TextFiles From Resorce File
    Without Save It A Temporary File [Which Could Cause Some Error Some Times]

    VB Code:
    1. '****************************************************************
    2. '  Name     : CResLoader
    3. '  Date     : 2006-06-25
    4. '  Type     : Class
    5. '  By       : Muhammed Al Ansari
    6. '****************************************************************
    7. 'Use Example:
    8. 'Private Sub Command1_Click()
    9. 'Dim X As New CResLoader
    10. 'Text1 = X.ReadResTextFile(103, "TEXTFILE")
    11. 'Me.Picture1 = X.ReadImage(101, "JPG")
    12. 'End Sub
    13. '****************************************************************
    14. Private Declare Function CreateStreamOnHGlobal Lib "ole32" (ByVal hGlobal As Long, ByVal fDeleteOnRelease As Long, ppstm As Any) As Long
    15. Private Declare Function OleLoadPicture Lib "olepro32" (pStream As Any, ByVal lSize As Long, ByVal fRunmode As Long, riid As Any, ppvObj As Any) As Long
    16. Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Any, pclsid As Any) As Long
    17. Private Declare Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal dwBytes As Long) As Long
    18. Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    19. Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    20. Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
    21.  
    22. Private Function PictureFromByteStream(B() As Byte) As IPicture
    23.     Dim LowerBound As Long
    24.     Dim ByteCount  As Long
    25.     Dim hMem  As Long
    26.     Dim lpMem  As Long
    27.     Dim IID_IPicture(15)
    28.     Dim istm As stdole.IUnknown
    29.  
    30.     On Error GoTo Err_Init
    31.     If UBound(B, 1) < 0 Then
    32.         Exit Function
    33.     End If
    34.    
    35.     LowerBound = LBound(B)
    36.     ByteCount = (UBound(B) - LowerBound) + 1
    37.     hMem = GlobalAlloc(&H2, ByteCount)
    38.     If hMem <> 0 Then
    39.         lpMem = GlobalLock(hMem)
    40.         If lpMem <> 0 Then
    41.             MoveMemory ByVal lpMem, B(LowerBound), ByteCount
    42.             Call GlobalUnlock(hMem)
    43.             If CreateStreamOnHGlobal(hMem, 1, istm) = 0 Then
    44.                 If CLSIDFromString(StrPtr("{7BF80980-BF32-101A-8BBB-00AA00300CAB}"), IID_IPicture(0)) = 0 Then
    45.                   Call OleLoadPicture(ByVal ObjPtr(istm), ByteCount, 0, IID_IPicture(0), PictureFromByteStream)
    46.                 End If
    47.             End If
    48.         End If
    49.     End If
    50.    
    51.     Exit Function
    52.    
    53. Err_Init:
    54.     If Err.Number = 9 Then
    55.         'Uninitialized array
    56.         MsgBox "You must pass a non-empty byte array to this function!"
    57.     Else
    58.         MsgBox Err.Number & " - " & Err.Description
    59.     End If
    60. End Function
    61.  
    62. Function ReadImage(ID As Long, Optional Folder As String = "CUSTOM") As IPicture
    63. Dim B() As Byte
    64.  
    65. B = LoadResData(ID, Folder)
    66. Set ReadImage = PictureFromByteStream(B)
    67. End Function
    68. Function ReadResTextFile(ID As Long, Optional Folder As String = "CUSTOM") As String
    69. '****************************************************************
    70. '  Name     : ReadResTextFile
    71. '  Date     : 2006-06-24
    72. '  Type     : Function
    73. '  By       : Muhammed_KSA [Muhammed Al Ansari]
    74. '****************************************************************
    75. '  This Function Is To Read Any TextFile Directly [*.txt;*.CSV;..]
    76. '  From Resource File !
    77. '****************************************************************
    78.  
    79. 'Declaration
    80. Dim B() As Byte
    81. Dim I As Long
    82. Dim BufferText As String
    83. 'Definition
    84. B = LoadResData(ID, Folder)
    85. For I = 0 To UBound(B)
    86. BufferText = BufferText & StrConv(ChrB(B(I)), vbUnicode)
    87. Next I
    88. ReadResTextFile = BufferText
    89. End Function

    Thank You [ALL] For Your Help !

  16. #16

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    43

    Don't Forget ..

    Plz Don't To Forget To Rate This Thread !


    Thnk You


  17. #17

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    43

    Thanks

    Thank You All For Your Replies !

    Muhamed Al Ansari

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