BSTR to char*

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
try without the +2.

In other words
(char *)strIn + 2 //get rid of these +2
Last edited on
guestgulkan:

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

Fabio
Topic archived. No new replies allowed.