问题
While browsing some code I found a call to OpenPrinter(). The code compiles and works fine. But, we are passing a HANDLE
instead of LPHANDLE
(as specified in MSDN). I found out that in windef.h
the following declaration exists:
typedef HANDLE FAR *LPHANDLE;
What does LP stand for? Should I use a LPHANDLE
, or keep HANDLE
?
回答1:
LP stands for Long Pointer. It's a pointer to a handle in this case.
HANDLE h = <winapi function>();
LPHANDLE ph = &h;
You can use it the same way you would a handle by dereferencing the pointer:
HANDLE anotherh = *ph;
or
<winapi function>(*ph, ...);
回答2:
"LP" stands for Long Pointer.
HANDLE != LPHANDLE, just as DWORD != DWORD* (or LPDWORD)
回答3:
The FAR construct dates back to the days of the 8086/8088 CPU and the segmented memory modals that it used. In those day you could have NEAR and FAR pointers to memory.
The LP (long pointer) is just a hangover from the Microsoft Hungarian Notation from those earlier days.
These days the Win32 memory model is flat so NEAR and FAR pointers are basically the same. But while a near pointer is now the same as a far poitner that does not mean a pointer is the same as a handle.
回答4:
And just to give you a little background long pointers also called far pointers were different than normal 16 bit pointers in 16 bit windows. The OS used a segmented memory model where you would offset from a segment or have a segment + offset which was a long pointer. The hungarian notation LP was used for these long pointers and is still littered throughout the windows api for these legacy reasons.
Of course in the 32 bit and 64 bit windows OSs a flat memory model is used and there is no distinction to be made between pointers in these OSs (though PAE adds something conceptually similar).
来源:https://stackoverflow.com/questions/391649/lphandle-vs-handle