Theos PowerBasic Museum 2017

Archive => Discussion - Legacy Software (PBWIN 9.0+/PBCC 5.0+) => Topic started by: Edwin Knoppert on December 11, 2010, 01:11:40 PM

Title: IStream determing size
Post by: Edwin Knoppert on December 11, 2010, 01:11:40 PM
I have used your code for testing the istream interface since in my original (vtable) code i had some differences for determing the size of the data.
I wrote my code in the w95/w98 days and for some reason i subtracted #1 from the size, don't recall why but on XP and W7 it's incorrect.
Therefore i went back to basics by creating the code from scratch but under win98 i still get a difference.

I just need some conformation if this was only a matter for those older os's  which are obsolete anyway.

Both the seek as size give a length of #8 instead of #5 (i run w98 in a virtual pc):


#Include "ObjIdl.inc"
#Include "OLE2UTILS.INC"
#Include "Ole2.inc"

Function PBMain() As Long

    Local hMem              As Long
    Local pMem              As Long
    Local hr                As Long
    Local ppstm             As IStream
    Local nLen_GetSize      As Quad
    Local nLen_Seek         As Quad
    Local libNewPosition    As Quad

    hMem = GlobalAlloc( %GMEM_MOVEABLE, 5 )
    pMem = GlobalLock( hMem )

    Poke$ pMem, "Hello"

    Call GlobalUnlock( hMem )

    hr = CreateStreamOnHGlobal( hMem, 0, ppstm )
   
    If hr <> %S_OK Then
        ? "Error"
        Exit Function
    End If

    nLen_GetSize = IStream_GetSize( ppstm )

    ppstm.Seek( 0, %STREAM_SEEK_SET, nLen_Seek )
    nLen_Seek = 0
    ppstm.Seek( 0, %STREAM_SEEK_END, nLen_Seek )

    ? Str$( nLen_GetSize ) & $CrLf & Str$( nLen_Seek )

End Function


Title: Re: IStream determing size
Post by: Edwin Knoppert on December 11, 2010, 02:41:05 PM
The GlobalAlloc() may cause this trouble.
GlobalSize() returns the wrong size so i suspect IStream is using this call as well.
In the help it size this api may return a larger size which i can understand (common practise for memory allocators) however an IStream interface should be exact imo.
Title: Re: IStream determing size
Post by: Edwin Knoppert on December 11, 2010, 02:51:12 PM
MSDN states the same and suggest you use SetSize()

After creating the interface set the size like:
ppstm.SetSize( 5 )

It now returns the correct size.
Title: Re: IStream determing size
Post by: José Roca on December 11, 2010, 07:04:33 PM
 
I know nothing about Win 95/98. The first one I used was Windows 2000.
Title: Re: IStream determing size
Post by: Edwin Knoppert on December 11, 2010, 08:24:55 PM
Doesn't matter, it seems by setting the desired size it is working fine.
Frankly i find it odd Windows nowadays creates memory with the exact size.
Maybe not but somehow stores the size (better) during creation.