How to dynamically fill the structure which is a pointer to pointer of arrays in C++ implementing xfs

后端 未结 1 1625
梦谈多话
梦谈多话 2020-12-21 17:15

Structure 1:

typedef struct _wfs_cdm_cu_info
{
    USHORT usTellerID;
    USHORT usCount;
    LPWFSCDMCASHUNIT * lppL         


        
相关标签:
1条回答
  • 2020-12-21 17:37

    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.

    0 讨论(0)
提交回复
热议问题