473,804 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10603
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******** ******@tk2msftn gp13.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)(HWN D hWnd, UINT uMsg, WPARAM wParam, LPARAM
lParam);

.....

wc.lpfnWndProc = (WNDPROC)(void *)(CWNDPROC)m_W ndProc;

.....

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**********@d iscussions.micr osoft.com> wrote in message
news:69******** *************** ***********@mic rosoft.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(G WL_WNDPROC) for instance.)
Nov 17 '05 #7
"Tim Robinson" <ti************ **********@nowh ere.com> wrote in message
news:38******** *****@individua l.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
12736
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 "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
67
4288
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. Unfortunately these ad hominem rhetorts are frequently introduced into purely technical discussions on the feasibility of supporting such functionality in C++. That usually serves to divert the discussion from the technical subject to a discussion of the...
2
1386
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 two pages add another similar set of data. This successful.aspx webpage is shared by a number of webforms, but some data entry procedures use one webform only, some require two webforms. In these instance i want to navigate back to the first
6
1627
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 Access 2003. I think this will be fairly simple for an experienced access person.
7
2198
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 another. I have tried declaring them as shared, public, friend, etc and I always get an error stating that something is not valid on a local variable declaration. For example, in the following code for Sub DataGrid_Select, I have CurrentID and...
3
2047
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 CanonML and ampliations and improvements over TeX, SGML, XML or Scheme based encodings are listed below. However, let me an incise first. Rememeber how we also saw that the mathematics in Distler's blog Musings were being incorrectly encoded with...
22
3676
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 dissect the nature and characterization of such “unix philosophy”, as have been described by Brian Kernighan, Rob Pike, Dennis Ritchie, Ken Thompson, and Richard P Gabriel et al, and in recent years by Eric Raymond.
206
8392
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 footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
7
2305
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 that info, but somewhere I've gone completely off the rails! Basically it is a room availability page for an intranet and should simply colour a calendar cell red if the room is booked, or green if it isn't. Rooms are typically booked by lecturers...
0
9714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10351
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6866
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.