-1

我需要将多个值放在一个结构的单个属性中,将接收值的属性是 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;
4

1 回答 1

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包含一个指向 a 的有效指针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;

请下次尝试创建一个最小、完整和可验证的示例来帮助我们帮助您。

于 2017-03-05T03:09:22.367 回答