Solved

LPCWSTR -> CString

Posted on 1998-10-12
3
1,848 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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 learn how to clear a vector as well as how to detect empty vectors in C++.

820 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