BSTR to char*

May 1, 2010 at 11:28pm
Hi, I'm studing some shell examples and one example have a mistake. In some places create a char array from a BSTR variable.
This is the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void * WINAPI BStr2TStr(HWND hwnd, BSTR strIn)
{
        DWORD   size;
        void    *strOut;

        if (!IsWindowUnicode(hwnd))
        {
				// ANSI
                size = WideCharToMultiByte(CP_ACP, 0, (WCHAR *)((char *)strIn + 2), -1, 0, 0, 0, 0);
                if ((strOut = GlobalAlloc(GMEM_FIXED, size)))
                        WideCharToMultiByte(CP_ACP, 0, (WCHAR *)((char *)strIn + 2), -1, (char *)strOut, size, 0, 0);
        }
        else
        {
				// UNICODE
                size = (*((short *)strIn) + 1) * sizeof(wchar_t);
                if ((strOut = GlobalAlloc(GMEM_FIXED, size)))
                        CopyMemory(strOut, (char *)strIn + 2, size);
        }

        return(strOut);
}



The problem is that the returned array don't have the first character (I'm using the ANSI section).
For instance, instead of returning "Hello World" just return "ello World". Can you fix this, please? Thank you in advance.

Fabio
Last edited on May 1, 2010 at 11:32pm
May 2, 2010 at 12:20am
try without the +2.

In other words
(char *)strIn + 2 //get rid of these +2
Last edited on May 2, 2010 at 12:28am
May 2, 2010 at 3:32am
guestgulkan:

It works perfectly! Thank you so much for your help.

Fabio
Topic archived. No new replies allowed.