check for NULL value

Hello,

I have declared an external method like so:

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 ?

thank you

You can run it twice:

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

Thank you for your answer.
But:

if xlSheetReadStrW (iSheetHandle, iRow,iCol,iFormat) <> nil then

gives an error:

I think the reason is that the function returns a “WString” - and it seeems that this can not be compared with “nil” ?

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

tried it, but doesn’t work.

I get the following error

[31524] RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly

In this line

mb_ReadStringFromSheet.WString(0) = xlSheetReadStrW (iSheetHandle, iRow,iCol,iFormat)

You shouldn’t need the memoryblock. WString auto-converts to String.

dim ReadStringFromSheet as String
ReadStringFromSheet = xlSheetReadStrW(iSheetHandle, iRow, iCol, iFormat)

the libxl documentation says

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

[quote]if mb.Len > 0 then
ReadStringFromSheet = mb.WString(0)
else
// error
end[/quote]

this didn’t work because mb.len is always 0. Same with mb.lenb.

But I did a little change and now it works:

[code]dim mb as memoryblock
dim ReadStringFromSheet as String

mb = xlSheetReadStrW(iSheetHandle, iRow, iCol, iFormat)

if mb <> nil then 'len() doesnt work - but NIL does :slight_smile:

 ReadStringFromSheet = mb.WString(0)

else
//error

end[/code]

thank you for the help