So I have a project that must run on 32bit and 64 bit windows. On 32 bit windows I sometimes get the dreaded "out of string space" error. I have tried as best I can to modify the code to use string buffers etc but it still happens sometimes
Using the information contained in this post I thought about creating a private heap to keep my large strings in.
https://www.vbforums.com/showthread....ocess-s-memory
The rough trial code looks like this. Is this a terrible idea? If it is then what would I need to do to make it workable. If its not what have I left out. I am not checking heap function status but I will. Does anyone know if out of string space means some limit has been hit or just that the default heap is too fragmented for the new allocation when I need to copy a string or pass it back up the stack. ( i guess this makes a new string)
Code:Dim lngPointer As Long Dim strString As String Dim lngPointer1 As Long Dim lngLength As Long Dim newLength As Long Dim oldBstrPtr As Long Dim oldStrPtr As Long Dim oldBStrLength As Long 10 newLength = 2048 20 strString = "a" 30 lngPointer = StrPtr(strString) ' save the old string pointer 40 oldStrPtr = lngPointer ' save the pointer to the old bStr 50 CopyMemory oldBstrPtr, ByVal lngPointer, 4 ' save the old length 60 CopyMemory oldBStrLength, ByVal lngPointer - 4, 4 ' ' The 4 bytes immediately before the BSTR is the length of the string ' ' Create a heap 70 hHeap = HeapCreate(0, 1024& * 1024, 0) ' allocate some memory 80 ptrHBlock = HeapAlloc(hHeap, 0, 2048) ' copy the address of the memory we allocated to to the old bstr address 90 CopyMemory ByVal lngPointer, ptrHBlock, 4 ' copy the new length we want 100 CopyMemory ByVal lngPointer - 4, newLength, 4 ' in theory I now have a string in a private heap ' Put it all back the way it was 110 CopyMemory ByVal lngPointer, oldBstrPtr, 4 120 CopyMemory ByVal lngPointer - 4, oldBStrLength, 4 'clean up 130 HeapFree hHeap, 0, ptrHBlock 140 HeapDestroy hHeap




Reply With Quote
