簡短版本我正在使用unicode。我試圖使用一個std :: string函數,它需要一個const WCHAR 字符串;的DrawString(常量WCHAR ...將std :: string轉換爲const所需的WCHAR *字符串GDI +參數
我用GCC編譯,一切都是unicode的,我已經指定。
我一直在試圖將字符串轉換成一個wchar_t的*,其目的是爲了讓我可以輸出使用GDI +的功能,它的參數需要這麼
這裏是我已經輸出字符串文字,沒有任何問題,調試精細,做工精細 http://msdn.microsoft.com/en-us/library/ms535991%28v=vs.85%29.aspx參考原因:。
// works fine
wchar_t* wcBuff;
wcBuff = (wchar_t*)L"Some text here.\0";
AddString(wcBuff, wcslen(wcBuff), &gFontFamilyInfo, FontStyleBold, 20, ptOrg_Controls, &strFormat_Info);
現在,這是我一直在努力的一整天和一個方面的說明:我的轉換功能正常工作,這不是問題,也不是創建一個問題。
// problems
string s = "Level " + convert::intToString(6) + "\0";
// try 1 - Segfault
wchar_t* wcBuff = new wchar_t[s.length() + 1];
copy(s.begin(), s.end(), wcBuff);
// random tries, compiles, but access violations (my conversion function here has worked other places, do not know for sure here.
wchar_t* wcBuff;
wstring wstr = convert::stringToWideChar(s);
wstring strvalue = convert::stringToWideChar(s);
wcBuff = (wchar_t*)strvalue.c_str();
wcBuff = (wchar_t*)wstr.c_str();
wstring foo;
foo.assign(s.begin(), s.end());
wcBuff = (wchar_t*)foo.c_str();
一切都編譯完成,但是後來出現問題。一旦達到該點,就會出現一些運行時錯誤。其他人訪問違規和段錯誤。一些編譯和調試沒有問題,但字符串輸出不斷隨着隨機字符變化。
任何想法?
duplicate http://stackoverflow.com/questions/2573834/c-convert-string-or-char-to-wstring-or-wchar-t?rq=1 – Ulterior 2014-09-24 21:18:14
重複http://stackoverflow.com/questions/246806/i-want-to-convert-stdstring-into-a-const-wchar-t – WhozCraig 2014-09-24 21:23:44
我實際上讀過這個話題,並嘗試過所有的例子,他們都沒有工作。我沒有使用visual studio,所以我沒有包含頭文件。 @Potatoswatter的兩個例子都是隨機的字符串輸出。 – 2014-09-24 21:25:16