468,752 Members | 1,577 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,752 developers. It's quick & easy.

LPVOID : Philosophy lesson needed : Can you offer a small history lesson for an aspiring C++ programmer?

I have been doing some C++ Interop using the new VS2005 (June Beta).
I am exposing these methods to .NET clients.

I ran into some WinAPI methods which use LPVOID types, and I don't
understand the philosophy behind this type.

What I don't get is why doesn't a person pass in a pointer to the datatype
they need, instead of LPVOID.

Can you provide a simple example to demonstrate how the LPVOID, was/is
generally used in WinAPI programming?

Thanks
Russell Mangel
Las Vegas, NV

P.S. The philosophy of why LPVOID types were used in WinAPI programming, is
most important to me.
Nov 17 '05 #1
7 10370
Russell Mangel wrote:
[...]
P.S. The philosophy of why LPVOID types were used in WinAPI programming, is
most important to me.


LPVOID is just a typedef for void*. (P = pointer, L = because in the
16-bit days, it was declared as a long 32-bit pointer, i.e. it could
point to any data segment, not just the data segment belonging to your
program.)

void* can hold a pointer to any piece of data, so it's used in C where
you might use Object in .NET. However, there's no real equivalent in
..NET, although the Marshal class has various functions for manipulating
these pointers.

--
Tim Robinson (MVP, Windows SDK)
http://mobius.sourceforge.net/
Nov 17 '05 #2
Russell Mangel wrote:
I have been doing some C++ Interop using the new VS2005
(June Beta).
I am exposing these methods to .NET clients.

I ran into some WinAPI methods which use LPVOID types,
and I don't understand the philosophy behind this type.

What I don't get is why doesn't a person pass in a
pointer to the datatype they need, instead of LPVOID.

Can you provide a simple example to demonstrate how the
LPVOID, was/is generally used in WinAPI programming?


A function argument is usually an LPVOID when you need to
support different types for that same argument. It covers
the same role as a generic reference to System.Object in
..NET since most other pointer types are implicitly converted
to LPVOID.

In C/C++ you can actually use almost any type in a generic
way as long as its size is not smaller than the pointer
size, but using a pointer instead of an integral type has
some syntactical and logical advantages.
--

// Alessandro Angeli
// MVP :: Digital Media
// a dot angeli at psynet dot net
Nov 17 '05 #3
On Sat, 5 Mar 2005 01:58:14 -0800, "Russell Mangel"
<ru*****@tymer.net> wrote:
I have been doing some C++ Interop using the new VS2005 (June Beta).
I am exposing these methods to .NET clients.

I ran into some WinAPI methods which use LPVOID types, and I don't
understand the philosophy behind this type.

What I don't get is why doesn't a person pass in a pointer to the datatype
they need, instead of LPVOID.

Can you provide a simple example to demonstrate how the LPVOID, was/is
generally used in WinAPI programming?

P.S. The philosophy of why LPVOID types were used in WinAPI programming, is
most important to me.


The Win API was written for C, where void* is an important and very
useful construct: any pointer can be converted to void* and
vice-versa. (No cast is even required).

In C, this is good (and preferred code):

unsigned char *buffer = malloc(BUF_SIZE);

No cast is required, and is in fact wrong, since casting the return of
malloc will mask the error of forgetting to include the proper header.
(In C function prototypes are not required.)

IMO, the Windows API uses LPVOID in many cases to avoid bloating the
API even further; you pass a "type of data" that you want returned,
and a pointer to an area (and size) to receive it. For example, the
GDI routine GetObject would have to become several different calls
(GetBitmap, GetBrush, GetPen, GetCursor, etc.), rather than the single
call.)

--
Sev
Nov 17 '05 #4

"Russell Mangel" <ru*****@tymer.net> wrote in message
news:em**************@tk2msftngp13.phx.gbl...
I have been doing some C++ Interop using the new VS2005 (June Beta).
I am exposing these methods to .NET clients.

I ran into some WinAPI methods which use LPVOID types, and I don't
understand the philosophy behind this type.

What I don't get is why doesn't a person pass in a pointer to the datatype
they need, instead of LPVOID.

Can you provide a simple example to demonstrate how the LPVOID, was/is
generally used in WinAPI programming?

Thanks
Russell Mangel
Las Vegas, NV

P.S. The philosophy of why LPVOID types were used in WinAPI programming, is most important to me.


qsort is a good example. When you want to sort something, it's up to you to
define the sort order. You do this by defining a function that takes two
void * parameters. You have to do a typecast in the function. This comes in
handy if you want to make some unusual sort. Lets say you have an array with
first name and last name. You can sort on last name in ascending order and
first name in descending order. This means that Zeke Smith comes before
Aaron Smith. In this case, the pointers will point at two ie´tems in this
structure.

You should never use void * if you know the datatype, like you say. With
qsort, you can't since it can be used to sort any kind of data.

http://www.cplusplus.com/ref/cstdlib/qsort.html

/Fredrik

Nov 17 '05 #5
LPVOID = void *

Basically, void * can point to anything, and thus be very useful. For
example, you might be writing data to disk, using fwrite or WriteFile or
whatever the Win32 function call is, and it could be an array of int's, or
CMyClass's or whatever. void * is insanely useful mainly because you can
typecast to/from the pointer for nearly everything. The only thing it can't
do is non static class functions. Last time I tried to typecast a class
function I came up with this:

.....

typedef LRESULT (*CWNDPROC)(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM
lParam);

.....

wc.lpfnWndProc = (WNDPROC)(void *)(CWNDPROC)m_WndProc;

.....

Now this didn't work, mainly because I wasn't thinking properly, because
m_WndProc is __thiscall, and it should have had const CWindow *this attached
to the end of the CWNDPROC typedef. And since then, I've learnt some
assembler and I've decided against trying to keep a virtual window procedure
inside my window class, because it just might not be too healthy for the
stack. If I'm wrong, email me at kawahee AT gmail DOT com.

-- Best of luck

Todd Aspeotis

"Russell Mangel" wrote:
I have been doing some C++ Interop using the new VS2005 (June Beta).
I am exposing these methods to .NET clients.

I ran into some WinAPI methods which use LPVOID types, and I don't
understand the philosophy behind this type.

What I don't get is why doesn't a person pass in a pointer to the datatype
they need, instead of LPVOID.

Can you provide a simple example to demonstrate how the LPVOID, was/is
generally used in WinAPI programming?

Thanks
Russell Mangel
Las Vegas, NV

P.S. The philosophy of why LPVOID types were used in WinAPI programming, is
most important to me.

Nov 17 '05 #6
"Todd Aspeotis" <To**********@discussions.microsoft.com> wrote in message
news:69**********************************@microsof t.com...
[...]
The only thing it can't
do is non static class functions. Last time I tried to typecast a class
function I came up with this:

[...]

It can't do regular functions, either: the standard lets void* contain only
pointers to data. It just happens to work for static functions on x86 with
VC++. (Unfortunately Win32 relies on this behaviour: see
SetWindowLong(GWL_WNDPROC) for instance.)
Nov 17 '05 #7
"Tim Robinson" <ti**********************@nowhere.com> wrote in message
news:38*************@individual.net...
Russell Mangel wrote:
[...]
P.S. The philosophy of why LPVOID types were used in WinAPI programming, is most important to me.


LPVOID is just a typedef for void*. (P = pointer, L = because in the
16-bit days, it was declared as a long 32-bit pointer, i.e. it could
point to any data segment, not just the data segment belonging to your
program.)

void* can hold a pointer to any piece of data, so it's used in C where
you might use Object in .NET. However, there's no real equivalent in
.NET, although the Marshal class has various functions for manipulating
these pointers.

--
Tim Robinson (MVP, Windows SDK)
http://mobius.sourceforge.net/


Tim,

There is System::IntPtr which models the handle (or LPVOID). Only used for
interop.

Cheers,
---
Tom Tempelaere
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

226 posts views Thread by Stephen C. Waterbury | last post: by
67 posts views Thread by Steven T. Hatton | last post: by
7 posts views Thread by William (Tamarside) | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.