News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Display system time

Started by majid1605, December 20, 2011, 07:11:14 PM

Previous topic - Next topic

majid1605

How do I display  the system time in the message box

.486
.model flat,stdcall
option casemap:none
 
     
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD
 
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\msvcrt.inc ;crt__ultoa
include     \masm32\macros\macros.asm


includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\kernel32.lib
includelib \masm32\lib\msvcrt.lib  ;crt__ultoa


     
.data
ClassName db "SimpleWinClass",0
AppName  db "Local Time",0


MyString      DB 20 dup(' '),0

LTimeis     db "LocalTime is: "
HTime       DB   2 dup (' ')
SymTime     db ':'
STime       DB   2 dup (' ')
            db 0     


.data?

hInstance HINSTANCE ?
CommandLine LPSTR ?


width_w       dd ?
height_w   dd ?

stimestruct STRUCT     
       wYear            WORD ?   
       wMonth           WORD ?
       wDayOfWeek       WORD ?
       wDay             WORD ?
       wHour            WORD ?
       wMinute          WORD ?
       wSecond          WORD ?
       wMilliseconds    WORD ?
    stimestruct EndS
       
    stime stimestruct {}

; example of simplified Windows programming using complex macro features



.code

  start:

LTimep proc
    lea ebx,stime
    push ebx
    call GetLocalTime
     
    xor eax,eax
    mov al,byte ptr [ebx].stimestruct.wHour
    invoke crt__ultoa,eax,ADDR MyString,10

    mov ax,word ptr MyString
    mov word ptr HTime,ax
    cmp HTime+1,0
    jne next
    mov al,HTime
    xchg HTime+1,al
    mov HTime,' '
next: 
     
    xor eax,eax
    mov  al,byte ptr [ebx].stimestruct.wMinute
    invoke crt__ultoa,eax,ADDR MyString,10
     
     
    mov ax,word ptr MyString
    mov word ptr STime,ax
    cmp STime+1,0
    jne next2
    mov STime,al
next2:


    ret
LTimep endp   

invoke MessageBox,HWND_DESKTOP,ADDR LTimep,invoke GetCommandLine,MB_OK
invoke ExitProcess,0

.end start



like the following code But in the message box

.486
.model flat,stdcall
option casemap:none
 
     
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD
 
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\msvcrt.inc ;crt__ultoa
include     \masm32\macros\macros.asm


includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\kernel32.lib
includelib \masm32\lib\msvcrt.lib  ;crt__ultoa


     
     
.data
ClassName db "SimpleWinClass",0
AppName  db "Local Time",0


MyString      DB 20 dup(' '),0

LTimeis     db "LocalTime is: "
HTime       DB   2 dup (' ')
SymTime     db ':'
STime       DB   2 dup (' ')
            db 0     


.data?

hInstance HINSTANCE ?
CommandLine LPSTR ?


width_w       dd ?
height_w   dd ?

stimestruct STRUCT     
       wYear            WORD ?   
       wMonth           WORD ?
       wDayOfWeek       WORD ?
       wDay             WORD ?
       wHour            WORD ?
       wMinute          WORD ?
       wSecond          WORD ?
       wMilliseconds    WORD ?
    stimestruct EndS
       
    stime stimestruct {}
 
 
     

.code
start:
    invoke GetModuleHandle, NULL
    mov    hInstance,eax
    invoke GetCommandLine
    mov CommandLine,eax
    invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
    invoke ExitProcess,eax
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,  CmdShow:DWORD
    LOCAL wc:WNDCLASSEX
    LOCAL msg:MSG
    LOCAL hwnd:HWND
    mov   wc.cbSize,SIZEOF WNDCLASSEX
    mov   wc.style, CS_HREDRAW or CS_VREDRAW
    mov   wc.lpfnWndProc, OFFSET WndProc
    mov   wc.cbClsExtra,NULL
    mov   wc.cbWndExtra,NULL
    push  hInst
    pop   wc.hInstance
    mov   wc.hbrBackground,COLOR_WINDOW+1
    mov   wc.lpszMenuName,NULL
    mov   wc.lpszClassName,OFFSET ClassName
    invoke LoadIcon,NULL,IDI_APPLICATION
    mov   wc.hIcon,eax
    mov   wc.hIconSm,0
    invoke LoadCursor,NULL,IDC_ARROW
    mov   wc.hCursor,eax
