Search code examples
c++dllstructcen-xfs

Allocate multiple structs in memory


i need pass multiple values to memory, i need make various country to CEN/XFS.

This api: CashDispenser - CDM

Struct reference: WFSCDMCURRENCYEXP

How am i trying to do:

HRESULT WINAPI WFPGetInfo(HSERVICE hService, DWORD dwCategory, LPVOID lpQueryDetails, DWORD dwTimeOut, HWND hWnd, REQUESTID ReqID) {
WFSRESULT * lpWFSResult;
WFSCDMSTATUS CdmStatus;
WFSCDMCAPS CdmCapabilities; 
WFSCDMCASHUNIT CdmCash;
WFSCDMCURRENCYEXP CdmCurrency;
HRESULT result;

result = WFMAllocateBuffer(sizeof(WFSRESULT), WFS_MEM_ZEROINIT | WFS_MEM_SHARE, (void**)&lpWFSResult); 

    if(result != WFS_SUCCESS){
        return WFS_ERR_INTERNAL_ERROR;
    }

if(dwCategory == WFS_INF_CDM_CURRENCY_EXP){

    const int countCurrencies = 2;

    WFSCDMCURRENCYEXP** ppCdmCurrencies;

    result = WFMAllocateMore(sizeof(WFSCDMCURRENCYEXP*) * (countCurrencies+1), lpWFSResult, (void**)&ppCdmCurrencies);

    lpWFSResult->hService=hService;      
    lpWFSResult->RequestID=ReqID;
    lpWFSResult->u.dwEventID=WFS_INF_CDM_CURRENCY_EXP;
    lpWFSResult->hResult=WFS_SUCCESS;

    result = WFMAllocateMore(sizeof(WFSCDMCURRENCYEXP), lpWFSResult, (void**)&ppCdmCurrencies[0]);

    WFSCDMCURRENCYEXP& cmdCurrency0(*ppCdmCurrencies[0]);
    memcpy(cmdCurrency0.cCurrencyID, "AED", 3);
    cmdCurrency0.sExponent = 0;

    WFSCDMCURRENCYEXP& cmdCurrency1(*ppCdmCurrencies[1]);
    memcpy(cmdCurrency1.cCurrencyID, "AFA", 3);
    cmdCurrency1.sExponent = 0;

    lpWFSResult->lpBuffer = ppCdmCurrencies;
    logFile.close();
}
}

Solution

  • I think you try to handle WFS_INF_CDM_CURRENCY_EXP message to get information about currencies in your CDM.

    Read XFS specification carefully:

    Output Param LPWFSCDMCURRENCYEXP *lppCurrencyExp; Pointer to a NULL-terminated array of pointers to WFSCDMCURRENCYEXP structures

    This means that you must allocate an array of pointers to WFSCDMCURRENCYEXP with size N+1 and set last item to null.

    In CEN/XFS you cannot use standard new or malloc for memory allocation. You need to use WFMAllocateBuffer and WFMAllocateMore to allocate memory for XFS structures that you return to caller.

    For you task yo need something like this:

    HRESULT WINAPI WFPGetInfo(HSERVICE hService, DWORD dwCategory, LPVOID lpQueryDetails, DWORD dwTimeOut, HWND hWnd, REQUESTID ReqID) {
    
    WFSRESULT * lpWFSResult;
    WFSCDMSTATUS CdmStatus;
    WFSCDMCAPS CdmCapabilities; 
    WFSCDMCASHUNIT CdmCash;
    WFSCDMCURRENCYEXP CdmCurrency;
    HRESULT result;
    
    result = WFMAllocateBuffer(sizeof(WFSRESULT), WFS_MEM_ZEROINIT | WFS_MEM_SHARE, (void**)&lpWFSResult); 
    
        if(result != WFS_SUCCESS){
            return WFS_ERR_INTERNAL_ERROR;
        }
    
    if(dwCategory == WFS_INF_CDM_CURRENCY_EXP){
    
        const int countCurrencies = 2;
    
        WFSCDMCURRENCYEXP** ppCdmCurrencies;
        result = WFMAllocateBuffer(sizeof(WFSCDMCURRENCYEXP*) * (countCurrencies+1), WFS_MEM_ZEROINIT | WFS_MEM_SHARE, (void**)&ppCdmCurrencies);
    
        lpWFSResult->hService=hService;      
        lpWFSResult->RequestID=ReqID;
        lpWFSResult->u.dwEventID=WFS_INF_CDM_CURRENCY_EXP;
        lpWFSResult->hResult=WFS_SUCCESS;
    
        result = WFMAllocateMore(sizeof(WFSCDMCURRENCYEXP), lpWFSResult, (void**)&ppCdmCurrencies[0]);
    
        WFSCDMCURRENCYEXP& cmdCurrency0(*ppCdmCurrencies[0]);
        memcpy(cmdCurrency0.cCurrencyID, "AED", 3);
        cmdCurrency0.sExponent = 0;
    
        result = WFMAllocateMore(sizeof(WFSCDMCURRENCYEXP), lpWFSResult, (void**)&ppCdmCurrencies[1]);
    
        WFSCDMCURRENCYEXP& cmdCurrency1(*ppCdmCurrencies[1]);
        memcpy(cmdCurrency1.cCurrencyID, "AFA", 3);
        cmdCurrency1.sExponent = 0;
    
        lpWFSResult->lpBuffer = ppCdmCurrencies;
        logFile.close();
        return WFS_SUCCESS;
    }
    }
    

    That's not so simple to manipulate with XFS. It's too many complex API structures with different rules of allocation and data representation. Please read carefully XFS manuals. In the first book ftp://ftp.cen.eu/CWA/CEN/WS-XFS/CWA16926/CWA%2016926-1.pdf many conceptual things is described. About configuration, memory mangement and so on.