Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. TCHAR* and LPTSTR

TCHAR* and LPTSTR

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
22 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K kelprinc

    Does anybody know how to assign a TCHAR* fromo a LPTSTR. This is my code: p->shi502_netname ="test"; LPTSTR h =p->shi502_netname ; TCHAR* var = h; cout< output: t when I try to print out h i get the first letter of string pointed to by h; I thing im no accessing the string from the pointer correctly Kelvin Cikomo

    O Offline
    O Offline
    Owner drawn
    wrote on last edited by
    #2

    kelprinc wrote:

    p->shi502_netname ="test";

    _tcscpy(p->shi502_netname, _T("test")); I think you should copy _T("test") to "p->shi502_netname". Now it should work.

    Jesus Loves:rose:

    --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

    K 1 Reply Last reply
    0
    • O Owner drawn

      kelprinc wrote:

      p->shi502_netname ="test";

      _tcscpy(p->shi502_netname, _T("test")); I think you should copy _T("test") to "p->shi502_netname". Now it should work.

      Jesus Loves:rose:

      --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

      K Offline
      K Offline
      kelprinc
      wrote on last edited by
      #3

      It did not work I think i shld rephrase my question. I want to access the string being pointed to by shi502_netname i.e if i just type cout<shi502_netname<

      O 1 Reply Last reply
      0
      • K kelprinc

        It did not work I think i shld rephrase my question. I want to access the string being pointed to by shi502_netname i.e if i just type cout<shi502_netname<

        O Offline
        O Offline
        Owner drawn
        wrote on last edited by
        #4

        kelprinc wrote:

        TCHAR * shi502_netname = _T("test"); LPTSTR h = shi502_netname ; TCHAR* var = h; cout< Here this is printing fine. What is this p->shi502_netname. Is this a pointer to a char or a single char.

        Jesus Loves:rose:

        --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

        K 1 Reply Last reply
        0
        • O Owner drawn

          kelprinc wrote:

          TCHAR * shi502_netname = _T("test"); LPTSTR h = shi502_netname ; TCHAR* var = h; cout< Here this is printing fine. What is this p->shi502_netname. Is this a pointer to a char or a single char.

          Jesus Loves:rose:

          --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

          K Offline
          K Offline
          kelprinc
          wrote on last edited by
          #5

          We are on totally different wave lengths. Forget about the first part. Assume you have a struct p that has shi502_netname as its member. shi502_netname is of type LPTSTR. now i want to print the LPTSTR string in the p struct. I tried doing this p->shi502_netname, but all i get is the first letter of the string Kelvin Chikomo

          O 1 Reply Last reply
          0
          • K kelprinc

            We are on totally different wave lengths. Forget about the first part. Assume you have a struct p that has shi502_netname as its member. shi502_netname is of type LPTSTR. now i want to print the LPTSTR string in the p struct. I tried doing this p->shi502_netname, but all i get is the first letter of the string Kelvin Chikomo

            O Offline
            O Offline
            Owner drawn
            wrote on last edited by
            #6

            I also declared a struct and put a member inside with the same name and it's still working.

            struct
            {
            LPTSTR shi502_netname;
            }p;

            p.shi502_netname = _T("test");
            LPTSTR h = p.shi502_netname ;
            TCHAR* var = h;
            cout<

            Here is the code. It's working.:^)

            Jesus Loves:rose:

            --Owner Drawn:rose:
            --Nothing special
            --Defeat is temporary but surrender is permanent
            --Never say quits
            --Jesus is Lord:rose:

            K 1 Reply Last reply
            0
            • O Owner drawn

              I also declared a struct and put a member inside with the same name and it's still working.

              struct
              {
              LPTSTR shi502_netname;
              }p;

              p.shi502_netname = _T("test");
              LPTSTR h = p.shi502_netname ;
              TCHAR* var = h;
              cout<

              Here is the code. It's working.:^)

              Jesus Loves:rose:

              --Owner Drawn:rose:
              --Nothing special
              --Defeat is temporary but surrender is permanent
              --Never say quits
              --Jesus is Lord:rose:

              K Offline
              K Offline
              kelprinc
              wrote on last edited by
              #7

              I have edited the struct, its not a normal struct (typedef) typedef struct { LPTSTR shi502_netname; }p; p.shi502_netname = _T("test"); LPTSTR h = p.shi502_netname ; TCHAR* var = h;cout<

              O 1 Reply Last reply
              0
              • K kelprinc

                I have edited the struct, its not a normal struct (typedef) typedef struct { LPTSTR shi502_netname; }p; p.shi502_netname = _T("test"); LPTSTR h = p.shi502_netname ; TCHAR* var = h;cout<

                O Offline
                O Offline
                Owner drawn
                wrote on last edited by
                #8

                typedef struct
                {
                LPTSTR shi502_netname;
                }p;

                Did you declare a variable of type p.

                p p1; //don't forget this
                p1.shi502_netname = _T("test");
                LPTSTR h = p1.shi502_netname ;
                TCHAR* var = h;
                cout<
                It still works fine.

                Jesus Loves:rose:

                --Owner Drawn:rose:
                --Nothing special
                --Defeat is temporary but surrender is permanent
                --Never say quits
                --Jesus is Lord:rose:

                K 1 Reply Last reply
                0
                • O Owner drawn

                  typedef struct
                  {
                  LPTSTR shi502_netname;
                  }p;

                  Did you declare a variable of type p.

                  p p1; //don't forget this
                  p1.shi502_netname = _T("test");
                  LPTSTR h = p1.shi502_netname ;
                  TCHAR* var = h;
                  cout<
                  It still works fine.

                  Jesus Loves:rose:

                  --Owner Drawn:rose:
                  --Nothing special
                  --Defeat is temporary but surrender is permanent
                  --Never say quits
                  --Jesus is Lord:rose:

                  K Offline
                  K Offline
                  kelprinc
                  wrote on last edited by
                  #9

                  Yes i do. you did not declare yours as *p look at my definition of my struct below typedef struct { LPTSTR shi502_netname; }*p; p p1; //don't forget thisp1.shi502_netname = _T("test");LPTSTR h = p1.shi502_netname ;TCHAR* var = h;cout<

                  O 1 Reply Last reply
                  0
                  • K kelprinc

                    Yes i do. you did not declare yours as *p look at my definition of my struct below typedef struct { LPTSTR shi502_netname; }*p; p p1; //don't forget thisp1.shi502_netname = _T("test");LPTSTR h = p1.shi502_netname ;TCHAR* var = h;cout<

                    O Offline
                    O Offline
                    Owner drawn
                    wrote on last edited by
                    #10

                    Aha here is the problem...

                    typedef struct
                    {
                    LPTSTR shi502_netname;
                    } ap,*p;

                    ap a;
                    p p1 = &a;
                    p1->shi502_netname = _T("test");
                    LPTSTR h = p1->shi502_netname ;
                    TCHAR* var = h;
                    cout<

                    It works with the modifications that I made. You have to make another typedef along with *p i.e ap. If you don't what are you going to assign to a variable of type p. p needs an address of a struct of it's own type.

                    An advice: Please provide some meaningful names for variables and specially typedefs.

                    Jesus Loves:rose:

                    --Owner Drawn:rose:
                    --Nothing special
                    --Defeat is temporary but surrender is permanent
                    --Never say quits
                    --Jesus is Lord:rose:

                    K 1 Reply Last reply
                    0
                    • O Owner drawn

                      Aha here is the problem...

                      typedef struct
                      {
                      LPTSTR shi502_netname;
                      } ap,*p;

                      ap a;
                      p p1 = &a;
                      p1->shi502_netname = _T("test");
                      LPTSTR h = p1->shi502_netname ;
                      TCHAR* var = h;
                      cout<

                      It works with the modifications that I made. You have to make another typedef along with *p i.e ap. If you don't what are you going to assign to a variable of type p. p needs an address of a struct of it's own type.

                      An advice: Please provide some meaningful names for variables and specially typedefs.

                      Jesus Loves:rose:

                      --Owner Drawn:rose:
                      --Nothing special
                      --Defeat is temporary but surrender is permanent
                      --Never say quits
                      --Jesus is Lord:rose:

                      K Offline
                      K Offline
                      kelprinc
                      wrote on last edited by
                      #11

                      I tried it it did not work the struct that iam using is part of the C++ libraries here it is typedef struct _SHARE_INFO_502 { LPTSTR shi502_netname; DWORD shi502_type; LPTSTR shi502_remark; DWORD shi502_permissions; DWORD shi502_max_uses; DWORD shi502_current_uses; LPTSTR shi502_path; LPTSTR shi502_passwd; DWORD shi502_reserved; PSECURITY_DESCRIPTOR shi502_security_descriptor; } SHARE_INFO_502, *PSHARE_INFO_502, *LPSHARE_INFO_502; And i have to use *PSHARE_INFO_502 as the name of the struct Kelvin Chikomo

                      O 1 Reply Last reply
                      0
                      • K kelprinc

                        I tried it it did not work the struct that iam using is part of the C++ libraries here it is typedef struct _SHARE_INFO_502 { LPTSTR shi502_netname; DWORD shi502_type; LPTSTR shi502_remark; DWORD shi502_permissions; DWORD shi502_max_uses; DWORD shi502_current_uses; LPTSTR shi502_path; LPTSTR shi502_passwd; DWORD shi502_reserved; PSECURITY_DESCRIPTOR shi502_security_descriptor; } SHARE_INFO_502, *PSHARE_INFO_502, *LPSHARE_INFO_502; And i have to use *PSHARE_INFO_502 as the name of the struct Kelvin Chikomo

                        O Offline
                        O Offline
                        Owner drawn
                        wrote on last edited by
                        #12

                        Go ahead it will work. PSHARE_INFO_502 should take the address of a valid structure of the same type. Then it has to work. Probably like this: SHARE_INFO_502 shInfo; PSHARE_INFO_502 pShInfo = &shInfo; Now use pShInfo as you should.

                        Jesus Loves:rose:

                        --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                        K 1 Reply Last reply
                        0
                        • K kelprinc

                          Does anybody know how to assign a TCHAR* fromo a LPTSTR. This is my code: p->shi502_netname ="test"; LPTSTR h =p->shi502_netname ; TCHAR* var = h; cout< output: t when I try to print out h i get the first letter of string pointed to by h; I thing im no accessing the string from the pointer correctly Kelvin Cikomo

                          K Offline
                          K Offline
                          kakan
                          wrote on last edited by
                          #13

                          This maybe is a stupid question but I have to ask: Are your program using UNICODE? In that case, it's perfectly understandable: The console doesn't want UNICODE, so when your 't' is written to the consol, the console sees a 't', followed by a \0. And since \0 is a string terminator, the output is terminated. Could this be the case here?

                          K 1 Reply Last reply
                          0
                          • K kakan

                            This maybe is a stupid question but I have to ask: Are your program using UNICODE? In that case, it's perfectly understandable: The console doesn't want UNICODE, so when your 't' is written to the consol, the console sees a 't', followed by a \0. And since \0 is a string terminator, the output is terminated. Could this be the case here?

                            K Offline
                            K Offline
                            kelprinc
                            wrote on last edited by
                            #14

                            Yes i am programing in unicode. So what shld i do Kelvin Chikomo

                            K 1 Reply Last reply
                            0
                            • O Owner drawn

                              Go ahead it will work. PSHARE_INFO_502 should take the address of a valid structure of the same type. Then it has to work. Probably like this: SHARE_INFO_502 shInfo; PSHARE_INFO_502 pShInfo = &shInfo; Now use pShInfo as you should.

                              Jesus Loves:rose:

                              --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                              K Offline
                              K Offline
                              kelprinc
                              wrote on last edited by
                              #15

                              I get wrong out puts if i try this. let me just give you part of my code PSHARE_INFO_502 BufPtr,p; //SHARE_INFO_502 shInfo; //PSHARE_INFO_502 pShInfo = &shInfo; NET_API_STATUS res; LPTSTR lpszServer = NULL; DWORD er=0,tr=0,resume=0, i; //declare server variable lpszServer = lpszArgv[1]; cout<shi502_netname; cout<

                              P 1 Reply Last reply
                              0
                              • K kelprinc

                                Yes i am programing in unicode. So what shld i do Kelvin Chikomo

                                K Offline
                                K Offline
                                kakan
                                wrote on last edited by
                                #16

                                Make sure your console output is in plain ANSI

                                K 2 Replies Last reply
                                0
                                • K kakan

                                  Make sure your console output is in plain ANSI

                                  K Offline
                                  K Offline
                                  kelprinc
                                  wrote on last edited by
                                  #17

                                  I am not printing any thing to the console as yet. You can look at the above threads to see my code. Kelvin Chikomo

                                  D 1 Reply Last reply
                                  0
                                  • K kelprinc

                                    I am not printing any thing to the console as yet. You can look at the above threads to see my code. Kelvin Chikomo

                                    D Offline
                                    D Offline
                                    David Crow
                                    wrote on last edited by
                                    #18

                                    kelprinc wrote:

                                    I am not printing any thing to the console as yet.

                                    Sure you are. What do you think cout is for?


                                    "The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb

                                    1 Reply Last reply
                                    0
                                    • K kakan

                                      Make sure your console output is in plain ANSI

                                      K Offline
                                      K Offline
                                      kelprinc
                                      wrote on last edited by
                                      #19

                                      I am now trying to use the W2A conversion but im realy doubting it Kelvin Chikomo

                                      1 Reply Last reply
                                      0
                                      • K kelprinc

                                        Does anybody know how to assign a TCHAR* fromo a LPTSTR. This is my code: p->shi502_netname ="test"; LPTSTR h =p->shi502_netname ; TCHAR* var = h; cout< output: t when I try to print out h i get the first letter of string pointed to by h; I thing im no accessing the string from the pointer correctly Kelvin Cikomo

                                        D Offline
                                        D Offline
                                        David Crow
                                        wrote on last edited by
                                        #20

                                        You need to use wcout instead of cout.


                                        "The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb

                                        1 Reply Last reply
                                        0
                                        • K kelprinc

                                          Does anybody know how to assign a TCHAR* fromo a LPTSTR. This is my code: p->shi502_netname ="test"; LPTSTR h =p->shi502_netname ; TCHAR* var = h; cout< output: t when I try to print out h i get the first letter of string pointed to by h; I thing im no accessing the string from the pointer correctly Kelvin Cikomo

                                          P Offline
                                          P Offline
                                          Prakash Nadar
                                          wrote on last edited by
                                          #21

                                          if you are using unicode build then you should use unicode set of apis to work on them. If you really need to work on multibyte string then you may have to use WideCharToMultiByte api to do the convertion.


                                          -Prakash

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups