Re: HANDLE
On Tue, 08 Apr 2008 16:27:34 -0700, Eric Kaplan <tobycraftse@yahoo.com>
wrote:
In visual C++, there is datatype HANDLE
void* HANDLE
so void* means I can assign any datatype ? like
int*
string*
byte*
bool* ...
You could, but you shouldn't.
and for HANDLE, is that use only for holding another pointer? like -
HANDLE hand = myfunction();
why we need to use HANDLE to hold pointer? can we just define a
pointer?
The only thing you use HANDLE for is storing HANDLE values. For example,
CreateFile, CreateMutex, and other functions return HANDLEs. Of course,
these HANDLEs identify very different things, but some functions, like
WaitForMultipleObjects, accept a variety of different objects, all
represented by the same type, HANDLE. This is about the best you can do in
C. In C++, you can use derivation to distinguish waitable from unwaitable
HANDLE, which would prevent passing unwaitable handles to functions that
require waitable handles, and since you'd be using classes to represent
files, mutexes, etc, you wouldn't be able to pass a mutex handle to a file
function, and so forth, because they wouldn't be defined in terms of
handles. In a sense, HANDLE is a type-unsafe "this" pointer for a set of
classes whose member functions are all global functions. Other Windows
handle types are defined as pointers to empty structs, so you can't pass an
HWND where an HDC is expected or vice versa.
--
Doug Harrison
Visual C++ MVP