Sign in to follow this  

Writing to binary

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi... I was wondering, if im outputing an integer to a file opened for binary output with vb6, will I be able to read the integer with a c++ program that opens a file for binary input? I know for types like strings, vb is way to high-level, but for bytes,integers and singles(float) , would that be ok? Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by md_lasalle
Hi... I was wondering, if im outputing an integer to a file opened for binary output with vb6, will I be able to read the integer with a c++ program that opens a file for binary input?

I know for types like strings, vb is way to high-level, but for bytes,integers and singles(float) , would that be ok?

Thanks.


In theory it should work but in practice it might not, check the data types and their sizes and compare between C++ and VB. I think some of the data types have different sizes from their C++ counterparts (ints, shorts and longs i believe). If you read and write the same sized (in bytes) information then it will work fine.

HTH

Share this post


Link to post
Share on other sites

Byte -> unsigned char (8 bits)
Integer -> signed short (16 bits)
Long -> signed int (32 bits)
Single -> float (32 bits)
Double -> double (64 bits)

Format, endianess, etc are identical.

As for strings... have a VB app write a single string to a blank file, and then crack it open in a hex editor and decode the format, and write a string-loading wrapper for your C/++ app. Not hard at all.

Share this post


Link to post
Share on other sites
If you want added power in VB, you could always import functions from the Windows API, such as WriteFile().

Here is an example:

What you would import:

Private Declare Function GetStdHandle Lib "kernel32" ( _
ByVal nStdHandle As Long) As Long
Private Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, lpBuffer As Any, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, _
lpOverlapped As Any) As Long
Private Declare Function WriteFile Lib "kernel32" ( _
ByVal hFile As Long, lpBuffer As Any, _
ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, _
lpOverlapped As Any) As Long



Write:

Function StdWrite(ByVal Text As String) As Long
Const STD_OUTPUT_HANDLE = -11&
Dim Handle As Long

Handle = GetStdHandle(STD_OUTPUT_HANDLE)
WriteFile Handle, ByVal Text, Len(Text), StdWrite, ByVal 0&
End Function



Read:

Function StdRead() As String
Const STD_INPUT_HANDLE = -10&
Dim Handle As Long
Dim Buffer As String * 2048
Dim Bytes As Long

Handle = GetStdHandle(STD_INPUT_HANDLE)
Do
ReadFile Handle, ByVal Buffer, Len(Buffer), Bytes, ByVal 0&
If Bytes < Len(Buffer) Then Exit Do
StdRead = StdRead & Buffer
Loop
StdRead = StdRead & Left$(Buffer, Bytes)
End Function



Of course you can impliment that in whatever way you need to, but just know that you have the full power of the Win32 API, even in Visual Basic.

Share this post


Link to post
Share on other sites

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this