mov     width_w,200
mov   height_w,300
    invoke RegisterClassEx, addr wc
    INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
           WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\
           CW_USEDEFAULT,width_w,height_w,NULL,NULL,\
           hInst,NULL
    mov   hwnd,eax
    INVOKE ShowWindow, hwnd,SW_SHOWNORMAL
    INVOKE UpdateWindow, hwnd
    .WHILE TRUE
                INVOKE GetMessage, ADDR msg,NULL,0,0
                .BREAK .IF (!eax)
                INVOKE TranslateMessage, ADDR msg
                INVOKE DispatchMessage, ADDR msg
    .ENDW
    mov     eax,msg.wParam
    ret
WinMain endp
WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
    LOCAL hdc:HDC
    LOCAL ps:PAINTSTRUCT
    LOCAL rect:RECT
    .IF uMsg==WM_DESTROY
        invoke PostQuitMessage,NULL
    .ELSEIF uMsg==WM_PAINT
        invoke BeginPaint,hWnd, ADDR ps
        mov    hdc,eax
        invoke GetClientRect,hWnd, ADDR rect
        call    LTimep
        invoke DrawText, hdc,ADDR LTimeis,-1, ADDR rect, DT_SINGLELINE or DT_CENTER or DT_VCENTER
        invoke EndPaint,hWnd, ADDR ps
    .ELSE     
        invoke DefWindowProc,hWnd,uMsg,wParam,lParam
        ret
    .ENDIF       
    xor    eax,eax
    ret
WndProc endp

LTimep proc
    lea ebx,stime
    push ebx
    call GetLocalTime
     
    xor eax,eax
    mov al,byte ptr [ebx].stimestruct.wHour
    invoke crt__ultoa,eax,ADDR MyString,10

    mov ax,word ptr MyString
    mov word ptr HTime,ax
    cmp HTime+1,0
    jne next
    mov al,HTime
    xchg HTime+1,al
    mov HTime,' '
next: 
     
    xor eax,eax
    mov  al,byte ptr [ebx].stimestruct.wMinute
    invoke crt__ultoa,eax,ADDR MyString,10
     
     
    mov ax,word ptr MyString
    mov word ptr STime,ax
    cmp STime+1,0
    jne next2
    mov STime,al
next2:


    ret
LTimep endp   
end start


MichaelW

To get the system time you need to call the GetSystemTime function. And to display your string in a message box you need to call the MessageBox function, passing the address of the string in the lpText parameter.
eschew obfuscation

bomz

.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\user32.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\user32.lib
includelib \MASM32\LIB\kernel32.lib

.data
mestitle db "TIME",0
TimeHeader db '00-00-00-00-00-000',0

.data?
mytime SYSTEMTIME <>

.code
start:
invoke GetLocalTime, addr mytime
lea edi, TimeHeader+3
lea esi, mytime+6
mov ebx, 10
xor eax, eax

mov ax, word ptr[mytime+2]
div bl
add ax, 3030h
mov word ptr[TimeHeader], ax

mov ecx, 4
@@:
mov ax, [esi]
add esi, 2
div bl
add ax, 3030h
mov [edi], ax
add edi, 3
sub ecx, 1
jnz @B

mov ax, [esi]
div bl
add ah, 30h
mov [edi], ah
add edi, 1
xor ah, ah
div bl
add ax, 3030h
mov [edi], ax

invoke MessageBox,0,ADDR TimeHeader,ADDR mestitle,MB_ICONASTERISK
invoke ExitProcess,0
end start

donkey

You can use GetTimeFormat if you want to avoid the extra step of converting to a string. Using NULL in place of the lpTime parameter will use the current time...

szTimeFormat DB "hh:mm tt",0 ; you can change this to whatever format you need
szTimeOut DB 64 DUP (?)

invoke GetTimeFormat,LOCALE_SYSTEM_DEFAULT,NULL,NULL,OFFSET szTimeFormat ,OFFSET szTimeOut,64


No need to get or translate the system time and it outputs in string format.

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

bomz

Quote.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\DateTime.inc
include \MASM32\INCLUDE\user32.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\DateTime.lib
includelib \MASM32\LIB\user32.lib
includelib \MASM32\LIB\kernel32.lib

.data
mestitle db "TIME",0

.data?
buffer db 512 dup(?)
mytime SYSTEMTIME <>

.code
start:

invoke GetLocalTime, addr mytime
INVOKE DateTimeToDateString, addr mytime, addr buffer
INVOKE DateTimeToDateStringLong, addr mytime, addr buffer
invoke MessageBox,0,ADDR buffer,ADDR mestitle,MB_ICONASTERISK
invoke ExitProcess,0
end start
With year something strange. DateTime struc need ?

donkey

BTW for the date you use GetDateFormat:

szDateFormat DB "dd MMM yyyy",0
szDateOut DB 64 DUP (?)

invoke GetDateFormat,LOCALE_SYSTEM_DEFAULT,NULL,NULL,OFFSET szDateFormat,OFFSET szDateOut,64


Current date direct to formatted string.

Putting them together:

szTimeFormat DB "hh:mm tt",0
szDateFormat DB "dd MMM yyyy, ",0
szDateOut DB 64 DUP (?)

invoke GetDateFormat,LOCALE_SYSTEM_DEFAULT,NULL,NULL,OFFSET szDateFormat,OFFSET szDateOut,64
invoke lstrlen,offset szDateOut
add eax,offset szDateOut
invoke GetTimeFormat,LOCALE_SYSTEM_DEFAULT,NULL,NULL,OFFSET szTimeFormat ,eax,64


The output is:

21 Dec 2011, 01:06 AM
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

MichaelW

Edgar,

Passing NULL in the GetTimeFormat lpTime parameter will cause the function to use the current local time, not the system (UTC) time.


;==============================================================================
; BUILD AS CONSOLE APP.
;==============================================================================
include \masm32\include\masm32rt.inc
;==============================================================================

;-----------------------------------------------------
; This currently missing from the MASM32 windows.inc:
;-----------------------------------------------------

LOCALE_SYSTEM_DEFAULT equ 0800h

;==============================================================================
    .data
        systemtime SYSTEMTIME <>
        buffer     db 40 dup(0)
    .code
;==============================================================================
start:
;==============================================================================
    invoke GetLocalTime, ADDR systemtime

    invoke GetTimeFormat, LOCALE_SYSTEM_DEFAULT,
                          TIME_FORCE24HOURFORMAT,
                          ADDR systemtime,
                          chr$("hh:mm:ss"),
                          ADDR buffer,
                          40

    invoke MessageBox, 0, ADDR buffer, chr$("Local Time"), 0

    invoke GetTimeFormat, LOCALE_SYSTEM_DEFAULT,
                          TIME_FORCE24HOURFORMAT,
                          0,
                          chr$("hh:mm:ss"),
                          ADDR buffer,
                          40
    invoke MessageBox, 0, ADDR buffer, chr$("Local Time"), 0


    invoke GetSystemTime, ADDR systemtime

    invoke GetTimeFormat, LOCALE_SYSTEM_DEFAULT,
                          TIME_FORCE24HOURFORMAT,
                          ADDR systemtime,
                          chr$("hh:mm:ss"),
                          ADDR buffer,
                          40
                         
    invoke MessageBox, 0, ADDR buffer, chr$("System Time (UTC)"), 0
    exit
;==============================================================================
end start


Then again, maybe I'm using the wrong interpretation of "system time".
eschew obfuscation

majid1605

Thank you friends
But i want use the following code

