Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between LPVOID and void*

Tags:

c

Can I use void* instead of LPVOID in C?

Or LPVOID perform some special functionality than void*.

like image 275
Siddiqui Avatar asked Jan 01 '10 05:01

Siddiqui


People also ask

What is an Lpvoid?

LPVOID. LPVOID data types are defined as being a "pointer to a void object". This may seem strange to some people, but the ANSI-C standard allows for generic pointers to be defined as "void*" types. This means that LPVOID pointers can be used to point to any type of object, without creating a compiler error.

Is void the same as ()?

It seems like () and Void both mean exactly the same thing (the type Void) when used in the context of a type declaration, but in all other contexts, () instead means an instance of the type Void, ie the same as Void().

How to use LPVOID?

LPVOID is a typedef to void*. You can implicitly convert any pointer to void* in C++, and implicitly convert between any pointer types in C. If you have a function that takes an LPVOID as a parameter, just pass it your pointer, with no cast, and it should work just fine.


2 Answers

There is no LPVOID type in C, it's a Windows thing.

And the reason those sort of things exists is so that the underlying types can change from release to release without affecting your source code.

For example, let's say early versions of Microsoft's C compiler had a 16-bit int and a 32-bit long. They could simply use:

typedef long INT32 

and, voila, you have your 32-bit integer type.

Now let's go forward a few years to a time where Microsoft C uses a 32-bit int and a 64-bit long. In order to still have your source code function correctly, they simply change the typedef line to read:

typedef int INT32 

This is in contrast to what you'd have to do if you were using long for your 32-bit integer types. You'd have to go through all your source code and ensure that you changed your own definitions.

It's much cleaner from a compatibility viewpoint (compatibility between different versions of Windows) to use Microsoft's data types.

In answer to your specific question, it's probably okay to use void* instead of LPVOID provided the definition of LPVOID is not expected to change.

But I wouldn't, just in case. You never know if Microsoft may introduce some different way of handling generic pointers in future that would change the definition of LPVOID. You don't really lose anything by using Microsoft's type but you could be required to do some work in future if they change the definition and you've decided to use the underlying type.

You may not think pointers would be immune to this sort of change but, in the original 8088 days when Windows was created, there were all sorts of weirdness with pointers and memory models (tiny, small, large, huge et al) which allowed pointers to be of varying sizes even within the same environment.

like image 195
paxdiablo Avatar answered Oct 03 '22 00:10

paxdiablo


LPVOID is simply a Windows API typedef for void*.

like image 29
mmx Avatar answered Oct 03 '22 01:10

mmx