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

Posted on 2011-05-06
Medium Priority
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

by:Corey Scheich
ID: 35710978
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 vb.net

LVL 46

Expert Comment

ID: 35711112
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 28

Accepted Solution

Ark earned 2000 total points
ID: 35718034
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 101

Expert Comment

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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

862 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