Jump to content

Help with variable


Recommended Posts

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.

Link to comment
Share on other sites

  • Moderators

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.

"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!

Link to comment
Share on other sites

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 by Chimp
removed the show_flag from the run command

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

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 by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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)

Link to comment
Share on other sites

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)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

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 Gude
How 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

Link to comment
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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)

Link to comment
Share on other sites

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 Gude
How 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

Link to comment
Share on other sites

idea The is same the. :D

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 here
RegExp tutorial: enough to get started
PCRE 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)

Link to comment
Share on other sites

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. :ILA2:
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...