aspose file tools*
The moose likes I/O and Streams and the fly likes Deserializing Visual Basic 6 files in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Deserializing Visual Basic 6 files in Java?" Watch "Deserializing Visual Basic 6 files in Java?" New topic
Author

Deserializing Visual Basic 6 files in Java?

Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Okay, so I have to be able to read files that have been serialized in VB. 2 things, 1 they are serialized in Binary, and I tried to look up on how to serialize/deserialize in Binary, but didn't have much help...

Also in the VB code they talk about "TYPES" which seems to be to just be a class full of stuff...

Any information would be appreciated, any questions please ask,

thanks,

~KZ.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jay Orsaw wrote:they are serialized in Binary, and I tried to look up on how to serialize/deserialize in Binary, but didn't have much help...


"Binary" is not one single format. word files, Excel files, Java's .class files, .exe files--they're all binary and they're all very different. Java's serialization mechanism is also binary, but that doesn't mean it can read any of those other formats or whatever format your VB files use.

All "binary" really means in terms of file formats is "may contain bytes that don't translate into textual characters."

You have two basic approaches:

1) Search for a third-party Java library that knows how to read that VB6 format.

2) Find the specs for that format and write your own deserialization code.
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Ah okay, that makes sense! I cannot open the .ser files when trying to in notepad...

I have the VB6 code, so judging by that comment I can read the VB6 files then if I know the format... Thank you!
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356



I copied everything and made my own class. The global variables aren't in order however(do they need to be?) I made a constructor with the parameters of each and then assigned the constructor parameters to the global variables IN EXACT ORDER of the VB6 serialized file.




The error appears when reading the file... hmmm...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jay Orsaw wrote:
The error appears when reading the file... hmmm...


What file? Are you trying to read the VB6 file with Java's ObjectInputStream? You can't do that. That would only work if Java's serialization format was the same as that VB6 format, which is phenomenally unlikely. Just writing a Java class that's the structural equivalent of the VB6 class doesn't magically make Java able to understand the VB6 format.
toddy shed
Greenhorn

Joined: Jan 28, 2012
Posts: 1
Jeff Verdegan wrote:
Jay Orsaw wrote:they are serialized in Binary, and I tried to look up on how to serialize/deserialize in Binary, but didn't have much help...


"Binary" is not one single format. Word files, Excel files, Java's .class files, .exe files--they're all binary and they're all very different. Java's serialization mechanism is also binary, but that doesn't mean it can read any of those other formats or whatever format your VB files use.

All "binary" really means in terms of file formats is "may contain bytes that don't translate into textual characters."

You have two basic approaches:

1) Search for a third-party Java library that knows how to read that VB6 format.

2) Find the specs for that format and write your own deserialization code.




thanks for the above knowledge of java,its really very nice to give information of java mixed with visual basic .I want to ask a question that is it possible to have java function enabled in visual basic



_____________
Speaking English Uk
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Jeff Verdegan wrote:
Jay Orsaw wrote:
The error appears when reading the file... hmmm...


What file? Are you trying to read the VB6 file with Java's ObjectInputStream? You can't do that. That would only work if Java's serialization format was the same as that VB6 format, which is phenomenally unlikely. Just writing a Java class that's the structural equivalent of the VB6 class doesn't magically make Java able to understand the VB6 format.


That is what I'm asking.

2) Find the specs for that format and write your own deserialization code.

What did you mean by that? I thought you meant if I knew the code in the VB file, I would be able to do it.

How am I able to read a VB6 file? Is it possible? Nothing on google at all, this question itself is on Google's first page...

I saw something called CORBA that had to do with mixing coding platforms, though I'm not sure if that will work...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

Jay Orsaw wrote:That is what I'm asking.

2) Find the specs for that format and write your own deserialization code.

What did you mean by that? I thought you meant if I knew the code in the VB file, I would be able to do it.

How am I able to read a VB6 file? Is it possible? Nothing on google at all, this question itself is on Google's first page...


