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

R

Russell Mangel

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.
 
T

Tim Robinson

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.
 
A

Alessandro Angeli [MVP::DigitalMedia]

Russell said:
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.
 
S

Severian

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.)
 
F

Fredrik Wahlgren

Russell Mangel said:
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
 
G

Guest

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
 
T

Tim Robinson

[...]
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.)
 
T

TT \(Tom Tempelaere\)

Tim Robinson said:
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,

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

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top