struct and lpvoid
-
hi! can someone give me a sample code on how to pass 2 different structs to a function with lpvoid as parameter and how to convert back to the structs from lpvoid. the structs should be received by the lpvoid... how to combine the 2 different structs together? and how to split the lpvoid back to the 2 structs? the structs are something like typedef struct { int dataStructCount; DWORD totalSize; } strHeader; typedef struct { int num; lptstr str; } dataStruct; this will be used for writing and reading data from named pipe.. the header will be used to determine the count of dataStruct send/read. thanks for any help! newbie :)
-
hi! can someone give me a sample code on how to pass 2 different structs to a function with lpvoid as parameter and how to convert back to the structs from lpvoid. the structs should be received by the lpvoid... how to combine the 2 different structs together? and how to split the lpvoid back to the 2 structs? the structs are something like typedef struct { int dataStructCount; DWORD totalSize; } strHeader; typedef struct { int num; lptstr str; } dataStruct; this will be used for writing and reading data from named pipe.. the header will be used to determine the count of dataStruct send/read. thanks for any help! newbie :)
Newbie, First of all, you are in the wrong forum! This is the C++/CLI or Managed C++ forum. C++/CLI uses the .NET Framework and depends on Garbage Collection (GC) for memory management. It appears you are writing C or possibly C++ code. You should get better assistance int the Visual C++/MFC forum. However,
LPVOID
is really a typedef ofvoid*
. Thus,LPVOID
is a pointer to any type. Unfortunately, once a pointer of a type, in your casestrHeader
anddataStruct
, is casted to a void pointer,LPVOID
, its type information is lost. Thus, if you have a function that accepts void pointers, the compiler will not help you with type checking.void Foo(LPVOID header, LPVOID data) { strHeader* theHeader = (strHeader*) header; dataStruct* theData = (dataStruct*) data; // Do something with the data } int main() { strHeader header; dataStruct data; Foo(&strHeader, &dataStruct); // Rest of application ... return 0; }
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
Newbie, First of all, you are in the wrong forum! This is the C++/CLI or Managed C++ forum. C++/CLI uses the .NET Framework and depends on Garbage Collection (GC) for memory management. It appears you are writing C or possibly C++ code. You should get better assistance int the Visual C++/MFC forum. However,
LPVOID
is really a typedef ofvoid*
. Thus,LPVOID
is a pointer to any type. Unfortunately, once a pointer of a type, in your casestrHeader
anddataStruct
, is casted to a void pointer,LPVOID
, its type information is lost. Thus, if you have a function that accepts void pointers, the compiler will not help you with type checking.void Foo(LPVOID header, LPVOID data) { strHeader* theHeader = (strHeader*) header; dataStruct* theData = (dataStruct*) data; // Do something with the data } int main() { strHeader header; dataStruct data; Foo(&strHeader, &dataStruct); // Rest of application ... return 0; }
"We make a living by what we get, we make a life by what we give." --Winston Churchill
oooopppss! sorry! i did not notice the message board... i thought i was posting in vc++/mfc forum... i have reposted the qxn there... thanks for your reply! however, for the foo function... i only have 1 LPVOID paramter... thus the strHeader and dataStruct should be combined together and passed as 1 data to function Foo.... :( newbie :)