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. LPTSTR type

LPTSTR type

Scheduled Pinned Locked Moved C / C++ / MFC
helpgraphicsperformancelearning
9 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I'm programing in C language. I'd like to use the wsprintf function with a LPTSTR variable. here my code: void GetDescription (LPTSTR Description) { char TypeRes[100]; char Resources[100]; lstrcpy (TypeRes, "Bitmap"); lstrcpy (Resource, "file.bmp"); Description=wsprintf (Description, "<%s : %s>", TypeRes, Resources); } It doesn't work! The variable Description is empty or invalid. But I have to use LPTSTR not char[xxx], otherwise, my program is very slow and I get a memory error. Help me!!!!!!:confused: Appstmd

    A F M 3 Replies Last reply
    0
    • L Lost User

      I'm programing in C language. I'd like to use the wsprintf function with a LPTSTR variable. here my code: void GetDescription (LPTSTR Description) { char TypeRes[100]; char Resources[100]; lstrcpy (TypeRes, "Bitmap"); lstrcpy (Resource, "file.bmp"); Description=wsprintf (Description, "<%s : %s>", TypeRes, Resources); } It doesn't work! The variable Description is empty or invalid. But I have to use LPTSTR not char[xxx], otherwise, my program is very slow and I get a memory error. Help me!!!!!!:confused: Appstmd

      A Offline
      A Offline
      Anders Molin
      wrote on last edited by
      #2

      If you use wsprintf() I guess you have defined UNICODE and _UNICODE, in that case you can not ude char, but should instead use wchar_t. Description=wsprintf() fails, wsprintf returns the number of characters the function put in to a string, not the string itself, so you should use something like int result = wsprintf (Description, "<%s : %s>", TypeRes, Resources);. Make sure that Description are large enough to contain the text you pun into it with wsprintf() - Anders Money talks, but all mine ever says is "Goodbye!"

      R 1 Reply Last reply
      0
      • A Anders Molin

        If you use wsprintf() I guess you have defined UNICODE and _UNICODE, in that case you can not ude char, but should instead use wchar_t. Description=wsprintf() fails, wsprintf returns the number of characters the function put in to a string, not the string itself, so you should use something like int result = wsprintf (Description, "<%s : %s>", TypeRes, Resources);. Make sure that Description are large enough to contain the text you pun into it with wsprintf() - Anders Money talks, but all mine ever says is "Goodbye!"

        R Offline
        R Offline
        Rick York
        wrote on last edited by
        #3

        If you use wsprintf() I guess you have defined UNICODE and _UNICODE, in that case you can not ude char, but should instead use wchar_t. This is not correct. The function has ANSI and UNICODE versions. In this case, the w does not refer to a wide character variant. It refers to a windoze-specific function that resides in user32.dll and is not part of the C runtime library.

        A 1 Reply Last reply
        0
        • R Rick York

          If you use wsprintf() I guess you have defined UNICODE and _UNICODE, in that case you can not ude char, but should instead use wchar_t. This is not correct. The function has ANSI and UNICODE versions. In this case, the w does not refer to a wide character variant. It refers to a windoze-specific function that resides in user32.dll and is not part of the C runtime library.

          A Offline
          A Offline
          Anders Molin
          wrote on last edited by
          #4

          Oops :-O - Anders Money talks, but all mine ever says is "Goodbye!"

          R L 2 Replies Last reply
          0
          • L Lost User

            I'm programing in C language. I'd like to use the wsprintf function with a LPTSTR variable. here my code: void GetDescription (LPTSTR Description) { char TypeRes[100]; char Resources[100]; lstrcpy (TypeRes, "Bitmap"); lstrcpy (Resource, "file.bmp"); Description=wsprintf (Description, "<%s : %s>", TypeRes, Resources); } It doesn't work! The variable Description is empty or invalid. But I have to use LPTSTR not char[xxx], otherwise, my program is very slow and I get a memory error. Help me!!!!!!:confused: Appstmd

            F Offline
            F Offline
            Felix Cho
            wrote on last edited by
            #5

            You should:

            • use the TEXT() or _T() macros for all your string literals
            • use TCHAR instead of char
            • include tchar.h
            • and if you have UNICODE and _UNICODE defined (ie you are using unicode version of wsprintf), make sure you check the "Display unicode strings" options under the Debug tab of the VC++'s Options dialog.

            PS. Are you sure you have a valid buffer in Description? PPS. Just saw you assigning Description from the results of wsprintf. Did you read the docs at all? LPTSTR is not a string class :mad:! You are making the Description pointer to point to some invalid address after the function call.

            1 Reply Last reply
            0
            • A Anders Molin

              Oops :-O - Anders Money talks, but all mine ever says is "Goodbye!"

              R Offline
              R Offline
              Rick York
              wrote on last edited by
              #6

              The rest of your answer was mostly right though. The assignment to Description is inappropriate and is causing the problem. FWIW, wsprintf is mostly like sprintf but it does not support floating point values. :)

              1 Reply Last reply
              0
              • L Lost User

                I'm programing in C language. I'd like to use the wsprintf function with a LPTSTR variable. here my code: void GetDescription (LPTSTR Description) { char TypeRes[100]; char Resources[100]; lstrcpy (TypeRes, "Bitmap"); lstrcpy (Resource, "file.bmp"); Description=wsprintf (Description, "<%s : %s>", TypeRes, Resources); } It doesn't work! The variable Description is empty or invalid. But I have to use LPTSTR not char[xxx], otherwise, my program is very slow and I get a memory error. Help me!!!!!!:confused: Appstmd

                M Offline
                M Offline
                Michael Dunn
                wrote on last edited by
                #7

                Assuming the calling code is passing a valid buffer in the Description parameter, take out the "Description =" part and it will work. Then do all the TCHAR-related stuff the other posts have mentioned (although they are wrong about Unicode; wsprintf() is not a Unicode-only function, that would be swprintf()). --Mike-- Fetchez la vache! My really out-of-date homepage Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan and Jamie Salé.

                1 Reply Last reply
                0
                • A Anders Molin

                  Oops :-O - Anders Money talks, but all mine ever says is "Goodbye!"

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Thks a lot! The code now works perfectly. My variable Description was not large enough:-O ... Here the changes: Lenght=lstrlen(TypeRes)+lstrlen(Resource); Description = (char*) malloc(Lenght); Description[Lenght]='\0'; wsprintf (Description, "<%s : %s>", TypeRes, Resource); Appstmd

                  M 1 Reply Last reply
                  0
                  • L Lost User

                    Thks a lot! The code now works perfectly. My variable Description was not large enough:-O ... Here the changes: Lenght=lstrlen(TypeRes)+lstrlen(Resource); Description = (char*) malloc(Lenght); Description[Lenght]='\0'; wsprintf (Description, "<%s : %s>", TypeRes, Resource); Appstmd

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #9

                    The code is still wrong, you don't allocate enough space for the Description string. Look at the wsprintf() format - you have the two substrings, two angle brackets, two spaces, and a colon. Plus the terminating null. So you should malloc (lstrlen(TypeRes) + lstrlen(Resource) + 6)*sizeof(TCHAR) --Mike-- Fetchez la vache! My really out-of-date homepage Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan and Jamie Salé.

                    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