Put multiple values in a single attribute of a struct











up vote
-1
down vote

favorite












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;









share|improve this question
























  • Can you reproduce the problem, maybe with less code?
    – silentboy
    Mar 4 '17 at 14:30










  • i edit question
    – Matheus Cardozo
    Mar 4 '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 4 '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 vectors, 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.
    – Anthony D.
    Mar 4 '17 at 14:43












  • You must allocate XFS memory for lpszExtra too as you allocate it for WFSPINCAPS.
    – Alex.D.Alexeev
    Mar 5 '17 at 14:11















up vote
-1
down vote

favorite












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;









share|improve this question
























  • Can you reproduce the problem, maybe with less code?
    – silentboy
    Mar 4 '17 at 14:30










  • i edit question
    – Matheus Cardozo
    Mar 4 '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 4 '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 vectors, 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.
    – Anthony D.
    Mar 4 '17 at 14:43












  • You must allocate XFS memory for lpszExtra too as you allocate it for WFSPINCAPS.
    – Alex.D.Alexeev
    Mar 5 '17 at 14:11













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











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;









share|improve this question















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++ dll struct cen-xfs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 6:27









Cœur

17k9102140




17k9102140










asked Mar 4 '17 at 14:28









Matheus Cardozo

157




157












  • Can you reproduce the problem, maybe with less code?
    – silentboy
    Mar 4 '17 at 14:30










  • i edit question
    – Matheus Cardozo
    Mar 4 '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 4 '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 vectors, 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.
    – Anthony D.
    Mar 4 '17 at 14:43












  • You must allocate XFS memory for lpszExtra too as you allocate it for WFSPINCAPS.
    – Alex.D.Alexeev
    Mar 5 '17 at 14:11


















  • Can you reproduce the problem, maybe with less code?
    – silentboy
    Mar 4 '17 at 14:30










  • i edit question
    – Matheus Cardozo
    Mar 4 '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 4 '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 vectors, 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.
    – Anthony D.
    Mar 4 '17 at 14:43












  • You must allocate XFS memory for lpszExtra too as you allocate it for WFSPINCAPS.
    – Alex.D.Alexeev
    Mar 5 '17 at 14:11
















Can you reproduce the problem, maybe with less code?
– silentboy
Mar 4 '17 at 14:30




Can you reproduce the problem, maybe with less code?
– silentboy
Mar 4 '17 at 14:30












i edit question
– Matheus Cardozo
Mar 4 '17 at 14:33






i edit question
– Matheus Cardozo
Mar 4 '17 at 14:33






2




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 4 '17 at 14:37




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 4 '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 vectors, 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.
– Anthony D.
Mar 4 '17 at 14:43






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 vectors, 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.
– Anthony D.
Mar 4 '17 at 14:43














You must allocate XFS memory for lpszExtra too as you allocate it for WFSPINCAPS.
– Alex.D.Alexeev
Mar 5 '17 at 14:11




You must allocate XFS memory for lpszExtra too as you allocate it for WFSPINCAPS.
– Alex.D.Alexeev
Mar 5 '17 at 14:11












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










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.






share|improve this answer























  • Thanks and sorry :(
    – Matheus Cardozo
    Mar 6 '17 at 16:08











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42597131%2fput-multiple-values-in-a-single-attribute-of-a-struct%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










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.






share|improve this answer























  • Thanks and sorry :(
    – Matheus Cardozo
    Mar 6 '17 at 16:08















up vote
0
down vote



accepted










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.






share|improve this answer























  • Thanks and sorry :(
    – Matheus Cardozo
    Mar 6 '17 at 16:08













up vote
0
down vote



accepted







up vote
0
down vote



accepted






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited May 23 '17 at 10:30









Community

11




11










answered Mar 5 '17 at 3:09









WPomier

788411




788411












  • Thanks and sorry :(
    – Matheus Cardozo
    Mar 6 '17 at 16:08


















  • Thanks and sorry :(
    – Matheus Cardozo
    Mar 6 '17 at 16:08
















Thanks and sorry :(
– Matheus Cardozo
Mar 6 '17 at 16:08




Thanks and sorry :(
– Matheus Cardozo
Mar 6 '17 at 16:08


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42597131%2fput-multiple-values-in-a-single-attribute-of-a-struct%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly