.486p
.MODEL Flat, StdCall

locals
jumps

UNICODE = 0
include w32.inc   ;imports & co.

extrn DrawIcon:proc     ;not in w32.inc
extrn _wsprintfA:proc   ;too

;from .res (buttons,...)
IDD_KEY    equ 103     ;Dialog
IDI_WINAMP equ 105     ;Icon for WinAmp
IDI_KAC    equ 104     ;Icon for KAC (I'm from KAC)
IDC_EDIT1  equ 1000    ;the name
IDC_EDIT2  equ 1001    ;serial (output)

.data
fmat            db "%lu",0      ;for convering
No_Name         db 'You have to enter at least 1 letter!',0    ;nothing entered

.data?
Name_IN  db 80h dup (?)     ;Place for the name (winamp accept max. 80h chars)
the_key  db 10h dup (?)     ;place for the key
hInst    dd ?               ;handle for ma APP
Icon_KAC dd ?               ;handle for KAC icon
Icon_WA  dd ?               ;handle for WinAmp icon
_pnt     PAINTSTRUCT ?      ;need for draw the WinAmp icon
DlgRect          RECT ?     ;this and
DesktopRect      RECT ?     ;this is needed to centre the dialog

.CODE

GetKey Proc             ;This proc is from WinAmp. I used W32DSM to get it.....
; uses ebx, edi, esi
         push ebp                 ;\
         mov ebp, esp             ; > 260 Bytes reserved on Stack
         sub esp, 00000104h ;260  ;/
         xor eax, eax

         push ebx  ;\
         push esi  ; > Use
         push edi  ;/

         mov ecx, 0000003Fh                  ; 3F*4+2 = 254 (ZERO)
         xor esi, esi
         lea edi, dword ptr [ebp+0FFFFFEFDh] ;-259
         push dword ptr 00000100h
         mov byte ptr [ebp+0FFFFFEFCh], 00   ;-260
         repz stosd
         stosw
         lea ecx, dword ptr [ebp+0FFFFFEFCh] ;-260
         xor ebx, ebx
         stosb
         push Offset Name_IN  ; [ebp+08]
         push ecx
         call Call1 
         add esp, 0000000Ch
         lea ecx, dword ptr [ebp+0FFFFFEFCh]
         push ecx
         call Call2  
         add esp, 00000004h
         test eax, eax
         je J_0040B430 
J_0040B3CB:
         lea edi, dword ptr [ebp-03]
         xor eax, eax
         push dword ptr 00000004
         lea ecx, dword ptr [ebp-04]
         mov [ebp-04], al ;00
         stosw
         stosb
         lea eax, dword ptr [ebp+ebx-00000104h]
         push eax
         push ecx
         call Call1
         add esp, 0000000Ch
         mov al, byte ptr [ebp-01]
         xor al, byte ptr [ebp-03]
         movzx edx, al
         not edx
         mov al, byte ptr [ebp-02]
         xor al, byte ptr [ebp-04]
         movzx eax, al
         not eax
         add edx, eax
         mov eax, ebx
         and al, 07
         lea ecx, dword ptr [2*eax+00000000]
         shl edx, cl
         test edx, edx
         jl J_0040B41A 
         add esi, edx
         jmp J_0040B41C 
J_0040B41A:
         sub esi, edx
J_0040B41C:
         inc ebx
         lea eax, dword ptr [ebp+0FFFFFEFCh]
         push eax              
         call Call2
         add esp, 00000004     
         cmp eax, ebx          
         ja J_0040B3CB 
J_0040B430:
         imul esi, 00000025h
         mov ecx, 000F4240h
         mov eax, esi
         cdq
         idiv ecx
         mov edi, edx
         mov ecx, edx
         imul edi, 00000064h
         xor esi, esi
         test ecx, ecx
         je J_0040B461 
         mov ebx, 00000064h
J_0040B44F:
         mov eax, ecx
         cdq
         idiv ebx
         add esi, edx
         mov eax, ecx
         cdq
         idiv ebx
         mov ecx, eax
         test ecx, ecx
         jne J_0040B44F
J_0040B461:
         mov eax, esi
         mov ecx, 00000064h
         imul eax, 00000003h
         cdq
         and edx, 00000003h
         add eax, edx
         sar eax, 02h
         cdq
         idiv ecx
         lea eax, dword ptr [edi+edx]            ; correct serial

         pop edi    ;\
         pop esi    ; > Uses
         pop ebx    ;/

         mov esp, ebp
         pop ebp
         ret


GetKey EndP

Call1 Proc      ;also from WinAmp

         mov ecx, dword ptr [esp+0Ch]
         push edi
         test ecx, ecx
         je J_00426903
         push esi
         push ebx
         mov ebx, ecx
         mov esi, dword ptr [esp+14h]
         test esi, 00000003h
         mov edi, dword ptr [esp+10h]
         jne J_004268A4
         shr ecx, 02
         jne J_00426911
         jmp J_004268C5
J_004268A4:
         mov al, byte ptr [esi]
         inc esi
         mov byte ptr [edi], al
         inc edi
         dec ecx
         je J_004268D2
         test al, al
         je J_004268DA 
         test esi, 00000003
         jne J_004268A4
         mov ebx, ecx
         shr ecx, 02
         jne J_00426911
J_004268C0:
         and ebx, 00000003
         je J_004268D2
J_004268C5:
         mov al, byte ptr [esi]
         inc esi
         mov byte ptr [edi], al
         inc edi
         test al, al
         je J_004268FE 
         dec ebx
         jne J_004268C5
J_004268D2:
         mov eax, dword ptr [esp+10h]
         pop ebx
         pop esi
         pop edi
         ret

J_004268DA:
         test edi, 00000003
         je J_004268F4
J_004268E2:
         mov byte ptr [edi], al
         inc edi
         dec ecx
         je J_00426976
         test edi, 00000003
         jne J_004268E2
J_004268F4:
         mov ebx, ecx
         shr ecx, 02
         jne J_00426967
J_004268FB:
         mov byte ptr [edi], al
         inc edi
J_004268FE:
         dec ebx
         jne J_004268FB
         pop ebx
         pop esi

J_00426903:
         mov eax, dword ptr [esp+08h]
         pop edi
         ret

J_00426909:
         mov dword ptr [edi], edx
         add edi, 00000004
         dec ecx
         je J_004268C0
J_00426911:
         mov edx, 7EFEFEFFh
         mov eax, dword ptr [esi]
         add edx, eax
         xor eax, 0FFFFFFFFh
         xor eax, edx
         mov edx, dword ptr [esi]
         add esi, 00000004h
         test eax, 81010100h
         je J_00426909 
         test dl, dl
         je J_0042695B 
         test dh, dh
         je J_00426951 
         test edx, 00FF0000h
         je J_00426947 
         test edx, 0FF000000h
         jne J_00426909 
         mov dword ptr [edi], edx
         jmp J_0042695F
J_00426947:
         and edx, 0000FFFFh
         mov dword ptr [edi], edx
         jmp J_0042695F
J_00426951:
         and edx, 000000FFh
         mov dword ptr [edi], edx
         jmp J_0042695F
J_0042695B:
         xor edx, edx
         mov dword ptr [edi], edx
J_0042695F:
         add edi, 00000004
         xor eax, eax
         dec ecx
         je J_00426971
J_00426967:
         xor eax, eax
J_00426969:
         mov dword ptr [edi], eax
         add edi, 00000004
         dec ecx
         jne J_00426969
J_00426971:
         and ebx, 00000003
         jne J_004268FB
J_00426976:
         mov eax, dword ptr [esp+10]
         pop ebx
         pop esi
         pop edi
         ret

Call1 EndP

Call2 proc    ;Tha last one

         mov ecx, dword ptr [esp+04]
         test ecx, 00000003
         je J_00425FF0
J_00425FDC:
         mov al, byte ptr [ecx]
         inc ecx
         test al, al
         je J_00426023
         test ecx, 00000003
         jne J_00425FDC
         add eax, 00000000
J_00425FF0:    
         mov eax, dword ptr [ecx]
         mov edx, 7EFEFEFFh
         add edx, eax
         xor eax, 0FFFFFFFFh
         xor eax, edx
         add ecx, 00000004
         test eax, 81010100h
         je J_00425FF0
         mov eax, dword ptr [ecx-04]
         test al, al
         je J_00426041 
         test ah, ah
         je J_00426037 
         test eax, 00FF0000h
         je J_0042602D
         test eax, 0FF000000h
         je J_00426023
         jmp J_00425FF0
J_00426023:
         lea eax, dword ptr [ecx-01]
         mov ecx, dword ptr [esp+04]
         sub eax, ecx
         ret


J_0042602D:
         lea eax, dword ptr [ecx-02]
         mov ecx, dword ptr [esp+04]
         sub eax, ecx
         ret


J_00426037:
         lea eax, dword ptr [ecx-03]
         mov ecx, dword ptr [esp+04]
         sub eax, ecx
         ret


J_00426041:
         lea eax, dword ptr [ecx-04]
         mov ecx, dword ptr [esp+04]
         sub eax, ecx
         ret

Call2 EndP

main:
       call GetModuleHandle, NULL  ;get handle
       mov     hInst, eax         ;save

;       push    0                  
;       push    offset DlgProc     
;       push    0                  
;       push    IDD_KEY
;       push    hInst              
;       call    DialogBoxParamA    ;create the dialog
       call     DialogBoxParamA, hInst, IDD_KEY, 0, offset DlgProc, 0

       call    ExitProcess, 0  ;cya...

; PUBLIC DlgProc
DlgProc proc STDCALL, __hwnd:DWORD, wmsg:DWORD, _wparam:DWORD, _lparam:DWORD
    USES  ebx, edi, esi

 cmp wmsg, WM_DESTROY   ;leave
 je _wmdestroy                  

 cmp wmsg, WM_CLOSE   ;leave
 je   _wmdestroy

 cmp wmsg, WM_COMMAND  ;pressed something
 je     _wmcommand              

 cmp wmsg, WM_INITDIALOG ;init dialog (once)
 je     _initdlg

 cmp wmsg, WM_PAINT   ;repeaint the dialog
 je     _paint

xor eax,eax
ret                             

_wmdestroy:
push    0                       
push    __hwnd                  
call    EndDialog               
call    ExitProcess, 0 ;leave
ret

_wmcommand:
cmp     word ptr [_wparam], IDC_EDIT1 
jne     @Next                         
call    GenKey, __hwnd     ;if somebody entered something (changed
			; IDC_EDIT1) make the new serial
ret
@Next:
cmp     _wparam, IDOK                 
je      _wmdestroy                    
ret

_initdlg:

push    IDI_WINAMP                    
push    hInst                         
call    LoadIcon                      

mov     Icon_WA, eax                  

Call LoadIcon, hInst, IDI_KAC
mov Icon_KAC, eax

push    eax                           
push    IDI_KAC                       
push    WM_SETICON                    
push    __hwnd                        
call    SendMessage    ; Application Icon is IDI_KAC (u c it when u press alt+tab)               

   push OFFSET DlgRect                ;centre
   push __hwnd                        
   call GetWindowRect                 
   call GetDesktopWindow              
   push OFFSET DesktopRect            
   push eax                           
   call GetWindowRect                 
   push  0                            
   mov  eax,DlgRect.rc_bottom         
   sub  eax,DlgRect.rc_top            
   mov  edi, eax
   push eax                           
   mov  eax,DlgRect.rc_right          
   sub  eax,DlgRect.rc_left           
   mov esi, eax
   push eax                           
   mov  eax,DesktopRect.rc_bottom     
   sub  eax,edi 
   shr  eax,1                         
   push eax                           
   mov  eax,DesktopRect.rc_right      
   sub  eax, esi
   shr  eax,1
   push eax                           
   push  __hwnd                       
   call MoveWindow                    
ret 

_paint:

push    offset _pnt
push    __hwnd
call    BeginPaint                    

push    Icon_WA                       
push    75                            
push    10
push    eax                           
call    DrawIcon                      
	
push    offset _pnt                   
push	__hwnd
call	EndPaint
ret

DlgProc ENDP

GenKey proc _hDlg:dword
uses ebx, edx
        push    80h
        push    offset Name_IN
        push    IDC_EDIT1             
        push    _hDlg                 
        call    GetDlgItemTextA       ;Get The name
        test eax, eax     
        jne CalcKey      ;Nothing entered ?
        push offset No_Name
        jmp Name_Leer
CalcKey:
        Call GetKey   ;create the key
	
        push    eax                   ;hex to dec
        push    offset fmat           
        push    offset the_key        
        call    _wsprintfA            
	add esp, 4*3
        mov eax, offset the_key        ;print it
        push eax
Name_Leer:
        push    IDC_EDIT2             
        push    _hDlg                 
        call    SetDlgItemTextA       
        mov     eax, 1                
        ret                           
ExitProc0:
        xor     eax, eax              ;end that's all
        ret                           
GenKey endp

ends
end main
;->EOF<-
