Hey,
i found the solution for the utf-8 encoding problem:
i used the:
wchar_t chAppWinTextUnicode[MAX_PATH] = { 0 };
::*GetWindowTextW*(hWnd, chAppWinTextUnicode, MAX_PATH);
int nSize = *WideCharToMultiByte*(/*CP_UTF8*/, 0, chAppWinTextUnicode,
-1, NULL, 0, NULL, NULL);
char* pchBuffer= (char*) malloc(nSize + 1);
*WideCharToMultiByte*(/*CP_UTF8*/, 0, chAppWinTextUnicode, -1,
pchBuffer, nSize, NULL, NULL);
...
...
the save the pchBuffer to std::string str(pchBuffer);
then this string will be correctly saved into sqlite :)
thanks very much for your helps!
best regards
Ming
On 16.12.2010 17:34, Afriza N. Arief wrote:
> @Cory: correct.
>
> Fixed Code:
>
> HWND hWnd = /* from somewhere */;
> int nLen = GetWindowTextLength(hWnd) + 1;
> WCHAR* lpszText = (WCHAR*)malloc(nLen*sizeof(WCHAR));
> // std::wstring str; str.resize(nLen);
> nLen = GetWindowText(hWnd,lpszText,nLen);
> // str.resize(nLen=GetWindowText(hWnd,&str[0],nLen));
> sqlite3_bind_text16(stmt,1,lpszText,nLen*sizeof(WCHAR),SQLITE_TRANSIENT);
> // (.., str.data(),nLen*sizeof(std::wstring::value_type),SQLITE_TRANSIENT);
> free(lpszText); // not needed for std::wstring
>
> When you first time create the database, use sqlite3_open() to store
> the data as utf8. I believe bind_text16() will convert from utf16 to
> utf8 for you.
>
> See:
> - GetWindowTextLength() http://msdn.microsoft.com/en-us/library/ms633521.aspx
> - GetWindowText() http://msdn.microsoft.com/en-us/library/ms633520.aspx
> - sqlite3_bind_text16() http://www.sqlite.org/c3ref/bind_blob.html
>
> Note:
> - You need your project to be compiled in UNICODE
>
> On Fri, Dec 17, 2010 at 12:24 AM, Cory Nelson<[email protected]> wrote:
>> Just a quick note -- TCHAR is not always UTF-16, and
>> sqlite3_bind_text16 takes a void* so it will happily take whatever you
>> give it. You should be using wchar_t directly instead of TCHAR, so an
>> error can be caught when you use GetWindowText.
>>
> _______________________________________________
> sqlite-users mailing list
> [email protected]
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users