0

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).

Edd
  • 1,350
  • 11
  • 14
Matheus Cardozo
  • 135
  • 1
  • 7
  • array is an array of pointers to LPWFSPINKEYDETAILEX, val is a pointer to a pointer to a LPWFSPINKEYDETAILEX –  Mar 20 '17 at 17:09

3 Answers3

0

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
0

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.

flamewave000
  • 547
  • 5
  • 12
  • Há um erro aqui: 'LPWFSPINKEYDETAILEX ** array = new LPWFSPINKEYDETAILEX [7];' -ERROR: http://imgur.com/JcHKC9U – Matheus Cardozo Mar 20 '17 at 20:06
  • Unfortunately I don't understand the language you're using. But I do realize I forgot to add a part to my previous answer which is as follows: `LPWFSPINKEYDETAILEX** array = new LPWFSPINKEYDETAILEX*[7];` – flamewave000 Mar 20 '17 at 20:22
0

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;
  • You do not free this memory, the application will free it all off by simply calling WFSFreeResult((LPWFSRESULT)lparam); which frees the entire chain of linked memory. – Allan Kirkwood May 09 '17 at 17:24