PRB: Stream Size May Vary Depending on Where It Is Loaded From (304964)



The information in this article applies to:

  • ActiveX Data Objects (ADO) 2.6
  • ActiveX Data Objects (ADO) 2.7

This article was previously published under Q304964

SYMPTOMS

When an ActiveX Data Objects (ADO) Stream object is loaded from a file, the leading 2 or 3 bytes of the stream may be used for the encoding. Therefore, if the size of the stream is checked, it may return 2 to 3 bytes more than the actual file size. However, if the stream is opened from the same file through a URL, the size of the stream is exactly the same as the file size.

STATUS

This behavior is by design.

MORE INFORMATION

The extra length can be 2 or 3 bytes depending on the code page (2 for UNICODE and 3 for UTF8). This discrepancy occurs only when the encoding is not already present in the file. If the encoding is present, the lengths are the same.

Steps to Reproduce Behavior

  1. Create an empty Win32 console application.
  2. Add a new .cpp file to the project and paste the following code:
    #include <stdio.h>
    #import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "ADOEOF")
    
    void main(int argc, char* argv[])
    {
    
            _StreamPtr m_pStream1, m_pStream2;
            int nSize1, nSize2;
    	
            // File size3.txt has 3 characters.
            WCHAR m_strmSource2[1024]=L"URL=http://adotest/size3.txt";
            WCHAR m_strmSource1[1024]=L"\\\\adotest\\size3.txt";
    	
            HRESULT hr = S_OK; 
            try{
    
                    CoInitialize(NULL);
                    _variant_t vSource;
                    VariantInit(&vSource);
                    vSource.vt = VT_ERROR;
                    hr = m_pStream1.CreateInstance(__uuidof(Stream));
                    hr = m_pStream2.CreateInstance(__uuidof(Stream));
                    hr = m_pStream1->Open(vSource,adModeUnknown,adOpenStreamUnspecified,L"",L"");
    
                    m_pStream1->LoadFromFile(m_strmSource1);
                    nSize1 = m_pStream1->Size; // 5 is returned, which is 3 + 2 (2 bytes for type encoding).
                    hr = m_pStream2->Open(m_strmSource2,adModeUnknown,adOpenStreamUnspecified,L"",L"");
                    nSize2 = m_pStream2->Size; // 3 is returned, which is expected.		
    
                    printf("SIZE: Opening Stream from file returned: %d\n",nSize1);
                    printf("SIZE: Opening Stream from URL returned: %d",nSize2);
    
                    hr = m_pStream1->Close();
                    hr = m_pStream2->Close();
    
                    CoUninitialize();
            }
            catch (_com_error &err){
                    _bstr_t bstrSource(err.Source());
                    _bstr_t bstrDescription(err.Description());
       		
                    // Print Com errors.
                    printf("Error\n");
                    printf("\tCode = %08lx\n", err.Error());
                    printf("\tCode meaning = %s\n", err.ErrorMessage());
                    printf("\tSource = %s\n", (LPCSTR) bstrSource);
                    printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
       
            }
    }
    					
  3. Build and then run the application to check the size difference.

Modification Type:MajorLast Reviewed:8/23/2001
Keywords:kbDSupport kbprb KB304964