如何动态填充结构,该结构是实现xfs的C ++中指向数组指针的指针

时间:2015-05-12 07:16:41

标签: pointers structure xfs cen-xfs

结构1:

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

结构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;

结构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;      

代码:

 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;

我在检索结构1中的所有值时遇到困难。  我需要动态地将值添加到这些结构中并显示Structure1作为输出。需要为此完成内存分配。我已经尝试使用上面的代码来分配内存但是尽管分配的值没有正确存储在结构

usCount的值根据面额集而变化。基于此usNumPhysicalCUs设置。 此外,当我在&lpWFSCDMCuinf方法中发送WFSExecute时,lppPhysical似乎是空的。

我无法弄清楚我哪里出错了。

1 个答案:

答案 0 :(得分:2)

首先,您必须为每个块分配内存。 对于指针数组,您将分配内存来存储指针数,而不是分配内存中的每个指针,您必须为结构本身分配内存。 我以更简短的形式重写你的代码。没有错误检查,此代码仅为示例。

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
        // ..
        // ..
    }
}

除了这个示例之外,我建议你编写一些辅助函数来分配像WFSCDMCUINFO这样的XFS结构。在我自己的项目中,我使用了一些宏来使用WFMAllocate和WFMAllocateMore函数序列化内存中的XFS结构。 XFS结构非常复杂,因类而异。我写了一些宏来序列化和反序列化内存流和XFS内存缓冲区中的结构。在应用程序中,我使用堆alloc来在内存中存储XFS结构,但是当我需要在另一个XFS消息中返回结构时,我需要使用WFMAllocate和WFMAllocateMore将内存缓冲区传输到XFS内存。