Skip to content
⌘ NSIS Forum Archive

I dont understand why my if statement is failing

22 posts

shaunb#

I dont understand why my if statement is failing

I just dont get it anymore, my text file written from the CF script merely produces 'failed' or 'success' but the if comparison must fail, because the messagebox never gets shown. I thought it might be an "\r\n" on the end of the string in the text file, but its not.

I have no idea why its happening any more, and also looping around this custom page is making me combust.

thanks


Function exitGetCFPassPage
!insertmacro MUI_INSTALLOPTIONS_READ $CFPASS "getCFPass.ini" "Field 2" "State"
MessageBox MB_OK "$$CFPASS == $CFPASS"
# check its not empty at least
#${If} $CFPASS == ""
# # empty password, prompt for a valid password again
# MessageBox MB_ICONEXCLAMATION|MB_OK "Please enter a valid ColdFusion admin password."
# abort
#${endIf}
# we forget all the items are extracted here already due to this page coming after the instfiles page
# we can just perform a simple wget on the testlogin script, and read the contents of c:\adminapi.log
# to see if it produced true or false for a valid login, then we can validate in here until we get a
# valid password!
NSExec::exec "$TEMP\${gossTemp}\wget\wget.exe http://localhost/icm/adminAPI/testlogin.cfm?password=$CFPASS"
# this returns a file c:\adminapi.log with true or false in, which we can use
# read the file, decide what to do etc, what about exceptions if its not there?
FileOpen $4 "c:\adminapi.log" r
# we read until the end of line (including carriage return and new line) and save it to $1
FileRead $4 $1
# close the file
FileClose $4
# debug the result
MessageBox MB_OK $1
${If} $1 == "failed"
MessageBox MB_OK "failed login!"
abort
${endIf}
# then it carries on anyway, why? it should abort
FunctionEnd
Red Wine#
some quick notes:
-stop the execution before open adminapi.log
-verify that this file exists, is accessible and readble
-you said it returns true or false, though you're checking for failed!
-when you pop up that message with $1, does $1 contains "failed"?
-finally try the nsExec::ExecToLog and/or nsExec::ExecToStack
shaunb#
I believe its something to do with a carriage return, on the end of the text in the file I created with CF, and trying to read in NSIS.

hah, it says failed because I've been playing with it to get it to check right, tried 0 or 1, etc, nothing works.

by stopping the execution I dont get how you do that exactly.

the file exists for sure, and is readable etc,

yes, the messagebox just contains success/failed, true or false etc, theres no extra text after (except maybe a carriage return I can't see), so this is why I dont get why the if statement underneath would fail.

I am trying to work out the carriage return character so basically i can include that in my if statement too.. like so (where "#" is the CR)

# we need to check on the carriage return too.. but how?
${If} $1 == "failed#"
MessageBox MB_OK "failed login!"
abort
${endIf}

hope this helps, a little, I've been trying for a couple hours now to no avail. gah
shaunb#
its this for sure, when I open the file in a word processor and turn on 'show extra formatting' it shows the CR/LF symbol.

also, theres 7 bytes in the text file "success" but the actual file size is 9 bytes.

I could do a sort of 'substr' on the string to find 'success' in 'success\r\n' I guess, but its a bit of a bodge
Red Wine#
OK this will make the job:
strcpy $1 $1 '-2'
${If} $1 == "failed"
MessageBox MB_OK "failed login!"
abort
${endIf}
Red Wine#
Well, create a text file, name it read.txt, write in this file failed press enter (add a CR) and save it in $exedir.
Now compile and execute the following script:

outfile test.exe

function .onInit
FileOpen $4 $EXEDIR\read.txt r
FileRead $4 $1
FileClose $4
messagebox mb_ok '$1 contains a CR'
strcpy $1 $1 '-2'
strcmp $1 'failed' +1 +3
messagebox mb_ok '$1 without CR'
abort
messagebox mb_ok 'Error!'
functionend

section -
sectionend
shaunb#
thats exactly what i'm trying now, chopping two bytes from the end! will let you know how it goes.

edit: works fantastically. now.. what about changing the focus back from the next button, to the texbox and clearing the value of the wrong password?

thanks, I owe you a lot, the help this community gives is great.
Red Wine#
thanks, I owe you a lot, the help this community gives is great.
You welcome :-) we all owe to NSIS development team.


what about changing the focus back from the next button, to the texbox and clearing the value of the wrong password?
Abort on a custom page leave function means that you stay in the page because validation has failed.
To clear the pwd field use SendMessage e.g.
!include WinMessages.nsh ;up along with other includes
!insertmacro MUI_INSTALLOPTIONS_READ $0 "getCFPass.ini" "Field 2" "HWND"
SendMessage $0 ${WM_SETTEXT} 1 "STR:"
shaunb#
that works great too, just need to set the focus now , I tried the obvious WM_SETFOCUS but meh. didnt work

I'll work this one out though .. hehe
Afrow UK#
Use TrimNewLines function in the documentation under Useful Scripts. It's not always guaranteed that the file will use carriage returns followed by new line breaks and a -2 chop may remove more characters than you want it to.

-Stu
shaunb#
Hmm, thats the only time i need to read from a text file thankfully.

The setfocus code, it sets the focus back to the textbox as I can see the cursor re-appear in the textbox after a wrong password, however, when I try to type I realise the focus is still actually on the next button, odd, strange how it appears the textbox is in focus. I still have to click in the textbox to edit however.
zeeh3#
Originally posted by Red Wine
SendMessage $0 ${WM_SETFOCUS} 1 0
Use instead: System::Call "user32::SetFocus(i r0, i 0x0007, i,i)i"
Red Wine#
Use instead: System::Call "user32::SetFocus(i r0, i 0x0007, i,i)i"
Did you use that your self?
Afrow UK#
I'm not sure why you have those extra parameters zeeh3, unless I'm missing something.

user32::SetFocus only takes one parameter and that is the window handle of the control to set keyboard focus on.



-Stu
zeeh3#
Originally posted by shaunb
thanks guys, will give it a go tomorrow.

edit: just checking, is "r0" supposed to be $R0 ?

🙂
No, read system plugin documentation:
r0 through r9 = $0 through $9
r10 through r19 (or R0 through R9) = $R0 through $R9, respectively.
shaunb#
Originally posted by zeeh3
Oops... System::Call "user32::SetFocus(i r0)i"
works perfect, thanks very much

I understand the registers better I think now, too.