-1

I need put multiple values ​​in a single attribute of a struct, the attribute that will receive the values ​​is LPSTR, I was trying to pass all this as a vector, compile, but it does not work as I would like.

My struct:

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;

As I'm trying to do:

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;
Cœur
  • 37,241
  • 25
  • 195
  • 267
Matheus Cardozo
  • 135
  • 1
  • 7
  • Can you reproduce the problem, maybe with less code? – Shakil Ahamed Mar 04 '17 at 14:30
  • i edit question – Matheus Cardozo Mar 04 '17 at 14:33
  • 2
    It's not clear what you are trying to do, but off the top, be aware that `sizeof(WFSPINCAPS)` is a constant determined at compile time. It will not magically increase to incorporate the lengths of `"Value1"` et al. – Igor Tandetnik Mar 04 '17 at 14:37
  • Like @Igor said, the code is a bit obscure because you have several global variables and no comments, but IMO something just feels fishy here. The syntax `typedef struct _wfs_pin_caps { /* ... */ } WFSPINCAPS, * LPWFSPINCAPS;` looks like it's C code, but on the other hand, you're using `vector`s, which are c++. Also, your code looks like it will go on a microcontroller. Are you sure you can use STL containers (like `vector`) on your microcontroller? I know that `avr_libc` doesn't allow it because doing a `malloc` is often complicated when you don't have an Operating System. – adentinger Mar 04 '17 at 14:43
  • You must allocate XFS memory for lpszExtra too as you allocate it for WFSPINCAPS. – Alex.D.Alexeev Mar 05 '17 at 14:11

1 Answers1

0

Your question is very unclear, but if I understand it, the problem is that you are setting lpszExtra to a local vector Tokens (stored in the stack) and that will be destroyed at the end of that function.

One way would be creating the vector in the heap like this:

// 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));

Now the ((LPWFSPINCAPS)lpWFSResult->lpBuffer)->lpszExtra contains a valid pointer to a vector that can be used in any other function like this:

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

But don't forget that in some point you will have to release the vector's memory:

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

And please next time try to create a Minimal, Complete, and Verifiable example to help us to help you.

Community
  • 1
  • 1
Wilfredo Pomier
  • 1,091
  • 9
  • 12