• Advertisement
Sign in to follow this  

Wierd: the first character is always different than the original after convertion

This topic is 3190 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using Visual C++ 2008 Express Edition, and I need to convert the text of the TextBox from System::String^ to TCHAR *. I found there was a post at http://bytes.com/groups/net-vc/615998-conversion-system-string-lpctstr So I copy and paste the routine StringConvT, then modify a little bit to fit into my program. See the codes at the bottom. Here is my one line of codes to call the conversion. However, after convertion, the first character was always changed to a greek-like character ξ. For example, "C:\Test\Test.txt" to "ξ:\Test\Test.txt". TCHAR * sFN = StringConvT(this->textBoxFile->Text) ; I debugged the software, and found the path was normal even before it was returned. At line "return szUnicode;", szUnicode looked right. However, right after it was sent to sFN, I found the first character became wierd. Does anybody know what happened? Is there anything related to my computer? I recently switched from XP to Vista. Thank you! Here are the codes that I borrowed from the website.
  #ifdef _UNICODE
    #define StringConvT StringConvW
  #else
    #define StringConvT StringConvA
  #endif


  struct StringConvA
  {
    char *szAnsi;
    StringConvA(System::String ^s):
    szAnsi(static_cast<char*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer()))
    {}

    ~StringConvA()
    {
      System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szAnsi));
    }

    operator char*() const
    {
      return szAnsi;
    }
  };

  struct StringConvW
  {
    wchar_t *szUnicode;
    StringConvW(System::String^ s):
    szUnicode(static_cast<wchar_t*>(System::Runtime::InteropServices::Marshal::StringToHGlobalUni(s).ToPointer()))

    {}
    ~StringConvW()
    {
      System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szUnicode));
    }

    operator wchar_t*() const
    {
      return szUnicode;
    }
  };

[edit] Added source tags - Apoch [Edited by - ApochPiQ on May 1, 2009 3:47:24 PM]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement