将多个值放在结构的单个属性中

时间:2017-03-04 14:28:25

标签: c++ dll struct cen-xfs

我需要在结构的单个属性中放置多个值,接收值的属性是LPSTR,我试图将所有这些作为向量传递,编译,但它不能像我一样工作我想。

我的结构:

typedef struct _wfs_pin_caps
{
WORD                wClass;
WORD                fwType;
............More...............
BOOL                bIDConnect;
WORD                fwIDKey;
WORD                fwValidationAlgorithms;
WORD                fwKeyCheckModes;
LPSTR               lpszExtra; //This attribute must receive more than one value 
} WFSPINCAPS, * LPWFSPINCAPS;

正如我试图做的那样:

HRESULT WINAPI WFPGetInfo(HSERVICE hService, DWORD dwCategory, LPVOID   lpQueryDetails, DWORD dwTimeOut, HWND hWnd, REQUESTID ReqID) {

    ...

    result = WFMAllocateMore(sizeof(WFSPINCAPS), lpWFSResult, &lpWFSResult->lpBuffer);

    ...

    //This Values
    vector<LPSTR> Tokens;
        Tokens[1] = (LPSTR)"Value1";
        Tokens[2] = (LPSTR)"Value2";
        Tokens[3] = (LPSTR)"Value4";
        Tokens[4] = (LPSTR)"Value5";

        PinCapabilities.lpszExtra = (LPSTR)&Tokens; //Pass HERE

    memcpy(lpWFSResult->lpBuffer,&PinCapabilities,sizeof(WFSPINCAPS));

    ... 
return WFS_SUCCESS;

1 个答案:

答案 0 :(得分:0)

您的问题非常清楚,但如果我理解,问题是您将lpszExtra设置为本地向量Tokens(存储在堆栈中)并且最终将被销毁那个功能。

一种方法是在堆中创建向量,如下所示:

// Create a new vector in the heap of 5 elements (0..4)
vector<LPSTR> &Tokens = *new vector<LPSTR>(5); 
Tokens[1] = (LPSTR) "Value1";
Tokens[2] = (LPSTR) "Value2";
Tokens[3] = (LPSTR) "Value4";
Tokens[4] = (LPSTR) "Value5";

PinCapabilities.lpszExtra = (LPSTR) &Tokens; //Pass HERE

// Assuming that lpBuffer has room for a WFSPINCAPS structure
memcpy(lpWFSResult->lpBuffer, &PinCapabilities, sizeof(WFSPINCAPS));

现在((LPWFSPINCAPS)lpWFSResult->lpBuffer)->lpszExtra包含一个指向vector的有效指针,可以在任何其他函数中使用,如下所示:

LPWFSPINCAPS pPinCapabilities = (LPWFSPINCAPS) lpWFSResult->lpBuffer;
vector<LPSTR> &Tokens = *(vector<LPSTR> *) pPinCapabilities->lpszExtra;
LPSTR str = Tokens[3]; // Will get "Value4"

但不要忘记,在某些时候你必须释放矢量的记忆:

LPWFSPINCAPS pPinCapabilities2 = (LPWFSPINCAPS) lpWFSResult->lpBuffer;
delete (vector<LPSTR> *) pPinCapabilities2->lpszExtra;

请下次尝试create a Minimal, Complete, and Verifiable example帮助我们为您提供帮助。