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;
c++ dll struct cen-xfs
add a comment |
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;
c++ dll struct cen-xfs
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 thatsizeof(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 syntaxtypedef struct _wfs_pin_caps { /* ... */ } WFSPINCAPS, * LPWFSPINCAPS;looks like it's C code, but on the other hand, you're usingvectors, which are c++. Also, your code looks like it will go on a microcontroller. Are you sure you can use STL containers (likevector) on your microcontroller? I know thatavr_libcdoesn't allow it because doing amallocis 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
add a comment |
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;
c++ dll struct cen-xfs
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
c++ dll struct cen-xfs
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 thatsizeof(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 syntaxtypedef struct _wfs_pin_caps { /* ... */ } WFSPINCAPS, * LPWFSPINCAPS;looks like it's C code, but on the other hand, you're usingvectors, which are c++. Also, your code looks like it will go on a microcontroller. Are you sure you can use STL containers (likevector) on your microcontroller? I know thatavr_libcdoesn't allow it because doing amallocis 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
add a comment |
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 thatsizeof(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 syntaxtypedef struct _wfs_pin_caps { /* ... */ } WFSPINCAPS, * LPWFSPINCAPS;looks like it's C code, but on the other hand, you're usingvectors, which are c++. Also, your code looks like it will go on a microcontroller. Are you sure you can use STL containers (likevector) on your microcontroller? I know thatavr_libcdoesn't allow it because doing amallocis 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
add a comment |
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.
Thanks and sorry :(
– Matheus Cardozo
Mar 6 '17 at 16:08
add a comment |
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.
Thanks and sorry :(
– Matheus Cardozo
Mar 6 '17 at 16:08
add a comment |
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.
Thanks and sorry :(
– Matheus Cardozo
Mar 6 '17 at 16:08
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 usingvectors, which are c++. Also, your code looks like it will go on a microcontroller. Are you sure you can use STL containers (likevector) on your microcontroller? I know thatavr_libcdoesn't allow it because doing amallocis 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