Solved

VC++: how-to convert LPWSTR  to   CHAR*

Posted on 2004-09-24
9
22,173 Views
Last Modified: 2011-10-03
Hello experts,
in my C++ program (console app)  I need to convert a LPWSTR to CHAR*.
---
      //*********************************      
      wchar_t *pServer = L"\\\\mycomputer";            // server to run on
      wchar_t *pShare  = L"myshare";                            // sharename to query
      //*********************************

      NET_API_STATUS rc; // result code
      SHARE_INFO_2 *pSI;
      const char *sharetype;
      LPBYTE pBuf;

      pBuf = NULL;
      rc = NetShareGetInfo(  pServer, pShare, 2, &pBuf );

      if ( ( rc == NERR_Success || rc == ERROR_MORE_DATA ) && pBuf != NULL )
      {
            pSI = (SHARE_INFO_2 *) pBuf;
            switch ( pSI->shi2_type )
            {
            case STYPE_DISKTREE:
                  sharetype = "disk";
                  break;
            case STYPE_PRINTQ:
                  sharetype = "print queue";
                  break;
            case STYPE_DEVICE:
                  sharetype = "comms queue";
                  break;
            case STYPE_IPC:
                  sharetype = "IPC";
                  break;
            default:
                  sharetype = "unknown";
                  break;
            }
            char* netname;
            //  existing definitions : LMSTR shi2_netname;  #define LMSTR   LPWSTR

                                // the following line is for the conversion statement I don't know of
            //netname   ====  pSI->shi2_netname);  

                                // Now I want to give a message:
            MessageBox(0,netname,"NetName",MB_OK);  
      }
---
If you know how to do this
please modifythe appropriate statement(s) above.

Thank you for any help.

   HStrix

 
0
Comment
Question by:HStrix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 12142489
Shouldn't the messagebox command accept wide chars when compiled on unicode? I think that it translates to either
MessageBoxA or MessageBoxW depending on the character settings of the project.
0
 
LVL 6

Expert Comment

by:msjammu
ID: 12142529
On use TEXT macro, You nothing more needed

MessageBox(0,TEXT(netname),"NetName",MB_OK);  
0
 
LVL 6

Expert Comment

by:msjammu
ID: 12142562
I also Recommend you to look this thread, also my comments in it,
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21126082.html
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:AlexFM
ID: 12142688
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_atl_string_conversion_macros.asp

#include <atlbase.h>


USES_CONVERSION;

SHARE_INFO_2 *pSI;
char* netname;

...

netname = W2A(pSI->shi2_netname);     // LPWSTR -> LPSTR
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 12142796
>>I need to convert a LPWSTR to CHAR*.

LPWSTR pwz = "some text";
char* psz = new char [wcslen(pwsz) + 1];

wsprintfA ( psz, "%S", pwsz);

The "%S" will implicitly convert UNICODE to ANSI.
0
 

Author Comment

by:HStrix
ID: 12143858
Thank you jkr,
it works perfectly.

  HStrix

0
 
LVL 86

Expert Comment

by:jkr
ID: 12143923
BTW, just as an additional info the same works the other way round, too:

char* psz = "some text";
wchar_t* pwsz = new wchar_t [strlen(psz) + 1];

wsprintfW ( pwsz, L"%S", psz);

0
 

Author Comment

by:HStrix
ID: 12145876
Thank you jkr,
it's always good to know such "small" things.

   HStrix
0
 
LVL 8

Expert Comment

by:milindsm
ID: 21160458
thanks jkr... it helped me too :-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question