473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 10581
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
2
by: Assimalyst | last post by:
Hi, I have a situation where users enter data, then on completion are directed to successful.aspx. On this page i essentially want two links, one to go to homepage, the other to link back one or...
6
by: busheman | last post by:
Have database that was generated on commercial software using an access 97 jet. Have converted the data to Access 2003 and need forms 2-3, queries, etc. simular to the original software, but now in...
7
by: Aaron | last post by:
Complete code follows. I am new to .NET programming (and programming in general) and I am having a difficult time understanding how to fill a variable in one sub, and then access it from...
3
by: Juan R. | last post by:
In http://canonicalscience.blogspot.com/2006/04/scientific-language-canonml-is.html] I presented some generic requirements for a markup language for science and mathematics. Basic features of...
22
by: Xah Lee | last post by:
The Nature of the “Unix Philosophy” Xah Lee, 2006-05 In the computing industry, especially among unix community, we often hear that there's a “Unix Philosophy”. In this essay, i...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
7
by: William (Tamarside) | last post by:
Please, if you have the time and knowledge to help me I'd truly appreciate it! I need to build a calendar page that displays available/unavailable info from a DB and colour a cell according to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.