Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-09-24
9
Medium Priority
?
22,577 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

722 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