SV: Working with LPCWSTR

Kim Gräsman <[email protected]> Tue, 15 Oct 2002 22:34:34 +0200
Newsgroups gmane.comp.windows.devel.oledb.devel
Message-ID <[email protected]>
Hi Alan,
=20
> hr returns S_OK, but  "wszCommand" now contains "" - something blew it =
away.
> still debugging now...
> anyone have any idea why?
=20
Yeah, you're returning a reference to a temporary object, kind of;
=20
First, understand that LPCWSTR is really just a define for const WCHAR*.
The code below shouldn't compile, as far as I can see... You can't =
re-point the const pointer.
=20
However, as soon as ParseString returns, the CString's destructor runs, =
and deallocates the memory pointed to by wszCommand.
=20
inline void ParseString(LPCWSTR wszCommand)=20
{=20
        //what i want to do!=20
        CString s =3D wszCommand;=20
        s.Replace("SUBSTRING","SUBSTR");=20
        wszCommand =3D s;  // s =3D "yada yada SUBSTR"
=20
        // ~CString - wszCommand points to garbage
}=20
=20
So, as Vagif said, either return a CString object, or reimplement =
ParseString some other way, to make sure the memory is not deallocated.
=20
Hope that helps,
Kim
=20

        -----Ursprungligt meddelande-----=20
        Fr=E5n: Alan Gamboa [mailto:[email protected]]=20
        Skickat: ti 2002-10-15 22:00=20
        Till: [email protected]=20
        Kopia:=20
        =C4mne: Re: Working with LPCWSTR
=09
=09
        thanks,
        =20
        this works to a point.
        =20
        tracing through atldbcli.h, "wszCommand" contains the modified string; =
however, after the following call (where it is creating the command and =
setting the command text):
           =20
            hr =3D CreateCommand(session);
        =20
        hr returns S_OK, but  "wszCommand" now contains "" - something blew it =
away. still debugging now...
        =20
        anyone have any idea why?
        =20
        thanks.
        =20
        Alan Gamboa
        CCG Systems, Inc.

                -----Original Message-----
                From: Vagif Abilov [mailto:[email protected]]
                Sent: Tuesday, October 15, 2002 1:41 PM
                To: [email protected]
                Subject: Re: [OLEDB_DEV] Working with LPCWSTR
        =09
        =09
                First of all, if ParseString takes an LPCWSTR parameter, you can't =
change it - LPCWSTR is a _const_ pointer to a wide character string.
                Second, if your intention is to modify the string, on return you will =
get another string, you can use LPWSTR either, it must be LPWSTR* =
(pointer to pointer).
                Third, why not let ParseString return a new string? Something like =
this:
                =20
                inline CString ParseString(LPCWSTR pwszInput)
                {
                    CString strOutput;
                    // do something
                    return strOutput;
                }
                =20
                Then if you need to convert return value to wide string, just call =
T2W(ParseString(L"some string")).
                =20
                Best regards
 =09
<http://www.contopronto.com/> =09
..................................=09
Vagif Abilov =09
Development team =09
[email protected] <mailto:[email protected]> =09
 =09
Tel:     +47     23 33 25 79=09
Mob:     +47     928 21 221=09
Fax:     +47     23 33 25 71=09
..................................=09
Contopronto AS=09
Valkyriegaten 15=09
0366 Oslo, Norway=09

                =20

                =20

                        -----Original Message-----
                        From: OLEDB_DEV [mailto:[email protected]]On Behalf Of =
Alan Gamboa
                        Sent: 15. oktober 2002 19:14
                        To: [email protected]
                        Subject: [OLEDB_DEV] Working with LPCWSTR
                =09
                =09

                        I am so used to manipulating CString and char[] variables, I am =
having problems working with LPCWSTR variables :)=20

                        in my accessor i have=20

                        MY_DEFINE_COMMAND_EX(CMyAccessor,L"SELECT blah, SUBSTRING(blah,1,1) =
FROM tblBlah")=20

                        i have redefined DEFINE_COMMAND_EX as follows:=20

                        #define MY_DEFINE_COMMAND_EX(x, wszCommand) \=20
                                typedef x _CommandClass; \=20
                                static HRESULT GetDefaultCommand(LPCWSTR* ppwszCommand) throw =
() \=20
                                { \=20
                                        ParseString(wszCommand); \=20
                                        *ppwszCommand =3D wszCommand; \=20
                                        return S_OK; \=20
                                }=20

                        here is ParseString():=20

                        inline void ParseString(LPCWSTR wszCommand)=20
                        {=20
                                //what i want to do!=20
                                CString s =3D wszCommand;=20
                                s.Replace("SUBSTRING","SUBSTR");=20
                                wszCommand =3D s;=20
                        }=20

                        I know the syntax above isn't exactly correct. Besides, I stepped =
through it and wszCommand does not come back out.=20

                        any direction is appreciated.=20

                        Alan Gamboa=20
                        CCG Systems, Inc.=20
                               =20

                        You can read messages from the OLEDB_DEV archive, unsubscribe from =
OLEDB_DEV, or subscribe to other DevelopMentor lists at =
http://discuss.develop.com.=20

                You can read messages from the OLEDB_DEV archive, unsubscribe from =
OLEDB_DEV, or subscribe to other DevelopMentor lists at =
http://discuss.develop.com.=20

You can read messages from the OLEDB_DEV archive, unsubscribe from =
OLEDB_DEV, or subscribe to other DevelopMentor lists at =
http://discuss.develop.com.

You can read messages from the OLEDB_DEV archive, unsubscribe from OLEDB_DEV,
or subscribe to other DevelopMentor lists at http://discuss.develop.com.