none
Conversations: wchar_t * to CString, wchar_t * to String, Cstring to wchar_t *, String to wchar_t *

    Question

  • Hi. How i can convert these type of variables:

    wchar_t * to CString
    wchar_t * to String
    Cstring to wchar_t *
    String to wchar_t *

    Thank you.
    Friday, March 21, 2008 9:47 AM

Answers

  • wchar_t *p = L"This is a wchar_t string";

    CString str(L"This is an initialized CString");

     

    // wchar_t * to CString

    CString str(p);

     

    // wchar_t * to String

    // Not sure, perhaps this works:

    CString str(p);

     

    // Cstring to wchar_t *

    wchar_t *p = (wchar_t *) str;  // use CString:Surpriseperator LPCTSTR

     

    // String to wchar_t *

    String ^str = new String();

    pin_ptr<const wchar_t> pPinWide = PtrToStringChars(str);

     

    -- David

    Friday, March 21, 2008 8:32 PM

All replies

  • Hi,

    wchar_t* is a pointer, so you can convert it to something else with using of interpret_cast.

    Friday, March 21, 2008 10:09 AM
  • Mykhaylo Terentyak:

    How i can convert back from CString to wchar_t * ?
    Friday, March 21, 2008 1:43 PM
  • Are you sure that you want to convert pointer to string and string to pointer? Maybe you want to convert wchar_t -> CString and CString -> wchar_t.

    To convert CString to wchar_t* you also should use reinterpret_cast.
    Friday, March 21, 2008 1:48 PM
  • em, i need to: wchar_t * to CString  and CString to wchar_t
    CString pathw = static_cast<CString>(wszNewStr);
    here is ok

    but then i try convert to wchar_t it returns mistake. why?

            wchar_t fullpath = reinterpret_cast<wchar_t>(pathw);

    Friday, March 21, 2008 1:51 PM
  • Please, take a look at this:

    http://msdn2.microsoft.com/en-us/library/ms235631(VS.80).aspx
    http://p2p.wrox.com/topic.asp?TOPIC_ID=56284
    Friday, March 21, 2008 1:59 PM
  • wchar_t *p = L"This is a wchar_t string";

    CString str(L"This is an initialized CString");

     

    // wchar_t * to CString

    CString str(p);

     

    // wchar_t * to String

    // Not sure, perhaps this works:

    CString str(p);

     

    // Cstring to wchar_t *

    wchar_t *p = (wchar_t *) str;  // use CString:Surpriseperator LPCTSTR

     

    // String to wchar_t *

    String ^str = new String();

    pin_ptr<const wchar_t> pPinWide = PtrToStringChars(str);

     

    -- David

    Friday, March 21, 2008 8:32 PM
  • thnx very much
    may be anybody know how detect how many real symbols has wchar_t variable massive after request? Because i set maximum but the request sets as it needs. I speak about pBuf variable:

    TCHAR pBuf[MAX_PATH];
    ITEMIDLIST * pidl = 0;
    BROWSEINFO bi = {0};
    bi.hwndOwner = m_hWnd; // хэндл окна-родителя
    bi.lpszTitle = _T("Выбор каталога");
    bi.pidlRoot = 0;
    bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_DONTGOBELOWDOMAIN;
    pidl = SHBrowseForFolder(&bi);
    if(pidl && SHGetPathFromIDList(pidl, pBuf))
    {
        AfxMessageBox(pBuf);
    }

    Answer: size_t len = wcslen(pBuf);

    anyway thnx.
    Friday, March 21, 2008 9:56 PM