Archive: Cookies, take 2


Cookies, take 2
Hello all,
We have created the following function that checks if specific cookies exist in the cookies folder (format: user@CookieName[3], user@CookieName[2], user@CookieName[1]. When a cookie is found it should take some text from within the cookie and plug it into the PC's registry.
The problem is that it doesn't work the first time and the only predictable way is to run the setup twice (with no uninstall after the first time). During the second install it works.
I am pasting the code here and would appreciate any ideas that users of this forum may have.
Thanks a lot,
David

---- CODE STARTS HERE ----

;Cookie read and record functions

; TrimNewlines
; input, top of stack (e.g. whatever$\r$\n)
; output, top of stack (replaces, with e.g. whatever)
; modifies no other variables.
Function TrimNewlines
Exch $R0
Push $R1
Push $R2
StrCpy $R1 0

loop:
IntOp $R1 $R1 - 1
StrCpy $R2 $R0 1 $R1
StrCmp $R2 "$\r" loop
StrCmp $R2 "$\n" loop
IntOp $R1 $R1 + 1
IntCmp $R1 0 no_trim_needed
StrCpy $R0 $R0 $R1

no_trim_needed:
Pop $R2
Pop $R1
Exch $R0
FunctionEnd

;Will detect and register the Cookie in the registry
Function RegisterKBID
Push $R0
;Search for the first cookie
FindFirst $R0 $R1 "$COOKIES\*.*"
Loop:
IfErrors Done
;Find the most recent cookie - as the IE enumerates them by [x] value, while the biggest is the latest.
;There shouldn't be more then 1, but for any case...
StrCpy $R2 $R1 "" -24
StrCmp $R2 "@testcookie[3].txt" OpenCookie
StrCmp $R2 "@testcookie[2].txt" OpenCookie
StrCmp $R2 "@testcookie[1].txt" 0 NextCookie
;Open the cookie
OpenCookie:
FileOpen $R2 "$COOKIES\$R1" "r"
CookieLoop:
ClearErrors
;Read the first line of the cookie
FileRead $R2 $R3
IfErrors CookieDone

;Find the cookie id
Push $R3
;Trim the /r and /n characters
Call TrimNewLines
Pop $R3
;See whether it's a valid cookie, i.e. it contains the KBID identifier
StrCmp $R3 "KBID" 0 CookieLoop

;Get the KBID value
FileRead $R2 $R3
Push $R3
;Trim the /r and /n characters again
Call TrimNewLines
Pop $R3

;Write it in the registry
WriteRegStr HKLM "Software\TestCookie" "client_id" "$R3"

CookieDone:
FileClose $R2
Goto Done
;Go to the next cookie
NextCookie:
ClearErrors
FindNext $R0 $R1
Goto Loop
Done:
FindClose $R0
FunctionEnd


Have you try "InternetGetCookieEx" from Wininet library? You can use system.dll to call this function and "retrieves data stored in cookies associated with a specified URL".

FYI: http://msdn.microsoft.com/library/en...asp?frame=true :)