PRB: GetChunk Corrupts Binary Data on Win98/NT4 SP4ID: Q199929
|
When using the Data Access Objects (DAO), Remote Data Objects (RDO) or the ADO GetChunk method, on a long binary field of a Microsoft Access database, binary data may appear corrupt if the binary information previously stored was read from disk using a String (BSTR) variable instead of a Byte array. The problem occurs under one of the following conditions:
The problem is that the information stored to the database is mistakenly being converted to Unicode before being stored. The "corruption" occurs when reading the information from disk using the Visual Basic or Visual Basic for Applications Get statement.
By design, the Get statement relies on the data type of the variable to determine how the information should be read into Visual Basic. If you pass a String (BSTR) variable to the function, Visual Basic identifies the information as being a text string, and converts it to Unicode. However, since the data is binary and not text, this conversion corrupts the data before it is ever stored to the database.
Although binary data stored in Unicode format can be "decoded" in some cases, it requires that the Unicode symbol table used to map 1-byte ANSI characters to 2-byte Unicode characters be the same for both the conversion to and the conversion from Unicode. Due to the recent addition of the European Currency symbol, the symbol table for Windows 98 and Windows NT 4.0 SP4 is different from those used previously. These changes will have no effect on text strings, but can effect binary data mistakenly converted to Unicode.
Binary data should NEVER be converted to/from Unicode. To avoid the possibility of such conversions, do not use BSTRs when working with binary data. Instead use a Byte array. For example:
Dim yBinaryData() As Byte
Open "C:\MyFile.dat" For Binary As #1
ReDim yBinaryData(LOF(1)) As Byte
Get #1, , yBinaryData
Close #1
rs.Fields("MyData").AppendChunk yBinaryData
This behavior is by design.
To reproduce the behavior you will need a .bmp image file (or other binary file) and two computers, one running Windows 95 and another running Windows 98.
Name | Type |
---|---|
ID (Primary Key) | AutoNumber |
Picture | OLE Object |
Sub TestPicIn(sFileName As String)
Dim db As Database
Dim rs As Recordset
Dim sData As String
Dim nDataSize As Long
Set db = CurrentDb()
Set rs = db.OpenRecordset("BinData")
rs.AddNew
Open sFileName For Binary As #1
nDataSize = LOF(1)
sData = Space$(nDataSize)
Get #1, , sData
Close #1
rs!Picture.AppendChunk sData
rs.Update
End Sub
Sub TestPicOut(sFileName As String)
Dim db As Database
Dim rs As Recordset
Dim sData As String
Dim nDataSize As Long
Set db = CurrentDb()
Set rs = db.OpenRecordset("BinData")
If Not rs.EOF Then
nDataSize = rs!Picture.FieldSize
sData = rs!Picture.GetChunk(0, nDataSize)
Open sFileName For Binary As #2
Put #2, , sData
Close #2
End If
End Sub
TestPicIn "C:\Windows\Setup.bmp"
TestPicOut "C:\Windows\Temp\TestPic.bmp"
For additional information about using GetChunk and AppendChunk, please see the following article in the Microsoft Knowledge Base:
Q153238 HOWTO: Use GetChunk and AppendChunk Methods of RDO Object
Additional query words: kbDSupport
Keywords : kbAccess kbADO kbAutomation kbDAO kbRDO KbVBA kbVBp500 kbVBp600 kbGrpDSO
Version : WINDOWS:2.0,5.0,6.0,97
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: January 23, 1999