2

Structure 1:

typedef struct _wfs_cdm_cu_info
{
    USHORT usTellerID;
    USHORT usCount;
    LPWFSCDMCASHUNIT * lppList;
} WFSCDMCUINFO, * LPWFSCDMCUINFO; 

Structure 2:

typedef struct _wfs_cdm_cashunit
{
    USHORT usNumber;
    USHORT usType;
    LPSTR lpszCashUnitName;
    CHAR cUnitID[5];
    CHAR cCurrencyID[3];
    ULONG ulValues;
    ULONG ulInitialCount;
    ULONG ulCount;
    ULONG ulRejectCount;
    ULONG ulMinimum;
    ULONG ulMaximum;
    BOOL bAppLock;
    USHORT usStatus;
    USHORT usNumPhysicalCUs;
    LPWFSCDMPHCU * lppPhysical;
} WFSCDMCASHUNIT, * LPWFSCDMCASHUNIT;

Structure 3:

typedef struct _wfs_cdm_physicalcu
{
    LPSTR lpPhysicalPositionName;
    CHAR cUnitID[5];
    ULONG ulInitialCount;
    ULONG ulCount;
    ULONG ulRejectCount;
    ULONG ulMaximum;
    USHORT usPStatus;
    BOOL bHardwareSensor;
} WFSCDMPHCU, * LPWFSCDMPHCU;      

The code:

 LPWFSCDMCUINFO lpWFSCDMCuinf = NULL;   
