Sign in to follow this  
JohnsonGPS

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

Recommended Posts

JohnsonGPS    109
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this