Solved

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

Posted on 2004-09-24
9
21,718 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now