Solved

LPCWSTR -> CString

Posted on 1998-10-12
3
1,843 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

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
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 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.

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

20 Experts available now in Live!

Get 1:1 Help Now