Home winapi LPVOID type and how to use it (C++)

LPVOID type and how to use it (C++)





I recently started working with data transmission channels, well, those that are NamedPipe.

The functions of writing and reading to the channel, if we consider the very basis, look like this:

WriteFile (HANDLE hFile, LPVOID lpBuffer, ...);
ReadFile (HANDLE hFile, LPVOID lpBuffer, ...);

With the first parameter, everything is clear, this is the channel to which we write, but here is the second. The second parameter of type LPVOID judging from the description in Windows data types there is a pointer to any type.

However, except for char , nothing can be written to the channel (the same with reading), the compiler says that it cannot convert, say, int to LPVOID .

Question, how to work with LPVOID ? Maybe we need to translate int into it or something else?
And why then does the compiler not swear at char ? (Only if this cahr is declared like this: char * X = new char [const]; )

Another question: You can write to a stream of any type. How can I read from a stream into a variable of any type? Nothing works, only in char it is possible, which is declared through new char

Answer 1, authority 100%

In general, it is clear that LPVOID is a pointer. Moreover, this is a pointer (P from P ointer) to anything (VOID), and even a distant one (L – from L ong). Those. it turns out like this:

typedef void far * LPVOID;

But, first of all, I’m not sure if the far specifier is currently relevant on Windows, because a flat memory model is used. In earlier versions of compilers and operating systems, this probably mattered. I remember the torment with Borland 3.1, in which the memory model played a very large role. Secondly, it is wrong to cast your pointers to a type other than LPVOID when calling the specified system functions. The fact is that in the next versions of the OS and development tools, this type can be overridden and then cranks. Therefore, it is correct to do this:

int * blablabla; // our pointer
LPVOID lpData;
lpData = (LPVOID) blablabla;
// or more correctly lpData = static_cast & lt; LPVOID & gt; (blablabla);

Answer 2, authority 83%

Is it like a normal void *?

// create a variable to which our pointer will refer
int var = 5;
std :: cout & lt; & lt; "\ nvar =" & lt; & lt; (int) var;
// create a pointer to an undefined type
void * pointer;
pointer = & amp; var; // put in the pointer the address of the var variable
// now cast the pointer to int type and access by value
std :: cout & lt; & lt; "\ npointer =" & lt; & lt; * (int *) pointer;

As a result, we should get output 5

// cast a void * pointer to an int * pointer
int * pointer_int = (int *) pointer;
// dereference the int * pointer. That is
// access by value
int var2 = * pointer_int;
std :: cout & lt; & lt; "\ nvar2 =" & lt; & lt; var2;
// So it looks like this:
// pointer - & gt; (int *) pointer resulted in required - & gt; * (int *) pointer dereferenced a coded pointer.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions