开发者

Accessing elements in two dimensional BYTE array in C++

开发者 https://www.devze.com 2023-03-24 15:21 出处:网络
I am trying to use a function provided in 3rd party documentation and am having trouble getting my head around it. I recently figured out the meaning of IN/OUT variables and how to work with them. The

I am trying to use a function provided in 3rd party documentation and am having trouble getting my head around it. I recently figured out the meaning of IN/OUT variables and how to work with them. The problem with this function is it has several diffe开发者_Python百科rent types all combined. I am really confused how to access these array elements. Provided below is a screenshot of the function information.

Accessing elements in two dimensional BYTE array in C++

This is the code I am working with:

BYTE numDevices = 10;
    BYTE devices;
    ULONG devicesArray = QCWWAN2kEnumerateDevices(&numDevices,&devices);

    //How do I access the elements in the returned array?

ULONG IS THE RETURN CODE TO SEE IF IT FAILED/WHY


You need to get a debugger on it. It is unclear whether the QCWWAN2kEnumerateDevices allocates memory for your devices. If it doesn't (I doubt it does, knowing Win32API) your

BYTE devices;

should instead be

struct DEVICE_ARRAY_ELEM {
char devID[256];
char devKey[16];
};
DEVICE_ARRAY_ELEM *pDevices = malloc(sizeof(DEVICE_ARRAY_ELEM) * 10);
ULONG devicesArray = QCWWAN2kEnumerateDevices(&numDevices, (pDevices);
//Do stuff
free((void *)pDevices);

EDIT___ sorry that was C, here it is in C++

struct DEVICE_ARRAY_ELEM {
char devID[256];
char devKey[16];
};
DEVICE_ARRAY_ELEM *pDevices = new DEVICE_ARRAY_ELEM[10];
ULONG devicesArray = QCWWAN2kEnumerateDevices(&numDevices, pDevices);
//do stuff
delete [] pDevices;

To access use:

pDevices[devnum].devID[IDIndex];


Improving upon John Silver's answer

// IN A HEADER:
typedef struct DEVICE_ARRAY_ELEM {
    char devID[256];
    char devKey[16];
} DEVICE_ARRAY_ELEM; //  This defines a struct to hold the data brought back
// it also type defs 'struct DEVICE_ARRAY_ELEM' to 'DEVICE_ARRAY_ELEM' for convienence

// IN YOUR CODE:
// This pointer should be wrapped in a auto_ptr to help with RAII
DEVICE_ARRAY_ELEM *pDevices = new DEVICE_ARRAY_ELEM[10]; // allocate 10 elements in-line
ULONG errorCode = QCWWAN2kEnumerateDevices(&numDevices, (BYTE*)pDevices); // get them

// Here is the hard part: iterating over the array of devices returned
// as per the spec numDevices is now the number of devices parsed
for(int i = 0; i < numDevices; i++) {
   printf("%s\n", pDevices[i].devID); // is the name of the device (a character array)
}

delete [] pDevices;

EDIT

I now use numDevices do iterate over the array since the spec says that is the number of devices enumerated after the function call

EDIT AGAIN

Here is the code working based on my assumptions: IDEONE

The code has some typedefs and a definition of what I think the QCWWAN2kEnumerateDevices operates. So those should be ignored, but the code is compiles and performs as expected

0

精彩评论

暂无评论...
验证码 换一张
取 消