There's no such thing as "a VB6 file". Or rather, VB code can write anything it likes into a file. So for example it might write a 32-character string followed by two integer values (at four bytes each). Or it might write a long integer value (eight bytes) followed by a single character followed by a short integer value (two bytes). The possibilities are endless. So what you have to do -- as already stated -- is to find out what is in that file. How it is structured. The format of the file, in other words. The answer is going to be something like one of the two examples I just mentioned, only probably more complicated than that.

If you don't have anybody to tell you what the format is then you're probably out of luck. Unless you have a copy of the code which produces the file, in which case you might have a chance at figuring it out.
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Paul Clapham wrote:
Jay Orsaw wrote:That is what I'm asking.

2) Find the specs for that format and write your own deserialization code.

What did you mean by that? I thought you meant if I knew the code in the VB file, I would be able to do it.

How am I able to read a VB6 file? Is it possible? Nothing on google at all, this question itself is on Google's first page...


There's no such thing as "a VB6 file". Or rather, VB code can write anything it likes into a file. So for example it might write a 32-character string followed by two integer values (at four bytes each). Or it might write a long integer value (eight bytes) followed by a single character followed by a short integer value (two bytes). The possibilities are endless. So what you have to do -- as already stated -- is to find out what is in that file. How it is structured. The format of the file, in other words. The answer is going to be something like one of the two examples I just mentioned, only probably more complicated than that.

If you don't have anybody to tell you what the format is then you're probably out of luck. Unless you have a copy of the code which produces the file, in which case you might have a chance at figuring it out.


thank you, as I have already mentioned I HAVE THE FILE, now what do I do with it? I have the format and I made it into a class but the problem is I cannot read it in with the objectinputsteam, as stated above I get an error. What should I do to read in the file since the OIS isn't working?

There are 3 TYPES each type has a set of objects like "String" for example. I set up the first TYPE as 1 class called planHead(Same as in the Vb code in the file..) But as you can see in the problem above I am stuck at reading in the file to try and deserialize it, so that's why I ask if there is a different way to read it in..?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

Jay Orsaw wrote:thank you, as I have already mentioned I HAVE THE FILE, now what do I do with it? I have the format and I made it into a class but the problem is I cannot read it in with the objectinputsteam, as stated above I get an error. What should I do to read in the file since the OIS isn't working?

There are 3 TYPES each type has a set of objects like "String" for example. I set up the first TYPE as 1 class called planHead(Same as in the Vb code in the file..) But as you can see in the problem above I am stuck at reading in the file to try and deserialize it, so that's why I ask if there is a different way to read it in..?


I guess this hasn't been explicitly stated, so let me start by pointing out that you can't use Java serialization, because that isn't how the file is formatted.

But you have the specs about what is in the file (right? That was what you meant when you said you "have the format", was it?) so the next step is to write some Java code which reads the file. If the specs include binary data which corresponds to Java types then you could use a DataInputStream to read that data.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jay Orsaw wrote:
Jeff Verdegan wrote:
Jay Orsaw wrote:
The error appears when reading the file... hmmm...


What file? Are you trying to read the VB6 file with Java's ObjectInputStream? You can't do that. That would only work if Java's serialization format was the same as that VB6 format, which is phenomenally unlikely. Just writing a Java class that's the structural equivalent of the VB6 class doesn't magically make Java able to understand the VB6 format.


That is what I'm asking.

2) Find the specs for that format and write your own deserialization code.

What did you mean by that? I thought you meant if I knew the code in the VB file, I would be able to do it.


By "specs" I didn't mean the VB6 source file that corresponds to the binary serialized version. I mean the specification for the format of how a class or object or whatever is serialized for that file you're trying to read. For instance, the format might be "First 2 bytes indicate the number of objects. Next 2 bytes indicate the length of the class name of the first object. Then whatever number those 2 bytes represent, that many bytes are the characters for that class name," and so on. Or it could be something totally different. You would need to learn that format, and write code to read and interpret those bytes accordingly. (Or find a library that already does that.)

How am I able to read a VB6 file? Is it possible?


