简体   繁体   中英

How to display string from LPVOID type in Edit control

I have problem when using File Mapping and read it.

Bellow code, i get pMemory form MapViewOfFile, but I don't know how to display content to Edit control, I only get mess characters (like Chinese characters). I want to use UNICODE

szFileName = L"abc.txt";
hFile = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, NULL);
hMapFile = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);

pMemory = MapViewOfFile(hMapFile, FILE_MAP_READ, 0,0,0);
FileSize = GetFileSize(hFile, NULL);
SendMessage(hWndEdit, WM_SETTEXT, 0, (LPARAM)(LPCWSTR)pMemory);
UnmapViewOfFile(pMemory);
CloseHandle(hMapFile);
CloseHandle(hFile);

Anyone can help me ???

UPDATE: I've debugged pMemory and sure it contain address. And when I use file with only ASCII text + use SendMessageA --> it work pretty fine, display correct text (but only not separate line). But if I use text with UNICODE text, it don't display correct characters

If your file data is not UTF-16 encoded, but your Edit window is using UTF-16 (because it was registered with RegisterClassW() or RegisterClassExW() ), then you must either:

  1. convert the file data to UTF-16 yourself using MultiByteToWideChar() (or equivalent) and then use SendMessageW() to send WM_SETTEXT .

  2. use SendMessageA() to send WM_SETTEXT and let the OS convert the data to UTF-16 for you using the OS's default Ansi codepage (this only works if the file data matches the OS's default encoding).

If your file data is UTF-16 encoded, but your Edit window is not using UTF-16 (because it was registered with RegisterClassA() or RegisterclassExA() ), then you must either:

  1. convert the file data to Ansi yourself using WideCharToMultiByte() (or equivalent) and then use SendMessageA() to send WM_SETTEXT .

  2. use SendMessageW() to send WM_SETTEXT and let the OS convert the data to Ansi for you using the OS's default Ansi codepage.

If your file data is UTF-16 encoded and your Edit window is using UTF-16, or if your file data is Ansi encoded and your Edit window is using Ansi and the encodings match, then you can use the generic SendMessage() to send WM_SETTEXT passing the data as-is without needing to do any conversions.

You can use IsWindowUnicode() to know whether the Edit window is using Ansi or UTF-16. As for the file data, you could try using IsTextUnicode() , but that is known to report false results, so you really should not rely on it. Know what your file encoding is ahead of time and code for that, or else prompt the user for the file encoding.

As you said, your file have not have UTF-16 encoded, but your Edit control is using UTF-16. You can use MultiByteToWideChar to convert UTF-8 to UTF-16.

Note that use MultiByteToWideChar you need call 2 times, example:

int size_needed = MultiByteToWideChar(CP_UTF8, 0, (LPCCH)pMemory, -1, NULL, 0);
wchar_t *buffer = new wchar_t[size_needed];
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)pMemory, -1, buffer, size_needed);

First time is get size needed to convert and Second is put into wide string. See more: http://msdn.microsoft.com/en-us/library/windows/desktop/dd319072%28v=vs.85%29.aspx

Your code can be edited:

szFileName = L"abc.txt";
hFile = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, NULL);
hMapFile = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
pMemory = MapViewOfFile(hMapFile, FILE_MAP_READ, 0,0,0);

int size_needed = MultiByteToWideChar(CP_UTF8, 0, (LPCCH)pMemory, -1, NULL, 0);
wchar_t *buffer = new wchar_t[size_needed];
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)pMemory, -1, buffer, size_needed);

FileSize = GetFileSize(hFile, NULL);
SendMessage(hWndEdit, WM_SETTEXT, 0, (LPARAM)buffer);
delete[] buffer;
UnmapViewOfFile(pMemory);
CloseHandle(hMapFile);
CloseHandle(hFile);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM