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

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 _
Who is Participating?
ArkConnect With a Mentor Commented:
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)
Corey ScheichDeveloperCommented:
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
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:

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.