LPWFSCDMCASHUNIT lpWFSCDMCashUnit =  NULL;   
LPWFSCDMPHCU   lpWFSCDMPhcu = NULL;   
int i=0;
try
 {
    hResult = WFMAllocateBuffer(sizeof(WFSCDMCUINFO),WFS_MEM_ZEROINIT|WFS_MEM_SHARE,(void**)&lpWFSCDMCuinf); 
    lpWFSCDMCuinf->usCount =7;   
    lpWFSCDMCuinf->usTellerID = 0;          
    hResult = WFMAllocateMore(7*sizeof(LPWFSCDMCASHUNIT),lpWFSCDMCuinf,(void**)&lpWFSCDMCuinf->lppList);   
    for(i=0;i<7;i++)
    {
        LPWFSCDMCASHUNIT   lpWFSCDMCashUnit = NULL; 
         hResult = WFMAllocateMore(sizeof(WFSCDMCASHUNIT), lpWFSCDMCuinf, (void**)&lpWFSCDMCashUnit);
        lpWFSCDMCuinf->lppList[i] = lpWFSCDMCashUnit;//store the pointer
        //FILLING CASH UNIT
        -----------------------------
         lpWFSCDMCashUnit->ulValues =50;
        -----------------------------
        WFMAllocateMore(1* sizeof(LPWFSCDMPHCU), lpWFSCDMCuinf, (void**)&lpWFSCDMCashUnit->lppPhysical);// Allocate Physical Unit structure
        for(int j=0;j<1;j++)
        {
            LPWFSCDMPHCU   lpWFSCDMPhcu = NULL;  
            hResult = WFMAllocateMore(sizeof(WFSCDMPHCU), lpWFSCDMCuinf, (void**)&lpWFSCDMPhcu);
            lpWFSCDMCashUnit->lppPhysical[j] = lpWFSCDMPhcu;

            //FILLING Phy CASHUNIT
            -------------------------------------------------------
            lpWFSCDMPhcu->ulMaximum = 2000; 
             -----------------------------
        }

    }

    //lpWFSCDMCuinf->lppList=&lpWFSCDMCashUnit;
    hResult =WFSExecute (hService,WFS_CMD_CDM_END_EXCHANGE,(LPVOID)&lpWFSCDMCuinf,60000,&lppResult);
    return (int)hResult;

I'm getting stuck while I retrieve all the values in structure 1. I need to dynamically add the values into these structure and display Structure1 as output.An allocation of memory needs to be done for this.I have tried using the above code for allocating the memory but in spite of allocating the values are not properly stored in structure.

The value of usCount changes as per the denomination set. Based on this usNumPhysicalCUs is set. Also when I send &lpWFSCDMCuinf within the WFSExecutemethod the lppPhysical seems to be empty.

I cant exactly figure out where I'm getting wrong.

TechBrkTru
  • 346
  • 1
  • 25
  • 2
    Do you expect anyone to read that wall of obfuscated code? Please reduce it to something that demonstrates your problem, with readable names. – Mike Seymour May 12 '15 at 07:35
  • 1
    Well this is an XFS compliant code due to which it sounds to be obfuscated one.I ll try to minimize it. – TechBrkTru May 12 '15 at 07:42
  • i have no idea of XFS but your c++ code isn't valid... for(int i=0;i<7i++) <--- and at line 2 you don't define i (in the for), in line 8 you do... with lpWFSCDMPhcu = (LPWFSCDMPHCU)malloc(sizeof(LPWFSCDMPHCU)); in a loop you overwrite it every time - then your code leaks memory?! – Wolfgang May 12 '15 at 09:08
  • yes int i is already declared above.I need to have 7 physical cash unit hence loop was considered, what could be different way then? – TechBrkTru May 12 '15 at 09:41

1 Answers1

2

First of all your must allocate memory for each block. For pointers array you will allocate memory to store count of pointers, than for each pointer in allocated memory you must allocate memory for structure itself. I rewrite your code in more short form. There is no error checking and this code is sample only.

LPWFSCDMCUINFO lpWFSCDMCuinf = NULL;
HRESULT hr = WFMAllocateBuffer(sizeof(WFSCDMCUINFO), WFS_MEM_ZEROINIT|WFS_MEM_SHARE, (void**)&lpWFSCDMCuinf);
// Allocate 7 times of WFSCDMCASHUNIT
const int cuCount = 7;
lpWFSCDMCuinf->usCount = cuCount;
hr = WFMAllocateMore(cuCount * sizeof(LPWFSCDMCASHUNIT), lpWFSCDMCuinf, (void**)&lpWFSCDMCuinf->lppList);
for (int i=0; i < cuCount; i++) 
{
    // for one entry
    LPWFSCDMCASHUNIT currentCU = NULL;
    hr = WFMAllocateMore(sizeof(WFSCDMCASHUNIT), lpWFSCDMCuinf, (void**)&currentCU);
    // Store pinter
    lpWFSCDMCuinf->lppList[i] = currentCU;
    // Fill current CU data here
    // ....

    // Allocate Phisical Unit Pointers
    const int phuCount = 1;
    currentCU->usNumPhysicalCUs = phuCount;
    WFMAllocateMore(phuCount * sizeof(LPWFSCDMPHCU), lpWFSCDMCuinf, (void**)&currentCU->lppPhysical);
    // Allocate Phisical Unit structure
    for (int j=0; j < phuCount; j++)
    {
        LPWFSCDMPHCU phuCurrent = NULL;
        // Allocate Phisical Unit structure
        WFMAllocateMore(sizeof(WFSCDMPHCU), lpWFSCDMCuinf, (void**)&phuCurrent);
        currentCU->lppPhysical[j] = phuCurrent;
        // Fill Phisical Unit here
        // ..
        // ..
    }
}

In additional to this sample I recommend you to write some helper function to allocate XFS structures like WFSCDMCUINFO. In my own project I've used some macro to serialize XFS structure in memory with WFMAllocate and WFMAllocateMore functions. XFS structures is so complex and different from class to class. I wrote some macros to serialize and deserialize structures in memory stream and XFS memory buffers. In application I use heap alloc to store XFS structures in memory, but when I need to return structures in another XFS message I need to transfer memory buffers to XFS memory with WFMAllocate and WFMAllocateMore.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Alex.D.Alexeev
  • 304
  • 1
  • 6
  • 1)A confusion exits .why have you declared "LPWFSCDMPHCU" twice. LPWFSCDMPHCU currentCU = NULL; LPWFSCDMPHCU phuCurrent = NULL;2)i didntget what helper function is.I'm trying to use WFMAllocateBuffer and WFMAllocateMore ,but i get an error of object not set to an instance while allocating it 7 times for Cash Unit 3)I get an error as:-- error C2664: 'WFMAllocateBuffer' : cannot convert parameter 3 from 'LPWFSCDMCUINFO *' to 'LPVOID *' 4)The filling of data is done but not passed inlast structure whole:lpWFSCDMCuinf->lppList=&lpWFSCDMCashUnit; 5) I can see currentCU is been an undefined value – TechBrkTru May 13 '15 at 07:26
  • There some typos in sample code. Now structures allocated correctly. – Alex.D.Alexeev May 13 '15 at 07:29
  • so in the end i don't need to add lpWFSCDMCuinf->lppList=&lpWFSCDMCashUnit; – TechBrkTru May 13 '15 at 07:37
  • Value of lpWFSCDMCuinf->lppList is set at WFMAllocateBuffer – Alex.D.Alexeev May 13 '15 at 07:38
  • i can see the structure lpWFSCDMCuinf.lppList empty when i pass it as whole . just because LPWFSCDMCASHUNIT currentCU = NULL; The code is been edited for your reference – – TechBrkTru May 13 '15 at 10:28
  • @TechBrkTru So strange. I've just run this sample code and got right result. See screenshot [link](https://www.dropbox.com/s/ueqm79yk9bwlhm2/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202015-05-13%2016.08.46.png?dl=0). currentCU value is set at `WFMAllocateMore(sizeof(WFSCDMCASHUNIT), lpWFSCDMCuinf, (void**)&currentCU)` and stored in array at next line `lpWFSCDMCuinf->lppList[i] = currentCU;` – Alex.D.Alexeev May 13 '15 at 13:11
  • While viewing lpWFSCDMCuinf->lppList[i] = lpWFSCDMCashUnit; lpWFSCDMCashUnit; stores all the 7 times values but in lpWFSCDMCuinf->lppList[i] only the 1 st value can be seen due to which i get an WFS_ERR_INTERNAL_ERROR – TechBrkTru May 13 '15 at 13:29
  • Were You can't see result? In external application? – Alex.D.Alexeev May 13 '15 at 13:59
  • @ Alex.D.Alexeev :I have a doubt ,how would we display the structure 3 values?? in structure 1 for example :for structure 1 we can say as: `fwCashUnitInfo.usTellerID;` ,to get structure 2 from 1 : `(*(*fwCashUnitInfo).lppList).usNumber` but what about structure3 – TechBrkTru May 20 '15 at 10:38
  • @TechBrkTru: There is simply C array of pointers in lppList. use 'fwCashUnitInfo->lppList[3]->usNumber'. Or something else index in square braces – Alex.D.Alexeev May 20 '15 at 12:52
  • @TechBrkTru: No, this is not 2D array this array of pointers to pointers to structures. Unfortunately, I don't know how to use data structures like this in C#. In C++, if I need to examine values of this array, I'll write the 'for ' loop and iterate through all array items one by one. – Alex.D.Alexeev May 20 '15 at 13:18
  • @ Alex.D.Alexeev :Well how should i store all the seven values in a duplicate (without aray of pointers)structure after i retrieve through loop ?? – TechBrkTru May 20 '15 at 13:30
  • @TechBrkTru: If you want to store data in XFS structures you can't use another way. You must store data in array of pointers to pointers. But you can use any other data structure to store data on your own side. There are many ways to do that. Can you describe your task more detailed? – Alex.D.Alexeev May 20 '15 at 14:44
  • Thank you very much @Alex.D.Alexeev. I was stuck with this for many hours and your answer helped me. – James Selvakumar May 27 '19 at 06:30