Can I 开发者_JAVA百科use void*
instead of LPVOID
in C?
Or LPVOID
perform some special functionality than void*
.
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.
LPVOID
is simply a Windows API typedef
for void*
.
LPVOID is
typedef void* LPVOID
defined in Windef.h
where every windows datatypes were defined.
We can use void* to point any type.
LPVOID is a pointer to any type. This type is declared in WinDef.h as follows: typedef void *LPVOID;
精彩评论