Binary Baloney Baffling Baseball Buff

Posted on 2002-06-13
Last Modified: 2010-05-02
I am creating a pdf file on a server and returning the binary stream of pdf formatted data via a function return in VB to a middle tier DLL.  When I get the stream on the receiving end I open up a binary file and then dump the info into that file and close it.   It works fine with one exception - it seems to want to write out four bytes at the beginning of the file which break my logic.  These characters are (Hex)  08-00-9C-35
They are not part of the stream.
Can any body tell me what is going on here and how I can prevent it from happening:

the code is:

datastream = object.returnstream(arg1,arg2)

open "c:\junk.pdf" for binary as #1  (i also tried specifying the len as the length of the datastream)

put #1, , datastream   ( I also tried put #1,1,datastream
close #1

Question by:sodakotahusker
  • 3
  • 2
LVL 22

Expert Comment

ID: 7076717
Just guessing, but is the file/datastream 538012 bytes long?

08-00-9C-35 could be read as a low-order double-byte number: &H0008359C = 538012

If so, it's putting the file length at the beginning of the file stream.

You could also try this instead:

open "c:\junk.pdf" for binary as #1  (i also tried specifying the len as the length of the datastream)

print datastream;
close #1

Author Comment

ID: 7076760
Mystery solved.
I was using a variant to hold the stream coming in.  Aparently the length information about the stream was being stored in front of the actual stream.  I changed to a string variable and life is good again!
LVL 16

Expert Comment

ID: 7076762
Could you put datasream in an array of bytes, then write down to file open in binary mode?
You could dimension the array first if you don't want to use a dynamic one.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 22

Expert Comment

ID: 7076787
sodakotahusker, did my comment help you to get there?  (I correctly(?) identified the string as length...)  If so, feel free to accept that comment as your answer.

Author Comment

ID: 7076806

As a matter of a fact it did.  I was wondering how to do this.  How about you copy my solution and make it a comment so I can accept that answer so people who find in the future might not spend hours over this silly issue like I did.  
LVL 22

Accepted Solution

rspahitz earned 50 total points
ID: 7076812

After examining the hex numbers, it occurred to me that these translate into the length of the datastream. (low order, double-byte: 08 00 9C 35 -> &H0008359C = decimal 538012.)

When you investigated this, you found that the length WAS being added because the datastream variable is actually defined as a *variant* rather than as a *string*.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

713 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