Saving Variables Permanently - vb6

I have a variable called random number that needs to be stored when the application has be shutdown or the computer has been shutdown. Every time this number is used I also need to +1 to it.
I have a few variables in my current vb6 application that need to be saved when the app is closed and loaded when the app is launched. Is this possible? I could use a text file or a config file to store the variables?
EDIT -------------------------------------------------------------------------------
I managed to fix this problem had just using a simple input and output text file. Please read my answer below if you have the same problem and need assistance.

The standard way to save values in VB6 apps was to use INI files. If I remember there are a couple of Win32 functions to read/write them.
They are GetPrivateProfileString and WritePrivateProfileString.

Using the registry is the correct way to do it.
VB has built in functions SaveSetting and GetSetting for writing to and reading from the registry.
See registry tutorial or Stack Overflow question to help you out.

I managed to complete the task by creating a file in my C Drive and putting in the number "123" to the top line of the text file. I then wrote the following code:
Function GetPOIRandomNum()
Dim LineA As String
'Collect stored variables
Open "C:\TestPartner\Config\POIRandomNum.txt" For Input As #1
While Not EOF(1)
Line Input #1, LineA 'Read the first line in the file
POIRandomNum = LineA + 1 'Give POIRandomNum the integer from line 1 and add 1 to it
Wend
Close #1
'Save the new random number variable to the file
Open "C:\TestPartner\Config\POIRandomNum.txt" For Output As #1 'Open for output to replace the old number
Write #1, POIRandomNum 'Input the new number to the text file
Close #1
End Function
Now whenever the Random number Variable is needed I call the above function.

Related

Pascal I need to modify the program as the result to be write in text file maxim.out

I have a program which is counting the biggest number from 3 numbers. I need to modify the program as the result to be write in text file maxim.out (PASCAL)
You can write the value (assuming it is an integer and it has the name, say, yourValue) with:
var
maximFile: Text;
...
Assign(maximFile, 'maxim.out'); // link the name to the Text variable
Rewrite(maximFile); // open it for writing
Writeln(maximFile, yourValue); // write the value as a line of its own
Close(maximFile); // close the file
You can then read back the value later on with:
Assign(maximFile, 'maxim.out');
Reset(maximFile);
Readln(maximFile, yourValue);
Close(maximFile);
I did not add any error handling (e.g. if the file can't be found, or if it is readonly, or empty, or ...). Depending on settings, that is either done with exceptions or with IOResult values. Read the documentation on how to do that. There should be examples in the docs.
You should read about "file management in pascal". Anyway, declare a variable of type textfile:
var
outputfile : TextFile;
then assignfile() to it your name of choice (maxim.out), rewrite() the file, use writeln() to write into it, and finally closefile() it.
You can find a complete example program here: http://wiki.freepascal.org/File_Handling_In_Pascal

Update a matrix into a text file without appending the results

I have a Fortran 77 code like this, more or less:
nMaxRow=100
nMaxStep=100
! initialization of the matrix if Step=1
do step=1,nMaxStep
if (step.eq.1) then
do ii=1,nMaxRow
do jj=1,nMaxStep
A(ii,jj)=0
end do
end do
end if
!now for each step and for each row update the cell of the matrix
do ii=1,nMaxRow
A(ii,step)=X(ii) !X(ii) is a number associated with the specific ow at that specific step
end do
!Now I want to write the updated matrix at this step into a text file,
!How can I do that????
end do !close the do step...
Is it possible to update the values of the matrix and write the updated matrix at that specific step into a text file? I mean, without appending the results each step...
I found out that for Fortran 90 the 'REPLACE' command exists... but I couldn't find anything similar for Fortran 77.
One simple idea would be deleting the file just before writing a new one... but I don't like it and I don't know how to do it anyway.
If the file is already open (from the previous writing), you can just go the the start of the file using
rewind(unitnumber)
and start writing again. It will delete the original content of the file and start again. If you wan't to go back just be several records, you can use backtrace(), but you probably don't want that here.
If it isn't open, just open it and start writing. Unless you open it of appending, it will overwrite the original content.

is there a way to stop the popping up of the md5.exe tool while it generates the hash for a file

i have written a program in vbscript for which i have used md5.exe to generate hash. since there are many files to which the hash has to be generated, the md5 hash repeatedly generates hash for each file one after the other. but while this process is in progress, i can see it popping out on the screen as it generates the hashes ( it does not pop out the hashes, the tool itself pops out on the screen repeatedly). i want to do something such that it stops popping out yet generate the hash for all the files. please help guys!
Generally, when running a command, youj can supress its output by directing it to a file, then deleting the file
md5.exe blahblah >null the >null being the critical part. Note however, that null doesn't directly it to a magical blackhole. It creates a file named null and prints the output there
This would supress the output. IF you posted some code, I could have told you how to do that there. but if you are running it as a shell exec, this should work
You can use the run method to supress the window.
Set WshShell = CreateObject("WScript.Shell")
cmd= "C:\Users\Administrator\desktop\experimenting\md5.exe"
'OR whatvere your whole command is
cmdRun = WshShell.Run(cmd,0,true);
Answer from here
Want to hide command prompt window in using WshShell.Exec method
(Upvote the guy if you find it useful)
Please note that you have tro use the output file to read. You cannot read the output from the shell anymore.

Visual Basic - Writing / Overwriting into a text line adds a new line

Hello everyone I made a simple program that takes my external IP and places in a my websites public camera. And I got a problem - The program is making a txt file with the ip inside it and uploads it to the server.When the program is overwriting/editing/creating the file its adding an empty new line which messes up my PHP code...
This is the code used for both overwriting/editing and creating the file
Dim strFile As String = "c:/IPtoUse.txt"
Dim fileExists As Boolean = File.Exists(strFile)
Using sw As New StreamWriter(File.Open(strFile, FileMode.OpenOrCreate))
sw.WriteLine( _
IIf(fileExists, GetIP, GetIP))
End Using
(the GetIP function is getting my ip from my server)
This ends up with another empty line. How can I fix it?
Thanks!
Going on the information from the question and comments, it seems that your file will end up with an additional linefeed at the end in both cases (ie. both for new and modified files).
The reason for this is that you're using the WriteLine method, which will append a newline at the end of the text it writes, even if that text already ends with a newline.
Simply change the code to use the Write method instead of the WriteLine method and you should end up with a file that contains only the text passed to the method.

Embed EXE file in the Excel file

I use:
retVal = Shell("program.EXE " & filename, vbNormalFocus)
To execute a program need for my excel spreadsheet.
Is it possible to embed the EXE file in the excel file itself?
And how would I execute it then?
Ideias:
1 - Some kind of a bin2str function to turn binary to string (so I can store it in the program as a variable and a str2bin (the oposite)
2 - I read something about OLE Control (that you can embed it there), but I really don't know where to start on this one
Here's an outline solution that avoids OLE:
Create a hidden worksheet.
Use a base 64 encoded to convert the exe to text.
Store that text in worksheet cells on the hidden worksheet. Since there is a limit on the number of characters in a cell (32,767) you will need to break the string into chunks.
Obviously you'll need to reverse this procedure when you want to save and execute the exe file.
You can do this by using: Insert > Object and then selecting 'Create from File'.
To add it to your sheet using VBA:
Dim o As OLEObject
Set o = ActiveSheet.OLEObjects.Add(Filename:="C:\program.exe")
Then this is the command to execute program.exe:
o.Verb Verb:=xlPrimary
Not sure how to pass arguments to it, however (e.g. your filename).
Note: Untrusted applications prompt a warning when you run them.

Resources