Is there any function or macro to achieve LPCWSTR-> LPWSTR and LPCSTR-> LPSTR and do not release ?

Category: VC/MFC -> Foundation Classes Author: jiahtbupt Date: 2003-03-01 12:13:15
 
jiahtbupt
2003-03-01 12:13:15
Or the problem may be changed CStringW- > LPWSTR and CStringA- > LPSTR. The main thing is recently doing ADSI programming , some functions such as IDirectorySearch where ExecuteSearch and other parameters are required LPWSTR, I do not know why you want to pass a modified , after all the arguments I will not go read it, but let it pass on the pass .

I am using the method
CString   strFilter   =   ...
hr   =   pSearch- > ExecuteSearch (_bstr_t (strFilter),   pszAttr,   dwCount,   & hSearch);
seem _bstr_t const string can be modified into a string , and do not release , I do not know this , right ?
function is declared as follows (Iads.h), only unicode form
virtual   HRESULT   STDMETHODCALLTYPE   ExecuteSearch ( 
                        / *   [in]   * /   LPWSTR   pszSearchFilter,
                        / *   [in]   * /   LPWSTR   * pAttributeNames,
                        / *   [in]   * /   DWORD   dwNumberAttributes,
                        / *   [ out]   * /   PADS_SEARCH_HANDLE   phSearchResult)   =   0;

question , the original program is UNICODE now changed under ANSI. Now CString and _bstr_t on all hell broke loose . I'm looking at codeproject in a source, which implements the CStdStringA and CStdStringW ( I VC6, does not own CStringA   CStringW), so I replace the relevant CString into CStdStringW, still be used. But how calls are ansi and unicode form _bstr_t it now _bstr_t (LPCSTR) is normal, but _bstr_t (LPCWSTR) is garbled. .

I do not know , I did not explain clearly , is to solve a problem can be any of the following :
1) LPCWSTR- > LPWSTR and LPCSTR- > not release LPSTR, back , releasing it trouble, but also prone to error, code chaos
2) _bstr_t is not able to achieve this function , they do not release ? That how calls are ansi and unicode form _bstr_t it ?
qqciayue
2003-03-01 12:30:02
This is a wide-character unicode convert to ascii program ( support Chinese ) , see if there is no use
class   CStr

{

public:

CStr (const   CString   & str)

{

len   =   WideCharToMultiByte (CP_ACP, 0 , str, -1, NULL, 0, NULL, FALSE);   //   return u back byte & uacute;??? number

ch   =   new   char [ len];

WideCharToMultiByte (CP_ACP, 0, str, -1, ch, len, NULL, FALSE);

}

char *   GetChars ()

{  return   ch;  }

int   GetLength ()

{  return   len ;  }

~ CStr ()

{  delete   ch;  }

private:
<br

};; /> char *   ch;   int   len  
lilibinbin2008
2003-03-01 12:38:07
cumbersome way I would actually , is a variety of distribution space , various release   delete hate ah, so I want to achieve the following macro, and does not require any advance assignment , do not need to delete anything afterwards , I do not know what
feasible BTW, I wrote the content is wrong and without reference to Kazakhstan

///////////////////////////////////// ////////////////////////////////////////////////
# define   CStringA   CStdStringA
# define   CStringW   CStdStringW
//////////////////////////// ////////////////////////////////////////////////// ///////
# define   CString2LPWSTR (strCString)   (_ bstr_t)   (CStdStringW (strCString) .c_str ())
# define   CString2LPSTR (strCString)   (_ bstr_t )   (CStdStringA (strCString) .c_str ())
# define   CString2LPCWSTR (strCString)   (_ bstr_t)   (CStdStringW (strCString) .c_str ())
# define   CString2LPCSTR ( strCString)   (_ bstr_t)   (CStdStringA (strCString) .c_str ())
/////////////////////////// ////////////////////////////////////////////////// ////////
# define   CString2LMSTR (strCString)   ((LMSTR)   CString2LPWSTR (strCString))
# define   CString2LMCSTR (strCString)   ((LMCSTR)   CString2LPCWSTR (strCString))
//////////////////////////////////////// /////////////////////////////////////////////
# define   LPSTR2CString (lpStr)   (CString)   ((CStdStringA)   (lpStr))
# define   LPWSTR2CString (lpStr)   (CString)   ((CStdStringW)   (lpStr) )
///////////////////////////////////////////// ////////////////////////////////////////
# define   CString2CStringA (strCString )   ((CStdStringA)   strCString)
# define   CString2CStringW (strCString)   ((CStdStringW)   strCString)
# define   CStringA2CString (strCStringA)   ((CString)   strCStringA)
# define   CStringW2CString (strCStringW)   ((CString)   strCStringW)
////////////////////// ////////////////////////////////////////////////// /////////////
a524513
2003-03-01 12:40:41
I am using the way _bstr_t   +   CString, no need to write something else , very safe , do not know the speed Oh