Solved

LPCWSTR -> CString

Posted on 1998-10-12
3
1,853 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

739 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