LTimep proc
    lea ebx,stime
    push ebx
    call GetLocalTime
     
    xor eax,eax
    mov al,byte ptr [ebx].stimestruct.wHour
    invoke crt__ultoa,eax,ADDR MyString,10

    mov ax,word ptr MyString
    mov word ptr HTime,ax
    cmp HTime+1,0
    jne next
    mov al,HTime
    xchg HTime+1,al
    mov HTime,' '
next:
     
    xor eax,eax
    mov  al,byte ptr [ebx].stimestruct.wMinute
    invoke crt__ultoa,eax,ADDR MyString,10
     
     
    mov ax,word ptr MyString
    mov word ptr STime,ax
    cmp STime+1,0
    jne next2
    mov STime,al
next2:


    ret
LTimep endp   

dedndave

is that the complete code ?
show us the data declarations, please

you seem to have 2 different names for stime/stimestruct
also, if you are going to use EBX, it is standard practice to preserve it across the call

GregL

Bomz,

If you want to use the DateTime library, you must use DateTime variables for the date and time. There is a help file in masm32\help and the source code is provided.

bomz

Quote.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\DateTime.inc
include \MASM32\INCLUDE\user32.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\DateTime.lib
includelib \MASM32\LIB\user32.lib
includelib \MASM32\LIB\kernel32.lib

.data
mestitle db "TIME",0

.data?
buffer db 512 dup(?)
mytime SYSTEMTIME <>

.code
start:

INVOKE GetLocalDateTime, addr mytime
INVOKE DateTimeToDateString, addr mytime, addr buffer
;INVOKE DateTimeToDateStringLong, addr mytime, addr buffer
invoke MessageBox,0,ADDR buffer,ADDR mestitle,MB_ICONASTERISK
invoke ExitProcess,0
end start

GregL

Bomz,

mytime should be a DateTime variable. It just happens a SYSTEMTIME is larger than a DateTime, so the program works.

bomz

Quote.386
.model flat, stdcall
option casemap:none
include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\kernel32.lib
include \MASM32\INCLUDE\user32.inc
includelib \MASM32\LIB\user32.lib

.data
MsgCaption      db "TIME",0
form      db '%02hu-%02hu-%02hu-%02hu-%02hu-%03hu',0
mytime      SYSTEMTIME <>

.data?
Buffer      db 1024 dup (?)

.code
start:
   invoke GetSystemTime, addr mytime
   xor eax, eax
   mov ax, mytime.wMilliseconds
   movzx ebx, mytime.wSecond
   movzx ecx, mytime.wMinute
   movzx edx, mytime.wHour
   movzx edi, mytime.wDay
   movzx esi, mytime.wMonth
   invoke wsprintf,addr Buffer,addr form, esi, edi, edx, ecx, ebx, eax
   invoke MessageBox, NULL, addr Buffer, addr MsgCaption, MB_ICONASTERISK

   invoke ExitProcess,NULL
end start

majid1605

please tell me how use Macro for color and font size?

include \masm32\include\masm32rt.inc
LOCALE_SYSTEM_DEFAULT equ 0800h
RGB macro red,green,blue
        xor eax,eax
        mov ah,blue
        shl eax,8
        mov ah,green
        mov al,red
endm
    .data
        systemtime SYSTEMTIME <>
        buffer     db 40 dup(0)
    .code
start:
    invoke GetLocalTime, ADDR systemtime
    invoke GetTimeFormat, LOCALE_SYSTEM_DEFAULT,
                          TIME_FORCE24HOURFORMAT,
                          ADDR systemtime,
                          chr$("hh:mm:ss"),
                          ADDR buffer,
                          40
    invoke MessageBox, 0, ADDR buffer, chr$("System Time (UTC)"), 0
    exit
end start

dedndave

with a message box, you cannot select font size or text colors
you will have to make a custom dialog box to do that

it is not necessary to put the RGB macro in your pogram
it is included with this line
include \masm32\include\masm32rt.inc
because that include file adds the macros.asm file
also, the RGB function can be better performed by simply adding values together - without generating any code