flat assembler
Message board for the users of flat assembler.

Index > Windows > wsprintf seems to be grabbing random data?

Author
Thread Post new topic Reply to topic
himtay



Joined: 03 May 2005
Posts: 4
himtay
Hi Smile



I am trying to log messages to file with an accurate date and time. The first message written to file always works fine. How-ever the second message seems to insert the message in to the time part of the message.

Is some one able to have a look for me to please to see what I am being a plonker about?


Note1: stLogPath will need to have a valid path on your PC if you wish to run.
Note2: To easily debug where the information is going I have three message boxes per log. first: paramter for the proc; second: date string after set; third: time string after set (where it goes wrong.)


Code follows:
Code:
format PE GUI 4.0
entry start

include '%fasminc%\win32a.inc'

section '.data' data readable writeable


  _class db 'BSServer',0

  hinstance dd ?

  ;log messages
  lmStarted             db         'Server Initiating',13,10,0
  lmStartComplete       db         'Server Startup Completed',13,10,0

  stLogPath db 'C:\dump\Home\ServerLog.txt',0
  err_cap   db 'error',0
  openerr   db 'error',0
  szReadWrite  dd      ?
  hLogFile dd ?
  systemTime  SYSTEMTIME
  _format_date     db '[%02d/%02d/%4d  ',0
  _buffer_date     rb 30                                        
  _format_time     db '%02d:%02d:%02d.%03d] ',0
  _buffer_time     rb 15


  msg MSG
  wc WNDCLASS

section '.code' code readable executable

  start:
        invoke  GetModuleHandle,0
        mov     [hinstance],eax
        invoke  LoadIcon,0,IDI_APPLICATION
        mov     [wc.hIcon],eax
        invoke  LoadCursor,0,IDC_ARROW
        stdcall logToFile,lmStarted
        stdcall logToFile,lmStarted
        mov     [wc.hCursor],eax
        mov     [wc.style],CS_NOCLOSE
        mov     [wc.lpfnWndProc],WindowProc
        mov     [wc.cbClsExtra],0
        mov     [wc.cbWndExtra],0
        mov     eax,[hinstance]
        mov     [wc.hInstance],eax
        mov     [wc.hbrBackground],COLOR_BTNFACE+1
        mov     [wc.lpszMenuName],0
        mov     [wc.lpszClassName],_class
        invoke  RegisterClass,wc
 ;       stdcall logToFile,stStartComplete
        invoke  PostQuitMessage

  msg_loop:
        invoke  GetMessage,msg,NULL,0,0
        or      eax,eax
        jz      end_loop
        invoke  TranslateMessage,msg
        invoke  DispatchMessage,msg
        jmp     msg_loop

  end_loop:
        invoke  ExitProcess,[msg.wParam]

proc WindowProc, hwnd,wmsg,wparam,lparam
        push    ebx esi edi
        cmp     [wmsg],WM_DESTROY
        je      wmdestroy
  defwndproc:
        invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
        jmp     finish
  wmdestroy:
        invoke  PostQuitMessage,0
        xor     eax,eax
  finish:
        pop     edi esi ebx
        ret
endp

proc logToFile,pbuffer
        pusha
        invoke MessageBox,NULL,[pbuffer],openerr,0

        invoke GetSystemTime,systemTime
        movsx   eax,[systemTime.wDay]
        movsx   ebx,[systemTime.wMonth]
        movsx   edi,[systemTime.wYear]
        cinvoke wsprintf,_buffer_date,_format_date,eax,ebx,edi
        invoke MessageBox,NULL,_buffer_date,openerr,0
        movsx   eax,[systemTime.wHour]
        movsx   ebx,[systemTime.wMinute]
        movsx   edi,[systemTime.wSecond]
        movsx   edx,[systemTime.wMilliseconds]
        cinvoke wsprintf,_buffer_time,_format_time,eax,ebx,edi,edx
        invoke MessageBox,NULL,_buffer_time,openerr,0

        ;Put the text all together
        invoke lstrcat,_buffer_date,_buffer_time
        invoke lstrcat,_buffer_date,[pbuffer]

        ;Open the file
        invoke CreateFileA, stLogPath, GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_ALWAYS, 080h, 0
        mov [hLogFile],eax
        invoke SetFilePointer,[hLogFile],0,0,FILE_END
        cmp eax, 0FFFFFFFFh
        jz error
        invoke lstrlen,_buffer_date
        invoke WriteFile, [hLogFile], _buffer_date, eax, szReadWrite, 0
        invoke CloseHandle,dword [hLogFile]
        popa
        ret
      error:
        invoke MessageBox,NULL,err_cap,openerr,0
        popa
        ret

endp

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
          user,'USER32.DLL'

  import kernel,\
         GetModuleHandle,'GetModuleHandleA',\
         ExitProcess,'ExitProcess',\
         CreateFileA,'CreateFileA',\
         WriteFile,'WriteFile',\
         CloseHandle,'CloseHandle',\
         GetLastError,'GetLastError',\
         lstrlen,'lstrlenA',\
         GlobalAlloc,'GlobalAlloc',\
         GetFileSize,'GetFileSize',\
         SetFilePointer,'SetFilePointer',\
         GetSystemTime,'GetSystemTime',\
         lstrcat,'lstrcatA'

  import user,\
         RegisterClass,'RegisterClassA',\
         CreateWindowEx,'CreateWindowExA',\
         DefWindowProc,'DefWindowProcA',\
         GetMessage,'GetMessageA',\
         TranslateMessage,'TranslateMessage',\
         DispatchMessage,'DispatchMessageA',\
         LoadCursor,'LoadCursorA',\
         LoadIcon,'LoadIconA',\
         PostQuitMessage,'PostQuitMessage',\
         MessageBox,'MessageBoxA',\
         wsprintf,'wsprintfA'      
Post 17 Jul 2005, 04:47
View user's profile Send private message MSN Messenger Reply with quote
himtay



Joined: 03 May 2005
Posts: 4
himtay
Hi me,


The problem was because I set _buffer_date too low at 30. I have now changed this to 128 and it works perfectly.


Thanks me
Post 19 Jul 2005, 00:58
View user's profile Send private message MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.