• Welcome to Theos PowerBasic Museum 2017.

ByteArray(0 TO 0)

Started by John Thompson, November 12, 2009, 02:52:57 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

John Thompson

I've been calling GetRawInputData which provides a string (array) which populates some TYPEs, UNIONs, and is also supposed to fill ByteArray(0 TO 0) with however much data was received.

Initially, I was unable to get the data I wanted, but after I changed the TYPE declaration from ByteArray(0 TO 0) to ByteArray(500), it has began to work.

I am curious if this is OK or if anyone can foresee any problems with this approach.  Also, is it possible to use this to store a changing amount of data?

Thanks!

José Roca

 
If with ByteArray(0 TO 0) you mean the bRawData member of the RAWHID structure, you will have problems because you don't know in advance how many elements you will need. According to the M$ documentation, the size of the bRawData array is dwSizeHid * dwCount. I think it would be better if you leave it as bRawData(0), get a pointer to the first element of the byte array with VARPTR(bRawData(0)) and use MoveMemory to copy dwSizeHid * dwCount bytes to a byte array or a string, wathever suits you more.

John Thompson

Jose,

Thanks again for your help.  I'd like to do something along those lines so that I don't have to hardcode the values, but as that works and I'm having problems with other methods that might be the easiest option.  When I keep the array as bRawData(0) or bRawData(0 TO 0) and use MoveMemory, it no longer works.

I was guessing that bRawData isn't filled beyond the size it is allocated at the time?

I then tried to REDIM the array to allow it to hold the data, but that doesn't work either as it says periods are not allowed in the REDIM element.


REDIM raw.ri.rhid.bRawData(dwSize) AS BYTE 'TODO: dwSize is the ENTIRE rawinput structure, not just bRawData.



José Roca

Quote
When I keep the array as bRawData(0) or bRawData(0 TO 0) and use MoveMemory, it no longer works.

You're right. It was thinking of other cases, where the memory is allocated by the callee and returns a pointer to the data.

Quote
I was guessing that bRawData isn't filled beyond the size it is allocated at the time?

The callee has no means of knowing it. It assumes that you will pass an array big enough to hold the data. If it is bigger, it doesn't matter, but if it is smaller it will corrupt the memory. It will not show in a quick test, but in a bigger application can lead to weird behavior or GPFs later in an unrelated part of your program. Therefore, you need to ascertain that the array is big enough.

Quote
I then tried to REDIM the array to allow it to hold the data, but that doesn't work either as it says periods are not allowed in the REDIM element.

It is not a metter of periods, but that dynamic arrays aren't currently supported by PB.

John Thompson

OK.  Thanks for all of the information.  So I just need to ensure that I define the array to be able to hold the maximum amount of information that may be passed and then use the other information to determine how much of that information is the actual information intended to be returned.  Thanks a lot!