Solved

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

Posted on 2004-09-24
9
21,958 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

821 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