LPTSTR initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alex

    LPTSTR initialization

    how do i initialize an LPTSTR variable?

    i'm trying to call a microsoft function

    LONG QueryStringValu e(
    LPCTSTR pszValueName,
    LPTSTR pszValue,
    ULONG* pnChars
    ) throw( );pszValue is supposed to get the value i want but i need to
    initialize it before i pass it to QueryStringValu e, how do i initialize
    it?the function (member of CRegKey) is described
    here:http://msdn.microsoft.com/library/de...y/en-us/vclib/
    html/_atl_cregkey.as p






    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
  • John Harrison

    #2
    Re: LPTSTR initialization


    "alex" <alex.smotritsk y@verizon.net> wrote in message
    news:40d99a5d$1 _2@127.0.0.1...[color=blue]
    > how do i initialize an LPTSTR variable?
    >
    > i'm trying to call a microsoft function
    >
    > LONG QueryStringValu e(
    > LPCTSTR pszValueName,
    > LPTSTR pszValue,
    > ULONG* pnChars
    > ) throw( );pszValue is supposed to get the value i want but i need to
    > initialize it before i pass it to QueryStringValu e, how do i initialize
    > it?the function (member of CRegKey) is described
    >[/color]
    here:http://msdn.microsoft.com/library/de...y/en-us/vclib/[color=blue]
    > html/_atl_cregkey.as p
    >[/color]

    You need to allocate some memory, you should specify how much memory you
    allocate in nChars.

    E.g. suppose you decide to allocate 1000 characters

    LPTSTR pszValue = new TCHAR[1000];
    nChars = 1000;
    xxxx.QueryStrin gFunction("yyyy y", pszValue, &nChars);
    // do something with pszValue
    delete[] pszValue; // free the allocated memory

    john


    Comment

    • Unforgiven

      #3
      Re: LPTSTR initialization

      "alex" <alex.smotritsk y@verizon.net> wrote in message
      news:40d99a5d$1 _2@127.0.0.1...[color=blue]
      > how do i initialize an LPTSTR variable?
      >
      > i'm trying to call a microsoft function
      >
      > LONG QueryStringValu e(
      > LPCTSTR pszValueName,
      > LPTSTR pszValue,
      > ULONG* pnChars
      > ) throw( );pszValue is supposed to get the value i want but i need to
      > initialize it before i pass it to QueryStringValu e, how do i initialize
      > it?the function (member of CRegKey) is described
      > here:http://msdn.microsoft.com/library/de...y/en-us/vclib/
      > html/_atl_cregkey.as p[/color]

      This newsgroup is about standard C++ only, so Microsoft's APIs are
      off-topic. However I think the question itself is enough C++ related to be
      answered here.

      What you need to realize is what an LPTSTR is. It's a typedef. An LPTSTR
      actually is a TCHAR*, which depending on whether UNICODE is defined maps to
      either char* or wchar_t*.

      You need to initialize your LPTSTR to sufficient size for the string you
      want to return. You do this in two ways, on the stack or on the heap (with
      new):
      On the stack:
      TCHAR szValue[50];
      ULONG nChars = 50;
      key.QueryString Value("SomeValu e", szValue, &nChars);

      On the heap:
      LPTSTR szValue = new TCHAR[50];
      ULONG nChars = 50;
      key.QueryString Value("SomeValu e", szValue, &nChars);
      // do something with szValue
      delete[] szValue; // DO NOT FORGET THIS!

      --
      Unforgiven

      Comment

      • JKop

        #4
        Re: LPTSTR initialization

        alex posted:
        [color=blue]
        > how do i initialize an LPTSTR variable?
        >
        > i'm trying to call a microsoft function
        >
        > LONG QueryStringValu e(
        > LPCTSTR pszValueName,
        > LPTSTR pszValue,
        > ULONG* pnChars
        > ) throw( );pszValue is supposed to get the value i want but i need to
        > initialize it before i pass it to QueryStringValu e, how do i initialize
        > it?the function (member of CRegKey) is described
        > here:http://msdn.microsoft.com/library/de...brary/en-us/vc
        > lib/ html/_atl_cregkey.as p[/color]


        typedef const char* LPCTSTR;

        typedef char* LPTSTR;

        typedef unsigned long ULONG;


        char value_name[] = "BlahBlah";

        char value[] = "BlahBlah";

        unsigned long poo = 0;

        QueryStringValu e(value_name,va lue,&poo);

        Or even:

        QueryStringValu e("Hello","Good bye",&poo);


        This function will alter the second and third arguments, as they're not
        declared const.


        Why does Microsoft give the intrinsic types new names?

        Because the information that you've got and the header files you've got are
        intended to be programming language NON-specific. If they used "char*", then
        some-one writing in a language called Blerg wouldn't know what's going on.
        So they've thought up of new names to be used with every computer
        programming language.


        -JKop

        Comment

        • Unforgiven

          #5
          Re: LPTSTR initialization

          "JKop" <NULL@NULL.NULL > wrote in message
          news:19hCc.2976 $Z14.3533@news. indigo.ie...[color=blue]
          > alex posted:
          >[color=green]
          >> how do i initialize an LPTSTR variable?
          >>
          >> i'm trying to call a microsoft function
          >>
          >> LONG QueryStringValu e(
          >> LPCTSTR pszValueName,
          >> LPTSTR pszValue,
          >> ULONG* pnChars
          >> ) throw( );pszValue is supposed to get the value i want but i need to
          >> initialize it before i pass it to QueryStringValu e, how do i initialize
          >> it?the function (member of CRegKey) is described
          >> here:http://msdn.microsoft.com/library/de...brary/en-us/vc
          >> lib/ html/_atl_cregkey.as p[/color]
          >
          >
          > typedef const char* LPCTSTR;[/color]

          Wrong, LPCTSTR will expand to const wchar_t* if UNICODE is defined. Actually
          the definition is:
          typedef const TCHAR* LPCTSTR;

          And TCHAR is:
          #ifdef UNICODE
          typedef wchar_t TCHAR
          #else
          typedef char TCHAR
          #endif
          [color=blue]
          > typedef char* LPTSTR;
          >
          > typedef unsigned long ULONG;
          >
          >
          > char value_name[] = "BlahBlah";
          >
          > char value[] = "BlahBlah";
          >
          > unsigned long poo = 0;
          >
          > QueryStringValu e(value_name,va lue,&poo);[/color]

          Wrong, the third parameter of QueryStringValu e must be the size in TCHARs of
          the buffer pointed to by the second parameter. It will receive the amount of
          TCHARs written. So it shouldn't be 0 initially.
          [color=blue]
          > Or even:
          >
          > QueryStringValu e("Hello","Good bye",&poo);
          >
          >
          > This function will alter the second and third arguments, as they're not
          > declared const.
          >
          >
          > Why does Microsoft give the intrinsic types new names?
          >
          > Because the information that you've got and the header files you've got
          > are
          > intended to be programming language NON-specific. If they used "char*",
          > then
          > some-one writing in a language called Blerg wouldn't know what's going on.
          > So they've thought up of new names to be used with every computer
          > programming language.[/color]

          Not really. The header files are meant for C(++), they use C syntax so it'd
          be a little hard to use them from a language that doesn't support C syntax.
          Also, this particular function is not part of the Win32 API, but of ATL,
          which is meant for C++ exclusively.

          The main reason Microsoft renames intrinsic types is to standardise their
          usage across the API, and to make them platform independant. With platform I
          don't mean Windows vs. Unix, but Win16 vs. Win32 vs. Win64 and in the past
          also Windows NT on the Alpha processors. For instance the LPARAM type, which
          is used for Windows messaging, should be large enough to hold a pointer. So
          on Win32 it'll expand to unsigned int, but on Win64 it's unsigned __int64.
          Also, types such as WORD, DWORD are of a fixed length, regardless of what
          word length your system has (which is a bit confusing, since it means DWORD
          on Win32 is only a single machine word long (32 bits)). Then there are the
          main pointer arithmic types such as INT_PTR which are guaranteed to have the
          same length as a pointer.

          In this situation we see another use of the typedefs. All string related
          types with a T in them (TCHAR, LPTSTR, LPCTSTR) expand to either ansi or
          wide characters depending on whether UNICODE is defined. In conjunction with
          the _T macro you can use those types to write a program that can compile for
          ansi and unicode without modification.

          --
          Unforgiven

          Comment

          • Pete C.

            #6
            Re: LPTSTR initialization

            JKop wrote:
            <snip>[color=blue]
            >
            > typedef const char* LPCTSTR;
            >
            > typedef char* LPTSTR;
            >
            > typedef unsigned long ULONG;
            >[/color]


            On some systems. Don't rely on it.
            [color=blue]
            >
            >
            > This function will alter the second and third arguments, as they're
            > not declared const.[/color]

            Not neccessarily. Non-const does not require that the variable be altered;
            const just prevents it.
            [color=blue]
            >
            >
            > Why does Microsoft give the intrinsic types new names?
            >
            > Because the information that you've got and the header files you've
            > got are intended to be programming language NON-specific. If they
            > used "char*", then some-one writing in a language called Blerg
            > wouldn't know what's going on. So they've thought up of new names to
            > be used with every computer programming language.
            >[/color]

            Plus it's more portable. If, for example, you use TCHAR correctly you can
            compile for both WinCE and `95 with the same code.

            - Pete


            Comment

            • JKop

              #7
              Re: LPTSTR initialization

              Pete C. posted:
              [color=blue][color=green]
              >> This function will alter the second and third arguments, as they're
              >> not declared const.[/color]
              >
              > Not neccessarily. Non-const does not require that the variable be
              > altered; const just prevents it.[/color]


              You are correct, although if the function does not alter the variables, it's
              stupid for not declaring them const.


              -JKop


              Comment

              • John Harrison

                #8
                Re: LPTSTR initialization


                "JKop" <NULL@NULL.NULL > wrote in message
                news:owjCc.2990 $Z14.3458@news. indigo.ie...[color=blue]
                > Pete C. posted:
                >[color=green][color=darkred]
                > >> This function will alter the second and third arguments, as they're
                > >> not declared const.[/color]
                > >
                > > Not neccessarily. Non-const does not require that the variable be
                > > altered; const just prevents it.[/color]
                >
                >
                > You are correct, although if the function does not alter the variables,[/color]
                it's[color=blue]
                > stupid for not declaring them const.
                >[/color]

                But the function does alter the variable, RTFM.

                john


                Comment

                • puppet_sock@hotmail.com

                  #9
                  Re: LPTSTR initialization

                  "alex" <alex.smotritsk y@verizon.net> wrote in message news:<40d99a5d$ 1_2@127.0.0.1>. ..[color=blue]
                  > how do i initialize an LPTSTR variable?
                  >
                  > i'm trying to call a microsoft function
                  >
                  > LONG QueryStringValu e(
                  > LPCTSTR pszValueName,
                  > LPTSTR pszValue,
                  > ULONG* pnChars
                  > ) throw( );pszValue is supposed to get the value i want but i need to
                  > initialize it before i pass it to QueryStringValu e, how do i initialize
                  > it?the function (member of CRegKey) is described
                  > here:http://msdn.microsoft.com/library/de...y/en-us/vclib/
                  > html/_atl_cregkey.as p[/color]

                  Ok, you are kind of out of scope for topic here. This is a library
                  fcn in a vendor specific librry. And LPTSTR is not a standard type.
                  So, you should really be asking in one of the Microsoft or Windows
                  related groups.

                  But just guessing... It *looks* like a pointer. The psz in front of
                  it seems to say it's a pointer to a string terminated by a null.
                  So, presumably you need to point it at such before the call.
                  Presumably your docs will tell you what.
                  Socks

                  Comment

                  • alex

                    #10
                    Re: LPTSTR initialization


                    "Unforgiven " <jaapd3000@hotm ail.com> wrote in message
                    news:2jtktiF15h 5lgU1@uni-berlin.de...[color=blue]
                    > "alex" <alex.smotritsk y@verizon.net> wrote in message
                    > news:40d99a5d$1 _2@127.0.0.1...[color=green]
                    > > how do i initialize an LPTSTR variable?
                    > >
                    > > i'm trying to call a microsoft function
                    > >
                    > > LONG QueryStringValu e(
                    > > LPCTSTR pszValueName,
                    > > LPTSTR pszValue,
                    > > ULONG* pnChars
                    > > ) throw( );pszValue is supposed to get the value i want but i need to
                    > > initialize it before i pass it to QueryStringValu e, how do i initialize
                    > > it?the function (member of CRegKey) is described
                    > >[/color][/color]
                    here:http://msdn.microsoft.com/library/de...y/en-us/vclib/[color=blue][color=green]
                    > > html/_atl_cregkey.as p[/color]
                    >
                    > This newsgroup is about standard C++ only, so Microsoft's APIs are
                    > off-topic. However I think the question itself is enough C++ related to be
                    > answered here.
                    >
                    > What you need to realize is what an LPTSTR is. It's a typedef. An LPTSTR
                    > actually is a TCHAR*, which depending on whether UNICODE is defined maps[/color]
                    to[color=blue]
                    > either char* or wchar_t*.
                    >
                    > You need to initialize your LPTSTR to sufficient size for the string you
                    > want to return. You do this in two ways, on the stack or on the heap (with
                    > new):
                    > On the stack:
                    > TCHAR szValue[50];
                    > ULONG nChars = 50;
                    > key.QueryString Value("SomeValu e", szValue, &nChars);
                    >
                    > On the heap:
                    > LPTSTR szValue = new TCHAR[50];
                    > ULONG nChars = 50;
                    > key.QueryString Value("SomeValu e", szValue, &nChars);
                    > // do something with szValue
                    > delete[] szValue; // DO NOT FORGET THIS!
                    >
                    > --
                    > Unforgiven
                    >[/color]

                    this provides the proper initialization i needed, thanks, thanks to everyone
                    else who participated as well!



                    Posted Via Usenet.com Premium Usenet Newsgroup Services
                    ----------------------------------------------------------
                    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
                    ----------------------------------------------------------
                    Best Usenet Service Providers 2025 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.





                    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
                    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
                    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

                    Comment

                    Working...