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. How to convert PWSTR to char * ?

How to convert PWSTR to char * ?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
15 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.
  • N Nish Nishant

    Lauren, I asked Tom this very question just now. I was about 30 seconds later than you were though :-) Nish


    The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

    L Offline
    L Offline
    l a u r e n
    wrote on last edited by
    #6

    hehe great minds nish great minds :)


    situations to avoid #37:
    "good morning ... how many sugars do you take in your coffee ... and what was your name again?"

    coming soon: situations to avoid #38: "...and the dog was there too?"

    A N 2 Replies Last reply
    0
    • L l a u r e n

      hehe great minds nish great minds :)


      situations to avoid #37:
      "good morning ... how many sugars do you take in your coffee ... and what was your name again?"

      coming soon: situations to avoid #38: "...and the dog was there too?"

      A Offline
      A Offline
      adara
      wrote on last edited by
      #7

      :rose:

      1 Reply Last reply
      0
      • L l a u r e n

        hey tom can you not just use: USES_CONVERSION; *pChar = W2A(*pWideChar); ????


        situations to avoid #37:
        "good morning ... how many sugars do you take in your coffee ... and what was your name again?"

        coming soon: situations to avoid #38: "...and the dog was there too?"

        T Offline
        T Offline
        Tom Archer
        wrote on last edited by
        #8

        Yep. Does the same thing and definitely much less typing and remembering of parameter values. Great catch! By the way, check out what these macros expand to :omg:

        Original C++ code

        void SayHello2(PWSTR wstrName)
        {
        USES_CONVERSION;
        AfxMessageBox(W2A(wstrName));
        }

        Expanded macros

        void SayHello2(PWSTR wstrName)
        {
        int _convert = 0;
        _convert;
        UINT _acp = ATL::_AtlGetConversionACP() ;
        _acp;
        LPCWSTR _lpw = 0;
        _lpw;
        LPCSTR _lpa = 0;
        _lpa;

        AfxMessageBox((((_lpw = wstrName) == 0) ? 0
        : (_convert = (lstrlenW(_lpw)+1)*2,
        AtlW2AHelper((LPSTR) _alloca(_convert),
        _lpw,
        _convert,
        _acp))));
        }

        Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

        N 1 Reply Last reply
        0
        • L l a u r e n

          hehe great minds nish great minds :)


          situations to avoid #37:
          "good morning ... how many sugars do you take in your coffee ... and what was your name again?"

          coming soon: situations to avoid #38: "...and the dog was there too?"

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #9

          lauren wrote: great minds nish great minds Yeah :-) I guess so :-) Nish


          The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

          1 Reply Last reply
          0
          • N Nish Nishant

            Hi Tom I am not at all experienced with ATL, but I was wondering why we cannot use W2A macro to achieve what the guy wanted to do! Nish


            The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

            T Offline
            T Offline
            Tom Archer
            wrote on last edited by
            #10

            Like a lot of things in Windows development, there's more than one way to accomplish this task. I'm just more accustomed to using the Win32 functions. As I told Lauren, using the ATL macros is certainly a lot less typing and remembering of parameters and therefore, probably the better way to accomplish this conversion task. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

            N 1 Reply Last reply
            0
            • T Tom Archer

              Yep. Does the same thing and definitely much less typing and remembering of parameter values. Great catch! By the way, check out what these macros expand to :omg:

              Original C++ code

              void SayHello2(PWSTR wstrName)
              {
              USES_CONVERSION;
              AfxMessageBox(W2A(wstrName));
              }

              Expanded macros

              void SayHello2(PWSTR wstrName)
              {
              int _convert = 0;
              _convert;
              UINT _acp = ATL::_AtlGetConversionACP() ;
              _acp;
              LPCWSTR _lpw = 0;
              _lpw;
              LPCSTR _lpa = 0;
              _lpa;

              AfxMessageBox((((_lpw = wstrName) == 0) ? 0
              : (_convert = (lstrlenW(_lpw)+1)*2,
              AtlW2AHelper((LPSTR) _alloca(_convert),
              _lpw,
              _convert,
              _acp))));
              }

              Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #11

              Ouch, that expanded code hurts my eyes :-) Nish


              The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

              1 Reply Last reply
              0
              • T Tom Archer

                Like a lot of things in Windows development, there's more than one way to accomplish this task. I'm just more accustomed to using the Win32 functions. As I told Lauren, using the ATL macros is certainly a lot less typing and remembering of parameters and therefore, probably the better way to accomplish this conversion task. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                N Offline
                N Offline
                Nish Nishant
                wrote on last edited by
                #12

                Tom Archer wrote: Like a lot of things in Windows development, there's more than one way to accomplish this task. I'm just more accustomed to using the Win32 functions. As I told Lauren, using the ATL macros is certainly a lot less typing and remembering of parameters and therefore, probably the better way to accomplish this conversion task. I think it's cool to know multiple ways to do something. It can be quite handy at times. Nish p.s. Got my mail? [with ppt]


                The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

                T 1 Reply Last reply
                0
                • N Nish Nishant

                  Tom Archer wrote: Like a lot of things in Windows development, there's more than one way to accomplish this task. I'm just more accustomed to using the Win32 functions. As I told Lauren, using the ATL macros is certainly a lot less typing and remembering of parameters and therefore, probably the better way to accomplish this conversion task. I think it's cool to know multiple ways to do something. It can be quite handy at times. Nish p.s. Got my mail? [with ppt]


                  The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

                  T Offline
                  T Offline
                  Tom Archer
                  wrote on last edited by
                  #13

                  Especially something like this where the person may need to use the Win32 SDK. Regarding the PPT. Yes, thank you. I replied to you. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                  1 Reply Last reply
                  0
                  • N Nish Nishant

                    Hi Tom I am not at all experienced with ATL, but I was wondering why we cannot use W2A macro to achieve what the guy wanted to do! Nish


                    The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

                    T Offline
                    T Offline
                    Tim Smith
                    wrote on last edited by
                    #14

                    The most serious drawback of W2A, W2CA, xx2xx, is that for each invocation, new stack space is allocated. So, you can't really use them in a loop. Other than that, I use them all the time. :) Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

                    N 1 Reply Last reply
                    0
                    • T Tim Smith

                      The most serious drawback of W2A, W2CA, xx2xx, is that for each invocation, new stack space is allocated. So, you can't really use them in a loop. Other than that, I use them all the time. :) Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

                      N Offline
                      N Offline
                      Nish Nishant
                      wrote on last edited by
                      #15

                      Tim Smith wrote: The most serious drawback of W2A, W2CA, xx2xx, is that for each invocation, new stack space is allocated. So, you can't really use them in a loop. I think these issues have been partially improved in ATL 7.0 Nish


                      The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

                      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