I've found this useful method on catch22.net, which converts a string to a handle.
The method is written using char instead of LPCTSTR.
How do I convert it so that it uses LPCTSTR?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
HANDLE StringToHandle(char *szText, int nTextLen)
// if text length is -1 then treat as a nul-terminated string
if(nTextLen == -1)
nTextLen = lstrlen(szText);
// allocate and lock a global memory buffer. Make it fixed
// data so we don't have to use GlobalLock
ptr = (void *)GlobalAlloc(GMEM_FIXED, nTextLen + 1);
// copy the string into the buffer
memcpy(ptr, szText, nTextLen);
ptr[nTextLen] = '\0';
In fact I suspect that in most cases that a HANDLE is actually a function pointer returned by the Win32 API to use as an argument in other functions. This way it internalises some of it's functionality providing a certain amount of "security through obscurity". I just thought of a way I can test this theory, I'll be right back.
@Disch I want to use this function to convert a string into a HGLOBAL, so that I can put it into a STGMEDIUM structure's HGLOBAL variable, which I will use to create a data object used for drag and drop operations
and yes a HANDLE is a PVOID which is a void*
and yes it didn't compile, it gives:
error C2036: 'void *' : unknown size
error C2120: 'void' illegal with all types
It uses void pointers to hide functionality and implementation. They do a good job at it as well. Void pointers can hold data that the user isn't aware of. For instance, you can have a window struct that's several kB large and all you know is that it's represented by a void pointer you pass around.
What pisses me off about Windows is they redefine types like "typedef int INT;". Just stupid... It makes you very angry when you have people nag at you for not using their type defines when it's the same shit except uglier (EDIT: and platform dependant (for the most part)).