unbalanced
unbalanced

Reputation: 1202

VB 6.0 Binary Reading and Writing from VB.NET

I have a vb.net code and want to convert it in vb 6.0. But I have some difficulties. I cant find equivalent of some .net classes

            Dim byteswritten As Integer
            Dim fs As System.IO.FileStream
            Dim r As System.IO.BinaryReader
            Dim CHUNK_SIZE As Integer = 65554
            fs = New System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
            r = New System.IO.BinaryReader(fs)
            Dim FSize As Integer = CType(fs.Length, Integer)
            Dim chunk() As Byte = r.ReadBytes(CHUNK_SIZE)

            While (chunk.Length > 0)
                dmPutStream.Write(chunk, chunk.Length, byteswritten)
                If (FSize < CHUNK_SIZE) Then
                    CHUNK_SIZE = FSize
                    chunk = r.ReadBytes(CHUNK_SIZE)
                Else
                    chunk = r.ReadBytes(CHUNK_SIZE)
                End If
             End While

Well, the document can be big then we used chunk. But I dont know steps for vb 6.0

Such as what i should do for binary reading.

Upvotes: 0

Views: 2347

Answers (2)

dbmitch
dbmitch

Reputation: 5386

Without all your code for opening the write stream and closing the read and write streams, here's an example of how you can do it in VB6 using ADODB.Stream.

Under Project | References, add a reference to ADO Active X Data Objects Library. My version is 6.1, but you should be okay to just choose the latest version - depends on what version of ADO is installed on your system

Hope it helps - more info online if you want to look at all the ADODB.Stream methods and properties

Public Sub StreamData(strWriteFilename As String, filePath As String)

    Const CHUNK_SIZE As Long = 65554

    Dim byteswritten    As Integer
    Dim FSize           As Long

    Dim adofs           As New ADODB.Stream 'Object 'System.IO.FileStream
    Dim varData         As Variant

    ' Include this here - but probably defined elsewhere
    Dim dmPutStream     As New ADODB.Stream

    ' Open Write Stream
    ' *** Looks like you do this elsewhere
    Set dmPutStream = CreateObject("ADODB.Stream")
    With dmPutStream
        .Type = adTypeBinary
        .Open strWriteFilename, adModeWrite
    End With

    ' Open Read strema and start pushing data from it to the write stream
    Set adofs = CreateObject("ADODB.Stream") 'New System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
    With adofs
        .Type = adTypeBinary
        .Open
        .LoadFromFile filePath

        ' Size of Read file - do you want this?
        FSize = .Size

        varData = .Read(CHUNK_SIZE)
        Do While Len(varData) > 0
            dmPutStream.Write varData
            If Not .EOS Then
                varData = .Read(CHUNK_SIZE)
            End If
        Loop

        .Close
    End With

    'Save binary data To disk
    dmPutStream.SaveToFile strWriteFilename, adSaveCreateOverWrite
    dmPutStream.Close

End Sub

Upvotes: 1

Drunken Code Monkey
Drunken Code Monkey

Reputation: 1839

Converting VB.NET to VB6 is a bad idea, and completely unnecessary. If you need to use the VB.NET code from a VB6 application, the best thing to do would be to create a COM-visible wrapper for your .NET library, and call that wrapper from your VB6 application.

You probably CAN convert the code functionally with VB6, but there really is no point. VB.NET is a better language than VB6, use its COM capabilities to save you from writing endless sketchy VB6 code.

If you are dead set on doing this, you will need to reproduce the Stream and Reader classes functionally.

Here is the source for FileStream.cs: http://referencesource.microsoft.com/#mscorlib/system/io/filestream.cs

And for BinaryReader: http://referencesource.microsoft.com/#mscorlib/system/io/binaryreader.cs

Upvotes: 1

Related Questions