Solved

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

Posted on 2004-09-24
9
21,550 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
Comment Utility
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
Comment Utility
On use TEXT macro, You nothing more needed

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

Expert Comment

by:msjammu
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
>>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
Comment Utility
Thank you jkr,
it works perfectly.

  HStrix

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
Thank you jkr,
it's always good to know such "small" things.

   HStrix
0
 
LVL 8

Expert Comment

by:milindsm
Comment Utility
thanks jkr... it helped me too :-)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

14 Experts available now in Live!

Get 1:1 Help Now