简体   繁体   English

如何在Edit控件中显示LPVOID类型的字符串

[英]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). Bellow代码,我得到了pMemory表单MapViewOfFile,但我不知道如何向E​​dit控件显示内容,我只得到乱七八糟的字符(就像汉字一样)。 I want to use UNICODE 我想使用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. 更新:我调试了pMemory并确定它包含地址。 And when I use file with only ASCII text + use SendMessageA --> it work pretty fine, display correct text (but only not separate line). 当我使用只有ASCII文本的文件+使用SendMessageA - >它工作得很好,显示正确的文本(但不是单独的行)。 But if I use text with UNICODE text, it don't display correct characters 但是,如果我使用UNICODE文本的文本,它不会显示正确的字符

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: 如果您的文件数据不是UTF-16编码,但您的编辑窗口使用的是UTF-16(因为它是使用RegisterClassW()RegisterClassExW() ),那么您必须:

  1. convert the file data to UTF-16 yourself using MultiByteToWideChar() (or equivalent) and then use SendMessageW() to send WM_SETTEXT . 使用MultiByteToWideChar() (或等效的)将文件数据转换为UTF-16,然后使用SendMessageW()发送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). 使用SendMessageA()发送WM_SETTEXT并让操作系统使用操作系统的默认Ansi代码页将数据转换为UTF-16(仅当文件数据与操作系统的默认编码匹配时才有效)。

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: 如果您的文件数据是UTF-16编码,但您的编辑窗口不使用UTF-16(因为它是使用RegisterClassA()RegisterclassExA() ),那么您必须:

  1. convert the file data to Ansi yourself using WideCharToMultiByte() (or equivalent) and then use SendMessageA() to send WM_SETTEXT . 使用WideCharToMultiByte() (或等效的)将文件数据转换为Ansi,然后使用SendMessageA()发送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. 使用SendMessageW()发送WM_SETTEXT并让操作系统使用操作系统的默认Ansi代码页将数据转换为Ansi。

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. 如果您的文件数据是UTF-16编码并且您的编辑窗口使用的是UTF-16,或者您的文件数据是Ansi编码而您的编辑窗口正在使用Ansi并且编码匹配,那么您可以使用通用SendMessage()发送WM_SETTEXT传递数据,无需进行任何转换。

You can use IsWindowUnicode() to know whether the Edit window is using Ansi or UTF-16. 您可以使用IsWindowUnicode()来了解Edit窗口是使用Ansi还是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. 至于文件数据,你可以尝试使用IsTextUnicode() ,但是已知这会报告错误的结果,所以你真的不应该依赖它。 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. 正如您所说,您的文件没有UTF-16编码,但您的Edit控件使用的是UTF-16。 You can use MultiByteToWideChar to convert UTF-8 to UTF-16. 您可以使用MultiByteToWideChar将UTF-8转换为UTF-16。

Note that use MultiByteToWideChar you need call 2 times, example: 注意使用MultiByteToWideChar需要调用2次,例如:

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 查看更多: 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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