?
Solved

LPCWSTR -> CString

Posted on 1998-10-12
3
Medium Priority
?
1,866 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 140 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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