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



结构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中的所有值时,我会卡住。 我需要将值动态添加到这些结构和显示结构1中。结构。

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

我无法确切地弄清楚我在哪里弄错了。

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

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

在此样本的其他建议中,我建议您编写一些辅助功能,以分配XFS结构,例如WFSCDMCUINFO。在我自己的项目中,我使用了一些宏来使用wfmallocate和wfmallocatemore函数在内存中序列化XFS结构。XFS结构是如此复杂,并且不同于类别。我写了一些宏来在内存流和XFS内存缓冲器中序列化和估算结构。在应用程序中,我使用Heap Alloc将XFS结构存储在存储器中,但是当我需要在另一个XFS消息中返回结构时,我需要将内存缓冲器传输到使用wfmallocate和wfmallocatemore。

相关内容

  • 没有找到相关文章

最新更新