How can I use iStream in VB6 to write to a file

Posted on 2011-05-06
Last Modified: 2012-06-27
Hi, can does anyone have an example for me for using iStream in VB6!! to write data to file? I have an external object that has a SaveSample method that uses iStream as a parameter. I can define a variable Dim oiStream as <ExternalClass>.iStream, but don't know how to go from there. This <ExternalClass>.iStream type has the following methods, which are quite common I think: Clone, Commit, LockRegion, RemoteCopyTo, RemoteRead, RemoteSeek, RemoteWrite, Revert, SetSize, Stat, UnlockRegion.
Don't know how to implement this. An few example lines would be great! Thanks!
Function definition of the SaveSample function is:
Public Sub SaveSample( _
   ByVal pStream As IStream, _
   Optional ByVal bCompressed As Boolean = True _
Question by:GLefering
    LVL 13

    Expert Comment

    I know you are looking for VB6, I used the following example to read and write an iStream from vb.Net

    I just used a conversion utility to convert it to
    LVL 44

    Expert Comment

    I've never used this, but there does appear to be a WinAPI that will create a stream object.  Here is a VB classic example:

    LVL 27

    Accepted Solution

    You can use API to initialize stream, smth like this.
    Private Declare Function CreateStreamOnHGlobal Lib "ole32" (ByVal hGlobal As Long, ByVal fDeleteOnRelease As Long, ppstm As Any) As Long
    Private Declare Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal dwBytes As Long) As Long
    Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
    Public Function IStreamFromByteArray(b() As Byte) As Object
    ' or Public Function IStreamFromByteArray(b() As Byte) As <ExternalClass>.iStream if it's std ole stream
    Dim LowerBound As Long
    Dim ByteCount As Long
    Dim hMem As Long
    Dim lpMem As Long
    Dim istm As stdole.IUnknown
    'or Dim istm As <ExternalClass>.iStream
    On Error GoTo Err_Init
    LowerBound = LBound(b)
    ByteCount = (UBound(b) - LowerBound) + 1
    hMem = GlobalAlloc(&H2, ByteCount)
    If hMem <> 0 Then
       lpMem = GlobalLock(hMem)
       If lpMem <> 0 Then
           MoveMemory ByVal lpMem, b(LowerBound), ByteCount
           Call GlobalUnlock(hMem)
           If CreateStreamOnHGlobal(hMem, 1, istm) = 0 Then
               Set  IStreamFromByteArray = istm
           End If
       End If
    End If
    Exit Function
    If Err.Number = 9 Then
        'Uninitialized array
        MsgBox "You must pass a non-empty byte array to this function!"
        MsgBox Err.Number & " - " & Err.Description
    End If
    End Function

    Open in new window

    But YMHO correct way is to initialize stream in your <ExternalClass>. Since VB6 doesn't support New constructors, this class need some subs like Init(b() As Byte)
    or Init(filePath as string)
    or Init(picture as OLEPicture)
    LVL 100

    Expert Comment

    This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now