Of course it's possible. If you know the format, or have a library that does.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Paul Clapham wrote:
I guess this hasn't been explicitly stated, so let me start by pointing out that you can't use Java serialization, because that isn't how the file is formatted.


I thought I was pretty explicit about that...

me wrote:Java's serialization mechanism is also binary, but that doesn't mean it can read any of those other formats or whatever format your VB files use.

me wrote:That would only work if Java's serialization format was the same as that VB6 format, which is phenomenally unlikely.


Okay, so I guess that's not quite as explicit as saying "you can't use Java serialization". My bad.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jay Orsaw wrote:
thank you, as I have already mentioned I HAVE THE FILE, now what do I do with it?


You do some research. You first find out if there's a Java library you can use that knows how to read that format. If you can't find one, then you find the specs for that format to find out how it converts a VB6 class or object into that particular binary representation.

I have the format and I made it into a class


What exactly is that supposed to mean?
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Paul Clapham wrote:
Jay Orsaw wrote:thank you, as I have already mentioned I HAVE THE FILE, now what do I do with it? I have the format and I made it into a class but the problem is I cannot read it in with the objectinputsteam, as stated above I get an error. What should I do to read in the file since the OIS isn't working?

There are 3 TYPES each type has a set of objects like "String" for example. I set up the first TYPE as 1 class called planHead(Same as in the Vb code in the file..) But as you can see in the problem above I am stuck at reading in the file to try and deserialize it, so that's why I ask if there is a different way to read it in..?


I guess this hasn't been explicitly stated, so let me start by pointing out that you can't use Java serialization, because that isn't how the file is formatted.

But you have the specs about what is in the file (right? That was what you meant when you said you "have the format", was it?) so the next step is to write some Java code which reads the file. If the specs include binary data which corresponds to Java types then you could use a DataInputStream to read that data.


Thank you the DataInputStream worked, only issue is that when I use the VB "Single" with the Java "Float" I get a # like 2.40239E-41 when it should be 3.0. If I read in 2 characters from the stream and then do the float I get 3.0, but when I try to do that with the next float which should be 4.0, I cannot, and I doubt that even makes sense to read in the 2 extra characters...











Everything else works fine except for this float... Even reading data after the misread floats, was successful. The joys of multiple languages... BOO!!! Java forever!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

What you are now doing is called "reverse engineering". A tedious technique but probably the only one available at this point in time.

You may find that looking at the hexadecimal representation of data in the file helps. For this you can use a tool called a "hex editor". You'll find plenty of them available on the internet.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jay Orsaw wrote:Thank you the DataInputStream worked, only issue is that when I use the VB "Single" with the Java "Float" I get a # like 2.40239E-41 when it should be 3.0.


I wouldn't call that "working". This means that the serialization format for a Java float is not the same as it is for a VB Single.

If I read in 2 characters from the stream and then do the float I get 3.0, but when I try to do that with the next float which should be 4.0, I cannot, and I doubt that even makes sense to read in the 2 extra characters...


No, it ceratinly doesn't make sense. It doesn't make sense to read any characters at all when reading a float. A float consists of bytes, not characters.

Everything else works fine except for this float... Even reading data after the misread floats, was successful. The joys of multiple languages... BOO!!! Java forever!


The problem is not multiple languages. The problem is that you're still assuming that two totally separate, independent entities would take identical approaches to a given problem.

Also, I wouldn't get too excited yet about other stuff working. The fact that it happened to work for whatever tests you ran doesn't mean it will work for all cases on those types. There may be some overlap, even with different formats. You still need to find out the specs for the format and see if they match the specs for Java's primitive serialization. If that information is not available to you, then you'll need to to some very thorough testing to include corner cases, minimums, maximums, etc.
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Paul Clapham wrote:What you are now doing is called "reverse engineering". A tedious technique but probably the only one available at this point in time.

You may find that looking at the hexadecimal representation of data in the file helps. For this you can use a tool called a "hex editor". You'll find plenty of them available on the internet.


