Declare Function xlSheetReadStrW Lib "libxl.dll" (handle as integer, row as integer, Col as integer, byRef format as integer) As WString
I can read the WString that is returned using a memoryblock:
dim mb_ReadStringFromSheet as new MemoryBlock (100)
mb_ReadStringFromSheet.WString(0) = xlSheetReadStrW (iSheetHandle, iRow,iCol,iFormat)
msgbox mb_ReadStringFromSheet.WString(0)
so this works, but I need some error handling:
If xlSheetReadStrW failes it will return NULL - and I must handle this.
How can I detect NULL?
I have found a way to do this using variants…
dim xVariant as Variant
xVariant = xlSheetReadStrW (iSheetHandle, iRow,iCol,iFormat)
if lenb(xVariant) = 0 then
//NULL
else
//OK
end if
…but I wonder if I can avoid to use variants (and work with the memoryblock) if I want to detect NULL ?
dim mb_ReadStringFromSheet as MemoryBlock
if xlSheetReadStrW (iSheetHandle, iRow,iCol,iFormat) <> nil then
mb_ReadStringFromSheet = new MemoryBlock(100)
mb_ReadStringFromSheet.WString(0) = xlSheetReadStrW (iSheetHandle, iRow,iCol,iFormat)
end if
if mb <> nil then
msgbox mb_ReadStringFromSheet.WString(0)
end if
Right. But then, it shouldn’t be able to return nil either, so there is something I’m not understanding here.
The next idea is to wrap the whole thing in a Try/Catch block.
dim mb_ReadStringFromSheet as MemoryBlock
Try
mb_ReadStringFromSheet = new MemoryBlock (100)
mb_ReadStringFromSheet.WString(0) = xlSheetReadStrW (iSheetHandle, iRow,iCol,iFormat)
Catch err as RuntimeException
mb_ReadStringFromSheet = nil
End Try
if mb_ReadStringFromSheet <> nil then
msgbox mb_ReadStringFromSheet.WString(0)
end if
Is it returning a literal zero pointer, or is it a pointer to an empty string? If it is a real NULL, then you should change the return type to Ptr and catch it in a memoryblock.
dim mb as memoryblock
dim ReadStringFromSheet as String
mb = xlSheetReadStrW(iSheetHandle, iRow, iCol, iFormat)
if mb.Len > 0 then
ReadStringFromSheet = mb.WString(0)
else
// error
end