简体   繁体   中英

Pointer to array of structs pointers

I need create one pointer to a null-terminated array of pointers to key detail structures..

Struct: WFS_RESULT

typedef struct _wfs_result
{
    REQUESTID       RequestID;
    HSERVICE        hService;
    SYSTEMTIME      tsTimestamp;
    HRESULT         hResult;
    union {
        DWORD       dwCommandCode;
        DWORD       dwEventID;
    } u;
    LPVOID          lpBuffer;
} WFSRESULT, *LPWFSRESULT;

Struct: PINKEY

typedef struct _wfs_pin_key_detail_ex
{
LPSTR         lpsKeyName;
DWORD         dwUse;
BYTE          bGeneration;
BYTE          bVersion;
BYTE          bActivatingDate[4];
BYTE          bExpiryDate[4];
BOOL          bLoaded;
} WFSPINKEYDETAILEX, * LPWFSPINKEYDETAILEX;

Program: How am i trying to do

    LPWFSPINKEYDETAILEX* array[7];

    LPWFSPINKEYDETAILEX Test;
    WFSPINKEYDETAILEX Obj;
    Test = &Obj;

    Test->lpsKeyName = NULL;

    array[0] = &Test;
    array[1] = &Test;
    array[2] = &Test;
    array[3] = &Test;
    array[4] = &Test;
    array[5] = &Test;
    array[6] = NULL;

    LPWFSPINKEYDETAILEX** val = array;

    lpWFSResult->lpBuffer = val;

The question is, is what I did above a pointer to an array of pointers? Because, I need to pass this Pointer Array Pointer to this parameter lpWFSResult-> lpBuffer = val; and in the final program (Bank Application) it gives error -15 ( WFS_ERR_INTERNAL_ERROR ).

Just as idea. Maybe they check if to be different objects. Maybe they are using the previous pointers and do not expect that value of val[0] will be changed after changing val[1].

Also check the API maybe they expects the rest of the fields filled up

  • lpsKeyName
  • bGeneration
  • bActivatingDate
  • bExpiryDate

This depends on how the array is created and where it is stored/used. based on the code provided, I assume the array has been generated on the memory stack but then used after that stack level has been popped (ie. function returns). The array memory will have been deallocated and the array pointer will be invalid (will cause unexpected behaviour). If you need to preserve the array outside of the stack, you will need to generate it on the heap using new . This way the memory will persist after the function exits and the memory stack level is popped.

LPWFSPINKEYDETAILEX** array = new LPWFSPINKEYDETAILEX*[7];

LPWFSPINKEYDETAILEX Test;
WFSPINKEYDETAILEX Obj;
Test = &Obj;

Test->lpsKeyName = NULL;

array[0] = &Test;
array[1] = &Test;
array[2] = &Test;
array[3] = &Test;
array[4] = &Test;
array[5] = &Test;
array[6] = NULL;

lpWFSResult->lpBuffer = array;

Don't forget to delete it later when you're done with the memory so you don't get a memory leak.

You need to read the API spec where it tells you how to allocate memory. I presume you are writing an SP, have six keys and the key names are in the array keyNames.

int numKeys=6;
LPSTR keyNames[6]={"key1","key2","key3","key4","key5","key6"};
LPWFSRESULT pResult;
LPWFSPINKEYDETAILEX* ppDetails;
WFMAllocateBuffer(sizeof(WFSRESULT), WFS_MEM_ZEROINIT, (LPVOID*)&pResult);
WFMAllocateMore(sizeof(LPWFSPINKEYDETAILEX)*(numKeys+1),pResult, (LPVOID*)&ppDetails);
for (int i=0;i<numKeys;i++)
{
  WFMAllocateMore(sizeof(WFSPINKEYDETAILEX),pResult,(LPVOID*)&ppDetails[i]);
  WFMAllocateMore(strlen(keyNames[i])+1,pResult,(LPVOID*)&ppDetail[i].lpsKeyName);
  strcpy(ppDetails[i].lpsKeyName,keyNames[i]);
  //TODO fill in other details
}
ppDetails[numKeys]=NULL;
pResult->lpBuffer=ppDetails;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM