2

结构一:

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

结构二:

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;

结构三:

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设置。此外,当我&lpWFSCDMCuinfWFSExecute方法内发送时,它lppPhysical似乎是空的。

我不能完全弄清楚我哪里错了。

4

1 回答 1

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

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

于 2015-05-12T15:49:02.953 回答