Solved

LPCWSTR -> CString

Posted on 1998-10-12
3
1,838 Views
Last Modified: 2012-08-14
I'm using a function called NetMessageBufferSend() that requires a LPCWSTR data type.  This string does not seem to be compatible with the CString.  I have created a WCHAR string that seems to work with the funtion, however, I am having problems transfering the data from the CString into the LPCWSTR.

I have tried wsprintf(),strcpy(),and wcscpy(), but none of them seem to work!

Can anyone please show me how to copy the contents of a CString into a WCHAR string(LPCWSTR)?
0
Comment
Question by:SileNcer
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 70 total points
ID: 1174927
A LPCWSTR is a UNICODE string.  If your application is all UNICODE, then this is typically not an issue as the CString class will be UNICODE by default.  If all you need to do is convert the ANSI string in the CString object into a UNICODE string, you can use a simple routine like:

LPWSTR ANSI2UNICODE(LPCSTR cvt_str)
{
      LPWSTR rv;
      int bufsiz, stat;

      if(cvt_str == NULL || strlen(cvt_str) == 0){
            return NULL;
      }

      // Get the needed conversion buffer size
      bufsiz = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cvt_str, -1, NULL, 0);

      // Make a space for the converted data
      rv = (LPWSTR)malloc(bufsiz * sizeof(WCHAR));

      // Now convert
      stat = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cvt_str, -1, rv, bufsiz);

      if(stat == 0){
            return NULL;
      }

      return rv      ;
}
0
 

Author Comment

by:SileNcer
ID: 1174928
The application was all in unicode, so I figured out how to call a pointer to the string.  I am still having problems getting the NetMessageBufferSend() to work.

The program will compile with this code:

LPBYTE message = (LPBYTE)*m_message.GetBuffer(2);
DWORD length = 500;
NetMessageBufferSend(
NULL,                  //server name
(LPCWSTR)*m_domain,      //send to name
(LPCWSTR)*m_username,      //from name
message,            //message
length);            //message length

But it keeps giving me this error:
error LNK2001: unresolved external symbol _NetMessageBufferSend@20

Is there something I'm not doing right with the pointers or something?


0
 

Author Comment

by:SileNcer
ID: 1174929
Okay! I figured out the problem.  I needed to include netapi32.lib to my project, so it wasn't the pointers after all.
0

Featured Post

Free Trending Threat Insights Every Day

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 the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

747 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

12 Experts available now in Live!

Get 1:1 Help Now