51

Can I use void* instead of LPVOID in C?

Or LPVOID perform some special functionality than void*.

Alexander
  • 23,432
  • 11
  • 63
  • 73
Siddiqui
  • 7,662
  • 17
  • 81
  • 129

4 Answers4

114

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.

David G
  • 94,763
  • 41
  • 167
  • 253
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
16

LPVOID is simply a Windows API typedef for void*.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 1
    Anyone know the history of why this typedef was useful? I suspect it had some relationship with 16 vs 32 bit. – Todd Stout Jan 01 '10 at 05:42
  • I haven't been a Windows programmer back then but I doubt there's so much history about it. Windows has a bunch of LPxyz data types and probably, this is provided for consistency with those names. – Mehrdad Afshari Jan 01 '10 at 05:44
  • 8
    @Todd: Naw. The windows developers felt they had to have SHOUTing types for all the system call parameters. – wallyk Jan 01 '10 at 05:45
  • FWIW: "LP" stands for "long pointer". It's a remnant of the horrible Hungarian notation days. – Asaph Jan 01 '10 at 05:51
  • 7
    Back on 16-bit 8086, long (far) pointers and short (near) pointers were different: the former was a 32-bit value of segment+offset, and the latter was a 16-bit value of offset (thus could only point to within the same segment). Nowadays there's no distinction between long pointers and any other kind of pointer. – ephemient Jan 01 '10 at 05:57
  • long vs short...16 vs 32 bit? – Todd Stout Jan 01 '10 at 05:59
  • 2
    "long/short pointer" is misleading ("far/near" is clearer); it has nothing to do with the datum being pointed at, but rather the pointer representation. A 16-bit system with segmented addressing can access more than 2^16 bytes of address space, but anything outside of the current segment cannot be accessed without a segment prefix. x86-32 and x86-64 use flat addressing (PAE is weird and sorta gives you longer physical addresses, but virtual addresses are still 32 bits), so this is no longer relevant, but the naming was established with Win16. – ephemient Jan 01 '10 at 06:07
  • It seems my original assertion has been unofficially confirmed. – Todd Stout Jan 01 '10 at 06:08
  • @ephemient: IA32 architecture actually supports segmented addressing but virtually all modern operating systems initialize segment base registers to 0 and effectively make a flat address space available. – Mehrdad Afshari Jan 01 '10 at 06:18
  • Right, it's possible use segments in 32-bit mode it but you break compatibility with every library in your environment and more :) (Of course, if you do everything yourself, you can use segments, as Google NaCl does.) x86-64 completely ignores the segment registers, huzzah. – ephemient Jan 01 '10 at 06:42
3

LPVOID is

typedef void* LPVOID

defined in Windef.h where every windows datatypes were defined.

We can use void* to point any type.

Wickkiey
  • 4,446
  • 2
  • 39
  • 46
-1

LPVOID is a pointer to any type. This type is declared in WinDef.h as follows: typedef void *LPVOID;

anshul garg
  • 473
  • 3
  • 7
  • 19
    Just curious, why would you answer a really old question with an answer much less informative than an already accepted answer? – Anish Ramaswamy Apr 18 '13 at 09:47