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. can a function return WCHAR

can a function return WCHAR

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studiocomquestion
9 Posts 3 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.
  • B Offline
    B Offline
    bkelly13
    wrote on last edited by
    #1

    Visual Studio C++ I need a function that would be declared like this:

    class Messages
    { ...
    WCHAR get_string();
    ... }

    and be called something like this

    WCHAR new_string[ MAX ];
    new_string = p_messages->get_string()
    // where p_messages is a pointer to that class

    I have tried that syntax but all readers probably know the errors so they are omitted. I am thinking it must be something more like

    class Messages
    { ...
    bool get_string( WCHAR * target, int target_size );
    ... }

    and the function will have to check sizes and copy the source string into the target and set a return status value. Can the former be done? If so, how?

    Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

    CPalliniC 1 Reply Last reply
    0
    • B bkelly13

      Visual Studio C++ I need a function that would be declared like this:

      class Messages
      { ...
      WCHAR get_string();
      ... }

      and be called something like this

      WCHAR new_string[ MAX ];
      new_string = p_messages->get_string()
      // where p_messages is a pointer to that class

      I have tried that syntax but all readers probably know the errors so they are omitted. I am thinking it must be something more like

      class Messages
      { ...
      bool get_string( WCHAR * target, int target_size );
      ... }

      and the function will have to check sizes and copy the source string into the target and set a return status value. Can the former be done? If so, how?

      Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Yes, a function may return a WCHAR (and your function, misnamed as it is, does it). Howevever, in your code you are trying to assign it to an array name (and you can't do that). The

      bool get_string( WCHAR * target, int target_size );

      function can properly modify the passed array (for instance, Windows API use such a mechanism). Please note, in C++ you could use std::wstring[^] instead of an array of WCHARs.

      In testa che avete, signor di Ceprano?

      B 1 Reply Last reply
      0
      • CPalliniC CPallini

        Yes, a function may return a WCHAR (and your function, misnamed as it is, does it). Howevever, in your code you are trying to assign it to an array name (and you can't do that). The

        bool get_string( WCHAR * target, int target_size );

        function can properly modify the passed array (for instance, Windows API use such a mechanism). Please note, in C++ you could use std::wstring[^] instead of an array of WCHARs.

        B Offline
        B Offline
        bkelly13
        wrote on last edited by
        #3

        Point taken. What I really need is a function that returns a string, an array of WCHAR. I am pretty sure that can be done with CString, but I have had problems using CString. The question more properly is: Can a function return a string of WCHAR? You mentioned std::wstring rather than WCHAR. During startup this app reads a file of some 80,000 data items to describe telemetry parameters it must process. That file is text based. Each line of the text file is tokenized, put in a vector, then the fields can be easily processed to extract the needed info. Speed is not real critical but so far things seem to work better with WCHAR. Do you have a preference for std::wstring over WCHAR? If so can you explain the reason or direct me to a good comparison of the two?

        Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

        Richard Andrew x64R CPalliniC 2 Replies Last reply
        0
        • B bkelly13

          Point taken. What I really need is a function that returns a string, an array of WCHAR. I am pretty sure that can be done with CString, but I have had problems using CString. The question more properly is: Can a function return a string of WCHAR? You mentioned std::wstring rather than WCHAR. During startup this app reads a file of some 80,000 data items to describe telemetry parameters it must process. That file is text based. Each line of the text file is tokenized, put in a vector, then the fields can be easily processed to extract the needed info. Speed is not real critical but so far things seem to work better with WCHAR. Do you have a preference for std::wstring over WCHAR? If so can you explain the reason or direct me to a good comparison of the two?

          Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          Simply put, a function CAN return a string of WCHAR, but you have to return a pointer to the string. Therefore:

          WCHAR* FunctionThatReturnsString()
          {
          WCHAR* NewString = new WCHAR[255];
          return NewString;
          }

          The error you were making before was that you were returning a single WCHAR character, not a pointer.

          The difficult we do right away... ...the impossible takes slightly longer.

          1 Reply Last reply
          0
          • B bkelly13

            Point taken. What I really need is a function that returns a string, an array of WCHAR. I am pretty sure that can be done with CString, but I have had problems using CString. The question more properly is: Can a function return a string of WCHAR? You mentioned std::wstring rather than WCHAR. During startup this app reads a file of some 80,000 data items to describe telemetry parameters it must process. That file is text based. Each line of the text file is tokenized, put in a vector, then the fields can be easily processed to extract the needed info. Speed is not real critical but so far things seem to work better with WCHAR. Do you have a preference for std::wstring over WCHAR? If so can you explain the reason or direct me to a good comparison of the two?

            Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Quote:

            but I have had problems using CString

            What problems?

            bkelly13 wrote:

            The question more properly is: Can a function return a string of WCHAR?

            Yes, you can do that (and Richard Andrew x64 already shown you how) however it is error prone, because the allocation is performed by the called function while memory release is left to the caller (simply put: you should know what you are doing). std::wstring (or CString), on the other hand handles the memory allocation/release mechanism internally.

            Quote:

            ou mentioned std::wstring rather than WCHAR. During startup this app reads a file of some 80,000 data items to describe telemetry parameters it must process. That file is text based. Each line of the text file is tokenized, put in a vector, then the fields can be easily processed to extract the needed info. Speed is not real critical but so far things seem to work better with WCHAR. Do you have a preference for std::wstring over WCHAR? If so can you explain the reason or direct me to a good comparison of the two?

            If speed is not critical I would definitely use std::wstring (or CString) insted of WCHAR arrays (avoiding the hassle of memory handling with the benefit of a more compact and possibly robust code).

            In testa che avete, signor di Ceprano?

            B 1 Reply Last reply
            0
            • CPalliniC CPallini

              Quote:

              but I have had problems using CString

              What problems?

              bkelly13 wrote:

              The question more properly is: Can a function return a string of WCHAR?

              Yes, you can do that (and Richard Andrew x64 already shown you how) however it is error prone, because the allocation is performed by the called function while memory release is left to the caller (simply put: you should know what you are doing). std::wstring (or CString), on the other hand handles the memory allocation/release mechanism internally.

              Quote:

              ou mentioned std::wstring rather than WCHAR. During startup this app reads a file of some 80,000 data items to describe telemetry parameters it must process. That file is text based. Each line of the text file is tokenized, put in a vector, then the fields can be easily processed to extract the needed info. Speed is not real critical but so far things seem to work better with WCHAR. Do you have a preference for std::wstring over WCHAR? If so can you explain the reason or direct me to a good comparison of the two?

              If speed is not critical I would definitely use std::wstring (or CString) insted of WCHAR arrays (avoiding the hassle of memory handling with the benefit of a more compact and possibly robust code).

              B Offline
              B Offline
              bkelly13
              wrote on last edited by
              #6

              Regarding what problems with CString? I don't remember the details right now. I think my problem in that area is settling down with one representation for strings. I fetch some critical data using the API of another application (I'll update this when I get back to work and verify), then the code reads text from a file, tokenizes it, builds parameters names with odd formatting and does a lot of compares. I would like to make this app easy to port to a Unix/Linux platform but am not obsessed with that. Selecting a single character and string representation is not trivial. I am receptive to suggestions as to my selection of the basic character/string representation. I am leaning towards WCHAR but can be convinced otherwise. The need for a simple tokenizer is strong. Regarding the function returning a string, avoiding the problems of allocation is rather important. It seems that I cannot so something like:

              WCHAR target[ MAX ];
              ...
              target[ 4 ] = get_paremeter_class( );

              Where get_string() will return a short phrase that is frequently used. The resultant string would look like: S01_ANA_0001, meaning "String 01, Analog, parameter 0001" or S01_DIG_0001. I am thinking of writing a procedure that will accept a pointer to target along with a size and copy that short string into target. I want the higher level to look simple and let the frequently called function handle the ugly details.

              Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

              CPalliniC 1 Reply Last reply
              0
              • B bkelly13

                Regarding what problems with CString? I don't remember the details right now. I think my problem in that area is settling down with one representation for strings. I fetch some critical data using the API of another application (I'll update this when I get back to work and verify), then the code reads text from a file, tokenizes it, builds parameters names with odd formatting and does a lot of compares. I would like to make this app easy to port to a Unix/Linux platform but am not obsessed with that. Selecting a single character and string representation is not trivial. I am receptive to suggestions as to my selection of the basic character/string representation. I am leaning towards WCHAR but can be convinced otherwise. The need for a simple tokenizer is strong. Regarding the function returning a string, avoiding the problems of allocation is rather important. It seems that I cannot so something like:

                WCHAR target[ MAX ];
                ...
                target[ 4 ] = get_paremeter_class( );

                Where get_string() will return a short phrase that is frequently used. The resultant string would look like: S01_ANA_0001, meaning "String 01, Analog, parameter 0001" or S01_DIG_0001. I am thinking of writing a procedure that will accept a pointer to target along with a size and copy that short string into target. I want the higher level to look simple and let the frequently called function handle the ugly details.

                Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                Quote:

                WCHAR target[ MAX ]; ... target[ 4 ] = get_paremeter_class( );

                Again, what is target[4] supposed to hold? It can just hold a character, not a string.

                In testa che avete, signor di Ceprano?

                B 1 Reply Last reply
                0
                • CPalliniC CPallini

                  Quote:

                  WCHAR target[ MAX ]; ... target[ 4 ] = get_paremeter_class( );

                  Again, what is target[4] supposed to hold? It can just hold a character, not a string.

                  B Offline
                  B Offline
                  bkelly13
                  wrote on last edited by
                  #8

                  Hello, Get_paremeter_class() was intended to be a function returning a string. In this case the plan was to have the string returned to target beginning with the fifth character in target. If target started with contents "abcdefghi" and the function returned "XYZ" then target would contain "abcdXYZhi" I am pretty sure this will not work. What character/string types do you recommend? I read that CString is a Microsoft creation so I think I would prefer something else. The code builds many strings with wprintf_s(...).

                  Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

                  CPalliniC 1 Reply Last reply
                  0
                  • B bkelly13

                    Hello, Get_paremeter_class() was intended to be a function returning a string. In this case the plan was to have the string returned to target beginning with the fifth character in target. If target started with contents "abcdefghi" and the function returned "XYZ" then target would contain "abcdXYZhi" I am pretty sure this will not work. What character/string types do you recommend? I read that CString is a Microsoft creation so I think I would prefer something else. The code builds many strings with wprintf_s(...).

                    Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    bkelly13 wrote:

                    In this case the plan was to have the string returned to target beginning with the fifth character in target. If target started with contents "abcdefghi" and the function returned "XYZ" then target would contain "abcdXYZhi"
                    I am pretty sure this will not work.

                    Could you please elaborate (detailing exactly what are the inputs and what should be the output)? I didn't get you.

                    bkelly13 wrote:

                    I read that CString is a Microsoft creation so I think I would prefer something else.

                    The underlying operative system is a Microsoft creation too. CString is just fine. You could also use, if you like, std::wstring.

                    In testa che avete, signor di Ceprano?

                    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