convert method to unicode

Hi,

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)
{
    void  *ptr;
	
    // 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';
	
    return ptr;
}


Best regards,
Yours3!f
I believe enclosing a char* in _T( ) should do it.
1
2
char* foo = "bar";
MessageBox(0, _T(foo), 0, 0);


(It's in <tchar.h>)
Last edited on
_T only works on string literals, not on variables.

Furthermore the OP's code wouldn't compile anyway because it's trying to dereference a void pointer which is nonsense.

Lastly, what is the point of this function? A "HANDLE" is extremely generic. You might as well just use a void pointer. Why would this be useful?


Anyway... to use TCHARs, just change the function to use TCHARs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
HANDLE StringToHandle(const TCHAR *szText, int nTextLen)  // <- const TCHAR
{
    TCHAR *ptr; // <- TCHAR (can't use a void pointer)
	
    if(nTextLen == -1)
        nTextLen = _tcslen(szText); // <- _tcslen
    
    ptr = (TCHAR*)GlobalAlloc(GMEM_FIXED, (nTextLen + 1) * sizeof(TCHAR)); // <- sizeof(TCHAR)

    memcpy(ptr, szText, nTextLen * sizeof(TCHAR)); //<- sizeof(TCHAR)
    ptr[nTextLen] = '\0';
	
    return ptr;
}
Last edited on
@ Disch: It's funny that you should say that because a HANDLE IS a void pointer: http://msdn.microsoft.com/en-us/library/aa383751(VS.85).aspx#HANDLE
It's one of many methods that Win32 uses to half-ass OOP into C.

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

on this line:
ptr[text_length] = '\0';
closed account (S6k9GNh0)
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)).
Last edited on
Topic archived. No new replies allowed.