Thanks, but I feel like I'm not clear. I have all of the VB code, I have the VB file, I am just having a problem reading this float(VB single). I can read the other things in order no problem, the float is giving me the issue. Int(VB Long) Boolean, etc all work, just this floating point # is giving me a random number... There is nothing between the BOOLEAN and the FLOAT, but the only way to actually get 3.0 is by throwing out 2 characters, and then reading the float, which makes no sense since they are right after another. I'm not sure how I'm "reverse engineering" since I have everything needed, I just need Java to be able to work for it...


it goes 2 Strings, 1 boolean, 2 floats, 2 ints, ETC. The fact I can read in the 2 strings, the 1 boolean, the 2 floats(wrong values) and then getting the CORRECT 2 int values next is weird.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

Jay Orsaw wrote:it goes 2 Strings, 1 boolean, 2 floats, 2 ints, ETC. The fact I can read in the 2 strings, the 1 boolean, the 2 floats(wrong values) and then getting the CORRECT 2 int values next is weird.


My best guess is that Java uses a different encoding of float to bits than VB uses. But they must both use the same number of bits for a float, which is why the following data is still unaffected.

A bit of digging in the Javadocs suggests that Java will read floats according to the format specified in Float.intToFloatBits:
Returns the float value corresponding to a given bit representation. The argument is considered to be a representation of a floating-point value according to the IEEE 754 floating-point "single format" bit layout.

(more details given if you follow the link).

You need to find the equivalent specification for what VB uses. You may then have to read the bytes directly, and manipulate them yourself, rather than relying on the Java library to do it for you.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jay Orsaw wrote:I can read the other things in order no problem


Because those things coincidentally happen to be the same in that VB6 format is in Java's format. (For the handful of values that you have tested.)

It's like you're listening to somebody speak Spanish, and you hear him say the word "no", and you say, "I understood 'no', so why can't I understand the rest of what he's saying?"

this floating point # is giving me a random number...


No, it is giving you the interpretation of those bytes according to Java's format for float, which, obviously, is different from VB's format for Single.

There is nothing between the BOOLEAN and the FLOAT, but the only way to actually get 3.0 is by throwing out 2 characters,


That happened to appear work for the few values you happened to test. Probably those bytes are important in general but didn't happen to be used in your particular test values.

I'm not sure how I'm "reverse engineering" since I have everything needed,


No, you don't have everything you need. You don't have the spec. You're reverse engineering because you're looking at the final result and the known input that generated it and trying to find patterns that you can use to deduce the rules of how the input gets translated to the final result. Or maybe you're not doing that. I'm starting to think you're just trying stuff and hoping for the best and getting lucky that some things happen to work, but not actually understanding how or why they work (that is, not knowing anything about the formats involved).

I just need Java to be able to work for it...


Java is not the problem. Java will work fine for this. Once you figure out the actual format and write the appropriate Java code to translate it.

Antoine Brultet
Greenhorn

Joined: Feb 22, 2014
Posts: 5
    
    1
Hi,

I just released a tiny library in order to read VB6 Binary files and insert the corresponding values in Java object.
I hope that it will help someone : http://biome404.com/lire-fichier-binaire-vb6/
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2398
    
  50
Welcome to the Ranch Antoine and thanks for providing the link to your library.
Antoine Brultet
Greenhorn

Joined: Feb 22, 2014
Posts: 5
    
    1
Thank you for your welcome Tony.
As you may not be familiar with the Molière language, here are some explanations :
-Create a Java class with a structure corresponding to the VB6 record
-Extend VB6Binary class
-Create an instance of this class and call the "deserialize" method
-That's it !

P.S : There are two useful annotations :
-@DeserializeIgnore : Do not consider the following attribute as a part of the VB6 record (can be useful for dynamic attributes like ID)
-@StringSize : If your VB6 record dispose of a fixed string size, specify it !
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2447
    
  28

Antoine,

Very interesting. Will this work on any file serialized by .NET, or is it VB6 specific? Can you make something that can work on any .NET file.

