[mASM] WriteFile's output has unicode in it? How can I stop it from doing this

Started by Dale, November 28, 2009, 10:14:25 PM

Previous topic - Next topic

Dale

Hi guys, so I am having trouble with unicode in my text file.

I start with Creating a new file (CreateFile) getting the handle, getting its length, then writing to the file.

Then I..



;invoke szMultiCat here
;CreateFile here

push offset writedata
call lstrlen

push 0
push offset bytesWRT ;bytesWRT dd ?
push eax ;strlen
push offset writedata ;writedata db 260 dUP(0)
push hFile ;handle to my file that i got from createfile
call WriteFile


And then when I call writefile with the length of eax.  (lstrlen) Now it writes what it is suppose to write, but the problem is it writes an extra line with unicode in it.

PÿKv?????‹ÿU‹ì·À¥Wv‹U ?HÒ;Ñ,#þÿV‹uPÿ5Ä¥WvVè›A

How do I make it write ONLY what it is suppose to write?  I do not want the above in my text file.  I have probably made an error somewhere, but I am puzzled and do not know what I have done wrong.

MichaelW

Judging from the little code you posted, Write file should not be writing anything to the file, because the first byte of your string is zero so lstrlen should be returning zero.
eschew obfuscation

hutch--

Dale,

When you create a new file it has ZERO length.

This is a basic file creation cqall.


      invoke CreateFile,lpFileName,GENERIC_READ or GENERIC_WRITE,
                        NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
    mov hFile, eax  ; get the file handle


Use Writefile to write data to the open file.

Use SetFilePointer if you need to change WHERE in the file you are writing data. If your file is sequential the pointer is automatically updated after each write.

When you have finised, use CloseHandle on the file handle.

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

UtillMasm

 :8)my output 'widechar' to 'utf8(no bom)':

_ProcessFile proc _lpszFile
  local @hFile
   ;004010FC
    ;push ebp
    ;mov ebp,esp
    ;add esp,-4
  ;处理找到的文件
  inc dwFileCount
   ;00401102
    ;inc dword ptr ds:[403010]
   ;文件数计数器
  invoke SetDlgItemTextW,hWinMain,IDC_NOWFILE,_lpszFile
   ;不要使用"OllyDRX Ultimate 1.10"的"F8"等它返回。
  invoke CreateFileW,_lpszFile,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
   ;取文件句柄。
  .if eax!=INVALID_HANDLE_VALUE
   ;00401132
    ;cmp eax,-1
    ;je short 00401157
     ;前一句中的"cmp"结果相等则跳。
   ;句柄是否有效。
   mov @hFile,eax
    ;00401137
    ;有效
   invoke GetFileSize,eax,NULL
    ;取文件字节数。
   add dwFileSizeLow,eax
   adc dwFileSizeHigh,0
   invoke CloseHandle,@hFile
  .endif
  invoke lstrlenW,_lpszFile
   ;00401157
   ;求字符数(是不包括结尾"NULL"字符的)
  invoke WideCharToMultiByte,CP_UTF8,0,_lpszFile,eax,addr buffer,sizeof buffer,NULL,NULL
  mov stringLength,eax
   ;字节数(不包括结尾"NULL")
  invoke CreateFileW,addr resultFileName,GENERIC_READ or GENERIC_WRITE,NULL,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
  mov hFileWrite,eax
   ;文件句柄。
  invoke GetFileSize,eax,NULL
  mov resultFileSize,eax
   ;旧文件长度。
  add eax,stringLength
   ;要添加内容长度。
  add eax,2
   ;每行尾都放的"0Dh, 0Ah"字符的长度。
  invoke SetFilePointer,hFileWrite,eax,NULL,FILE_BEGIN
  invoke SetEndOfFile,hFileWrite
  invoke SetFilePointer,hFileWrite,resultFileSize,NULL,FILE_BEGIN
  invoke WriteFile,hFileWrite,addr buffer,stringLength,addr hasWriteBytes,NULL
  invoke WriteFile,hFileWrite,addr CRLF,2,addr hasWriteBytes,NULL
  invoke CloseHandle,hFileWrite
  ret
_ProcessFile endp

sorry for no time to delete these windows-936 ansi characters.

hutch--

UtillMasm,

No problems and it looks good. Unicode big 5 if I am correct.

One question, what do you use to write unicode for this character set ?

I keep the east asian fonts loaded so I can display Japanese and Chinese.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

WryBugz

Hutch, there are a number of chinese input editors. I use NJSTAR from http://www/njstar.com.au
It is shareware but there is no nag attached.
Comes in two ways - a line editor called 'NJStar Communicator' that works like a statusbar that inputs to whatever program has the focus. It works with qeditor. Does chinese, english and korean.
Another option is to download the full editor from the same site. The editor has a good dictionary. Write, cut, paste.
There are several input methods. Pinyin is favoured but it requires you know the language.
There is an english input but only trust it for for the most simple phrases.
Play with it. It is fun.
You can learn a couple hundred words in not time.
If you are thinking to write a chinese interface, I would recommend getting a chinese speaker to do it for you.
Both programs also do a lot of the unicode wingdings/symbols. Works easier than most char maps.

UtillMasm


hutch--

I already have an editor called WAKAN that can produce eiter Japanese or Chinese characters from a large collection of dictionaries with the option of using a US English keyboard. With Japanese you get Hiragano phonetics and a list of Kanji under it so you can pick which is closest to what you are after. In Japanese the hardest part is comprehending the particles.

I gather wth UtillMasm's answer that he uses a method something like IME to type chinese characters into EditPlus.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php