首页
社区
课程
招聘
[求助]怎么定义一个UNICODE字符???
发表于: 2008-5-17 13:08 6327

[求助]怎么定义一个UNICODE字符???

2008-5-17 13:08
6327
怎么定义一个UNICODE字符???
网上搜索了好象MultiByteToWideChar函数可以转换成UNICODE,但是我始终转换不成功
invoke RtlZeroMemory,addr szFileName,MAX_PATH  
invoke GetWindowsDirectory,addr szFileName,MAX_PATH
invoke lstrcat,addr szFileName,CTEXT("\adds.exe")
invoke MultiByteToWideChar,CP_ACP,MB_PRECOMPOSED,addr szFileName,-1,0,0
mov ebx,eax
invoke GlobalAlloc,GMEM_FIXED or GMEM_ZEROINIT,ebx
mov esi,eax
invoke MultiByteToWideChar,CP_ACP,MB_PRECOMPOSED,addr szFileName,-1,esi,ebx
invoke MessageBox,0,esi,esi,MB_OK  ;报错,不知道为什么
invoke GlobalFree,esi

[培训]科锐逆向工程师培训第53期2025年7月8日开班!

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 424
活跃值: (2509)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
2
.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

include \masm32\macros\ucmacros.asm

.data
WSTR MyUnicodeString,"Unicode Test"

.code
start:
invoke MessageBoxW, NULL, offset MyUnicodeString, offset MyUnicodeString, MB_OK
invoke ExitProcess, NULL
end start


注意WSTR宏不支持中文……
2008-5-17 14:55
0
雪    币: 120
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
那这个MultiByteToWideChar函数用法哪里错了呢
2008-5-17 17:12
0
雪    币: 722
活跃值: (123)
能力值: ( LV12,RANK:300 )
在线值:
发帖
回帖
粉丝
4
仔细看MultiByteToWideChar函数的MSDN说明:

int MultiByteToWideChar(
  UINT CodePage,         // code page
  DWORD dwFlags,         // character-type options
  LPCSTR lpMultiByteStr, // string to map
  int cbMultiByte,       // number of bytes in string
  LPWSTR lpWideCharStr,  // wide-character buffer
  int cchWideChar        // size of buffer
);

……
cchWideChar
[in] Specifies the size, in wide characters, of the buffer pointed to by the lpWideCharStr parameter. If this value is zero, the function returns the required buffer size, in wide characters, and makes no use of the lpWideCharStr buffer.

看了红字部分就应该明白了吧,第一次MultiByteToWideChar返回的所需字符串长度,是以宽字符为单位的,而不是以字节为单位的。也就是说,实际需要的内存空间大小,是返回值的两倍。

所以应该这样改:
……
invoke MultiByteToWideChar,CP_ACP,MB_PRECOMPOSED,addr szFileName,-1,0,0
mov ebx,eax
shl ebx,1;ebx乘2,变为实际需要的内存空间大小
invoke GlobalAlloc,GMEM_FIXED or GMEM_ZEROINIT,ebx
……
另外,既然是UNICODE字符了,就应该用MessageBoxW,而不是MessageBox(实际被默认为MessageBoxA)
2008-5-18 16:17
0
游客
登录 | 注册 方可回帖
返回