We have another application here that is written in .NET, and someone had the great idea of storing a serialized .NET object in the database. The problem is Java cannot read it. Being able to read .NET serialized objects would be very useful.
Antoine Brultet
Greenhorn

Joined: Feb 22, 2014
Posts: 5
    
    1
Hi Jayesh,

I am happy that you find it interesting !
Unfortunately, I am cruelly lacking of time, implying that I cannot check the .NET binary application yet.
However, I am going to be more available next week so I keep this idea and will come back to you as soon as possible.
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

I just want to say you're a lifesaver, thanks a ton for this!

I wanted to know, what if we do run into a Byte issue?

From what I saw doing my own conversion the real issue was Floats and Doubles.

Is there any documentation you read up on to do this, or you just know a lot about binary, I'm interested to know more about it all, though if I looked into it, it might not be that difficult!
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 700
    
  11
Just reading this topic, and I was wondering: have you by any chance VB6 around (or maybe even VB4.5)? Or do you know someone who does?
Then it would be a very easy job to read those files in uing VB and write them out as text (using the PRINT# statements).
You would need to know what's in those files, but if I understand correctly that's not the problem.

Greetz,
Piet
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Piet Souris wrote:Just reading this topic, and I was wondering: have you by any chance VB6 around (or maybe even VB4.5)? Or do you know someone who does?
Then it would be a very easy job to read those files in uing VB and write them out as text (using the PRINT# statements).
You would need to know what's in those files, but if I understand correctly that's not the problem.

Greetz,
Piet


Okay so, I am getting a BINARY file from someone using VB6, and creating an Java application that reads it.

It is possible to create a file with text, and a non-binary file, but then peopel can read it, as a business application I don't want this, thus the binary format.

The issue comes into play when we run into FLOATS/SINGLEs and DOUBLES.

I read somewhere that the IEEE standard for VB6 and Java are different(then I found that to be false at some point so not too sure) it's just the binary values are different, so we need a converter.
Antoine Brultet
Greenhorn

Joined: Feb 22, 2014
Posts: 5
    
    1
Jay Orsaw wrote:I just want to say you're a lifesaver, thanks a ton for this!

I wanted to know, what if we do run into a Byte issue?

From what I saw doing my own conversion the real issue was Floats and Doubles.

Is there any documentation you read up on to do this, or you just know a lot about binary, I'm interested to know more about it all, though if I looked into it, it might not be that difficult!


Thanks for the compliment Jay Orsaw !

In fact, if you run into a Byte issue (bad class definition, bad values), the parser will go into deepness and you will get some very strange exception like this one :


Or this one :


I agree with you that it is not very developper-friendly because you cannot deduce if it come from your code or from the library...
I may implement a more talkative error in the future but it is quite difficult to process in a very relaxed parser.

As for your problem with Double, I just made some check and this type wasn't implemented in the library.
Go back to my website in order to download the V1.1 which contain your holy graal.

I created this library for a personnal project, based on my own experience, some documentation and by analyzing with hands the bytes created by the serializer.
Documentations on raw types here :
http://msdn.microsoft.com/en-us/library/47zceaw7.aspx
http://www.commentcamarche.net/contents/564-java-les-types-de-donnees

I am glad that this library was useful for you !
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2398
    
  50
Hi Antoine,
For your continued help on this difficult topic and for providing your library I've awarded you a cow.
Antoine Brultet
Greenhorn

Joined: Feb 22, 2014
Posts: 5
    
    1
Tony Docherty wrote:Hi Antoine,
For your continued help on this difficult topic and for providing your library I've awarded you a cow.


Hi Tony,

Thanks you for this cow, I will take good care of her ! ;)

Nota bene :
I have just released a new version (1.2) of the library.
News are :
-Better deserialization of simple and multi-dimensionnal arrays.
-Possibility to put the library in a more talkative mode (Very useful to debug)
-Possibility to parse a binary file in multiple phases

I highly recommand to upgrade yours if you have got an older version but it is up to you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deserializing Visual Basic 6 files in Java?