News:

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

lstrcmp not expected result

Started by Nilrem, February 10, 2005, 01:32:33 PM

Previous topic - Next topic

Nilrem

I am not getting the expected result (in and out of Olly) for lstrcmp, for some reason one of them is empty (""):

    .data
    FileName db ".\ID.txt",0
    RootPathName dw 128 dup(0)
    VolumeNameBuffer        db 128 dup(0)
    nVolumeNameSize         dd 128
    VolumeSerialNumber      dd 0
    MaximumComponentLength  dd 0
    FileSystemFlags         dd 0
    FileSystemNameBuffer    db 128 dup(0)
    nFileSystemNameSize     dd 128
    DriveBufferSize dw 128(0)
    TempName db "C:\",0
    PasswordGen dd 128 dup(0)
    PasswordEntered dd 128 dup(0)
    UserNameEntered dd 128 dup(0)

Begin PROC
LOCAL lpstring[128]:DWORD

invoke CreateFile,ADDR FileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
invoke GetLastError
.if eax != 0
invoke CreateFile,ADDR FileName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL
print chr$("This is your first time running the program.")
print chr$(13,10,"It was written by Nilrem. ")
mov lpstring, _input (13,10,"Hit enter to begin.")
cls
.endif
call ID
exit
Begin endp

ID PROC

LOCAL lpstring[128]:DWORD
LOCAL FindName[128]:DWORD
LOCAL FindNameSize[128]:DWORD
invoke GetLogicalDriveStrings, ADDR FindNameSize, ADDR FindName
again:

    invoke GetVolumeInformation, ADDR TempName,
                                ADDR VolumeNameBuffer,
                                nVolumeNameSize,
                                ADDR VolumeSerialNumber,
                                ADDR MaximumComponentLength,
                                ADDR FileSystemFlags,
                                ADDR FileSystemNameBuffer,
                                nFileSystemNameSize
  print chr$(13,10,"RootPathName: ")
    print ADDR TempName
    print chr$(13,10,"VolumeSerialNumber (hex): ")
    print uhex$(VolumeSerialNumber)
    mov UserNameEntered, _input (13,10,"UserName: ")
    call PasswordGeneration; Generate the password

PasswordJump:
mov PasswordEntered, _input ("Password: ")
invoke lstrcmp, ADDR UserNameEntered, ADDR PasswordEntered
;print hex$(PasswordGen)
TEST eax, eax
JNZ @f
print chr$(13,10,"That was correct")
ret
@@:
print chr$(13,10,"That was incorrect")
@@:
mov lpstring, _input(13,10,"Try Again? Y/N: ")

xor eax,eax
mov eax,[lpstring]
.if BYTE PTR [eax] == 'Y' || BYTE PTR [eax] == 'y'
jmp PasswordJump
.elseif BYTE PTR [eax] == 'N' || BYTE PTR [eax] == 'n'
ret
.else
print chr$(13,10,"Invalid input",13,10)
jmp @b
.endif
ret

ID endp

PasswordGeneration PROC

invoke lstrcat, ADDR UserNameEntered, ADDR VolumeSerialNumber
;invoke lstrcat, ADDR PasswordGen, ADDR UserNameEntered
print hex$(UserNameEntered)
ret

PasswordGeneration endp

end start
Quite a lot of code to digest but basically you enter your username, it then adds your username value to the value of your volumeserialnumber, (lstrcat), then you enter your password and this is compare (lstrcmp) to the password, it then figures out if what you entered was correct. Thanks in advance. I know I ask a lot of awful questions, but really it is helping my learn quick.

Nilrem

I have edited a portion of my code (the lstrcmp bit)

PasswordJump:
mov PasswordEntered, _input ("Password: ")
;invoke lstrcmp, ADDR UserNameEntered, ADDR PasswordEntered
;print hex$(PasswordGen)
xor eax,eax
xor edx,edx
mov eax, UserNameEntered
mov edx, PasswordEntered
cmp eax, edx
JNE @f
print chr$(13,10,"That was correct")
ret
Using this I find it easier to monitor in a debugger, I believe the problem lies with the 'mov Buffer, _input' command, I believe this to be the problem because when using lstrcat then moving the value to eax, eax actually holds the password, but when moving the 'buffer,input' to edx it moves some kind of reference to edx where the entered password is stored. Hope this makes sense, it does to me but I am unsure on how to handle it, should I perhaps use 'StdIn' instead?

MichaelW


int lstrcmp(
    LPCTSTR lpString1,
    LPCTSTR lpString2
);

lstrcmp expects each of the parameters to be a pointer to a string. The input macro returns a pointer to a string. Your code is storing pointers to strings in UserNameEntered and PasswordEntered, so:

invoke lstrcmp, ADDR UserNameEntered, ADDR PasswordEntered

Should actually be:

invoke lstrcmp, UserNameEntered, PasswordEntered

It would be easier for you to see these sorts of errors if you would use variable and parameter names that adequately describe the variable or parameter. For example, UserNameEntered and PasswordEntered are both storing pointers so more suitable names would be lpUserNameEntered and lpPasswordEntered. The naming conventions you see used in the API documentation were developed specifically to reduce coding errors.
eschew obfuscation

Nilrem

I will definitely take this into consideration instead of wasting time, thanks and sorry.