|
Prev: process.h
Next: static member
From: Eric Kaplan on 8 Apr 2008 19:27 In visual C++, there is datatype HANDLE void* HANDLE so void* means I can assign any datatype ? like int* string* byte* bool* ... 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?
From: Doug Harrison [MVP] on 8 Apr 2008 20:00 On Tue, 08 Apr 2008 16:27:34 -0700, Eric Kaplan <tobycraftse(a)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
From: Joseph M. Newcomer on 8 Apr 2008 20:08 Handles are handles. They are not void*. Take a look, for example, at line 335 of WinNT.h, where the macro used to declare various types of specialized handle types is declared. A HANDLE type would generally be inappropriate for holding a pointer. You should not use it to hold general pointers. You have not specified the return type of myfunction. If you want a generic pointer hold, you can use LPVOID/void*, but generally there is little reason to use this. While a HANDLE type *can* hold a pointer, it would not be an appropriate type for holding a pointer. What you wrote was void * HANDLE which would be a declaration of a variable whose name is HANDLE whose type is void*. I'm sure you had meant to say On line 334 of WinNT.h, there is a line that says typedef void * HANDLE; but you didn't say that. However, it is still inappropriate to use HANDLE as a substitute for LPVOID/void *. Why would you want to use a type HANDLE when there is already a type LPVOID that is far more appropriate? joe On Tue, 08 Apr 2008 16:27:34 -0700, Eric Kaplan <tobycraftse(a)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* ... > >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? Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Dan Bloomquist on 8 Apr 2008 20:17 Eric Kaplan wrote: > In visual C++, there is datatype HANDLE > > void* HANDLE > > so void* means I can assign any datatype ? like > int* > string* > byte* > bool* ... > > 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? > HANDLE has a specific meaning. Handles are 'IDs', not memory pointers. They may be pointers for whatever interface you are using, but it is for memory you have no business with. HANDLE, HWND, HGLOBAL should only be treated as identifiers to objects that are beyond your memory realm. HGLOBAL hText= GlobalAlloc(...) hT is a handle to some memory, you don't know where. TCHAR *ptr= (TCHAR *)GlobalLock( hText ); Now ptr is a memory pointer to the memory that hText represents. Now you can: _tcscpy( ptr, str ); But once you: GlobalUnlock( hText ); The actual memory is gone to you, all you have is a 'handle' for that memory. Best, Dan.
From: Eric Kaplan on 8 Apr 2008 20:17
then HANDLE is similar to "file handle"? which is unique identifier for a file, for a process, for a thread ... etc which is returned by a function when it's creating something - like creating a thread, creating a process, creating a file ... etc >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, |