?
Solved

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

Posted on 2011-05-06
5
Medium Priority
?
2,260 Views
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 _
)
0
Comment
Question by:GLefering
4 Comments
 
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

http://stackoverflow.com/questions/2586159/does-a-wrapper-class-for-a-com-interop-istream-already-exist

I just used a conversion utility to convert it to vb.net

http://www.developerfusion.com/tools/convert/csharp-to-vb/
0
 
LVL 46

Expert Comment

by:aikimark
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:
http://read.pudn.com/downloads151/sourcecode/graph/texture_mapping/657997/%E9%80%8F%E6%98%8E%E5%8A%A8%E6%80%81%E6%97%B6%E9%92%9F/Classes/cGDIPlus.cls__.htm

0
 
LVL 28

Accepted Solution

by:
Ark earned 2000 total points
ID: 35718034
Hi
You can use API to initialize stream, smth like this.
 
'Declarations
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)

'Using
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

Err_Init:
If Err.Number = 9 Then
    'Uninitialized array
    MsgBox "You must pass a non-empty byte array to this function!"
Else
    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)
etc
0
 
LVL 101

Expert Comment

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

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