Here’s a quick tip for troubleshooting API calls:
If you are calling an API and notice that data you are expecting isn’t being provided, verify that you are allocating enough space for the return values and telling it how much space you’ve allocated.
Many APIs require you to allocate space to hold the returned info – but if you don’t tell the API how much space you’ve provided, it will only return the information that fits in the space it knows about.
Case in point … a co-worker and I were trying to use the QDBRTVFD api … our code was allocating 32k (yeah, a lot more than needed) … but the data length we provided was only 400 bytes (size of the header data structure).
The information we were interested in was actually in one of the sub-structures … but the offset value for that sub-structure was zero.
A bit of digging turned up that the API was returning 400 bytes of data for the call (in the QDBFYRET field)… but there were 1400 bytes of data available (QDBFYAVL field).
The fix was quite simple … we told the API that we were providing 32k of space (same amount we were allocating) and it worked fine.
So you’re publicly stating when you write bad code, now? Interesting.
Tip: Always set the bytes provided = %size(return_Struct)
Well, strictly speaking, it wasn’t my code 🙂
But, to paraphrase a movie quote …. “Why do we write bad code? So we can learn from our mistakes”.
That will only work if your
return_struct
is all the data that you want back. In this case we WERE passing the size ofreturn_struct
to the API, but the information we needed was in sub-structures that thereturn_struct
had offsets to.