JuanFelipe Posted August 17, 2018 Posted August 17, 2018 Hello friends, I have a simple question, can I have a variable that is not reset, even if my program is closed?That is, I want to make a program that can only be opened once, have a variable $ ini = 0, when it is opened for the first and only time it becomes $ ini = 1, so that it can not use the program a second time, as if it was a test software.I do not want the variable to depend on a reading of an external file that is easily manipulated. Thank you.
Moderators JLogan3o13 Posted August 17, 2018 Moderators Posted August 17, 2018 Variables, by their nature, live while your program is alive. You cannot expect the computer to remember the variable setting between runs of your program unless you are writing it somewhere outside of memory (it is called volatile for a reason). That would be an INI file, a Registry entry, a DLL even, but somewhere on the physical drive. If you do not want to use a file, go with a registry entry written on first run and then checked on subsequent runs. Xandy and JuanFelipe 1 1 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
FranckGr Posted August 17, 2018 Posted August 17, 2018 Why not FileDelete(@ScriptFullPath) at the end of your script ... JuanFelipe 1
mikell Posted August 18, 2018 Posted August 18, 2018 22 hours ago, FranckGr said: Why not FileDelete(@ScriptFullPath) at the end of your script ... Just keep a copy of the program and then you can run it twice - and so on
IAMK Posted August 18, 2018 Posted August 18, 2018 (edited) https://www.autoitscript.com/autoit3/docs/libfunctions/_Singleton.htm Seems you already got answered, but I'll put this here if you want it. Edited August 18, 2018 by IAMK
mikell Posted August 19, 2018 Posted August 19, 2018 11 hours ago, IAMK said: https://www.autoitscript.com/autoit3/docs/libfunctions/_Singleton.htm No. This forbids 2 instances of the same program to run together, nothing more BTW the only secure (?) and non-local way I can think of would be a file (encrypted ?) to check on a ftp
Gianni Posted August 19, 2018 Posted August 19, 2018 (edited) supposing you are using a compiled script, here a simple 'experiment' to have a program runnable only once. In few words, after the first run, I just append a byte at the end of the exe setted to "1" so at the next run we found the "1" as the last byte and we exit. just a funny experiment, no claim of bullet-proofness... If Not @Compiled Then Exit MsgBox(0, "Info", "script not compiled error.") Local $sThis = @ScriptFullPath Local $hHndl = FileOpen($sThis, 16) ; open the exe in byte mode FileSetPos($hHndl, -1, 2) ; $FILE_END (2) -1 = last byte of the file $LastByte = FileRead($hHndl, 1) ; get last byte If $LastByte = "1" Or $LastByte = 0x31 Then MsgBox(0, "Debug " & $LastByte, "I'm sorry, this program has expired", 5) Exit EndIf ; your one time run program here below ; ------------------------------------ MsgBox(0, "Debug " & $LastByte, "This is a 'one time only' run program" & @CRLF & "next time this program will not run") ; ; ; on exit add a byte to the exe to flag "no more run" ; ------------------------------------ ; ping is just to wait 2 seconds so to give time to the main exe to terminate ; You can't write a byte to the exe if is running Local $sCommand = 'ping localhost -n 2 > nul & echo | set /p dummy="1" >> ' & $sThis ; the command is run 'detached' from the main exe Run(@ComSpec & " /c " & $sCommand) ; , @SW_HIDE) Edited August 20, 2018 by Chimp removed the show_flag from the run command Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....
mikell Posted August 20, 2018 Posted August 20, 2018 Chimp, this doesn't work for me... Isn't the last on-exit command a part of the still running exe ?
Gianni Posted August 20, 2018 Posted August 20, 2018 (edited) hi @mikell the last run command delegates the dos environment to execute the 2 concatenated commands in a detached way from the main script. The ping command is there as a sort of delay of about 2 seconds to give time to the main exe to terminate so that it is not locked when the second command will append the last byte to the exe. However a weird fact happens.... if the show_flag parameter of the run command is set @sw_hide or @sw_minimize then the write of the byte fails. I've seen that just removing the show_flag parameter from the run command allows it to work. ...maybe the echo fails if command prompt is hidden...?? suggestions and alternatives to write a byte from dos environment to a file are welcome Edited August 20, 2018 by Chimp Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....
jchd Posted August 20, 2018 Posted August 20, 2018 Another way but NTFS-only. Kid sister will bypass this but most users won't. #include <Date.au3> If Not @Compiled Then Exit MsgBox(0, "Info", "Script must be compiled to run (once only).") Local $sFname = @ScriptFullPath & ":Dead Beef" If FileExists($sFname) Then MsgBox(0, "Run status", "Sorry this program has expired. It already ran on " & FileRead($sFname), 5) Exit Else _Main() EndIf Func _Main() MsgBox(0, "Welcome", "This is a 'one time only' runable program" & @CRLF & "Next time this program will not run.") ; program core here FileWrite($sFname, _NowCalc()) EndFunc This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
Simpel Posted August 20, 2018 Posted August 20, 2018 As a colon is not allowed in windows inside a file name, what is happening there? SciTE4AutoIt = 3.7.3.0 AutoIt = 3.3.14.2 AutoItX64 = 0 OS = Win_10 Build = 19044 OSArch = X64 Language = 0407/german H:\...\AutoIt3\SciTE H:\...\AutoIt3 H:\...\AutoIt3\Include (H:\ = Network Drive) Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.
BrewManNH Posted August 20, 2018 Posted August 20, 2018 Alternate File Stream data. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
jchd Posted August 20, 2018 Posted August 20, 2018 2 hours ago, Simpel said: As a colon is not allowed in windows inside a file name, what is happening there? My example demonstrates a colon there is perfectly valid syntax. The magic is named as @BrewManNH said an ADS (Alternate Data Stream). This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
BrewManNH Posted August 20, 2018 Posted August 20, 2018 8 minutes ago, jchd said: ADS (Alternate Data Stream) No idea why I keep reversing those. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
jchd Posted August 20, 2018 Posted August 20, 2018 idea The is same the. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
JuanFelipe Posted August 20, 2018 Author Posted August 20, 2018 Thank you very much to all for your answers, I tell them how I did it, in the end I created a .log file, with information encrypted inside, where I give guidelines to the program of how many times it can be executed and also the limit of queries that can be made , I read them all and I'm going to try the things they told me to see how it goes. Thanks brothers.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now