vb6 read and write a text file in win 7 - windows-7

I am trying to execute a .exe file (created by VB6) on Windows 7.
What the application will do is to simply create a text file, and then read the text for a text file.
All the functions are being tested successfully on Windows XP. When I execute the .exe file on Win 7, the functions act as usual but the file I created doesn't exist on the specific path (C:\test.txt). The most weird thing is that I can still read the content from the text file (through the .exe file) despite being unable to find the text file on file explorer.
Then I discovered that I have to choose [run as Administrator] to execute the .exe file, so that the file (test.txt) will be created on the C: drive. I am very curious how the .exe file can still read the text file even it is not existed, and how can I force the .exe file to run as Administrator?
Here is the coding to write and read a file.
Open "C:\" & "test.txt" For Output As #1
Print #1, cDrive.Text
Close #1
Open "C:\" & "test.txt" For Input As #1
Input #1, msg
Close #1
cDrive.Text = msg
Exit Sub

To answer the third remark:
Windows Vista and Windows 7 User Access Control (UAC) introduced a feature called the VirtualStore which is designed to add an extra layer of security protection for applications installed under the Program Files folder. If you search for the file on you hdd you might find a second instance of the file in /User/AppData/Local/VirtualStore
So that's why it is still able to read text.txt allthough is doesn't excist in the location you mentioned.

I suggest that you run the program as an administrator by using the feature in the shortcut (after compiling and installing).
If you asked about it when in debug mode - I believe that if the user that you use to debug is an admin - it's enough.
I have no answer for your question "I am very curious how the .exe file can still read the text file even it is not existed"
You should not specify open as #1 directly, use FreeFile() function instead Look here for a sample.
Good luck

Drive C is being protected by Win7, you can still write to it, but you really shouldn't.
If you want to run the app as admin:
right click on the EXE
select PROPERTIES
go into COMPATABILITY tab
check RUN THIS PROGRAM AS ADMINISTRATOR check box
click OK
Now every time you run the app, it will run as administrator

Related

Mimic drag-and-drop from the Windows command prompt

In Windows, you can initiate a Bluetooth transfer of a file by right clicking the file and choosing Send to > Bluetooth device. The Bluetooth File Transfer box appears, after which you select what device to send to. I have found that I can achieve the same effect by dragging-and-dropping the file that you want to send onto C:\Windows\system32\fsquirt.exe.
To figure out how Windows passes files to programs when you use Send to or drag-and-drop, I wrote the following Python script and placed a shortcut to it in C:\Users\<username>\AppData\Roaming\Microsoft\Windows\SendTo. The shortcut's Start In directory is the .py file's containing directory. (I am using Python 3.6.)
#!python3
import os, sys
print(os.getcwd())
print(sys.argv)
input("Press Enter to exit...")
When I drag-and-drop a file onto the shortcut or the script itself and when I right-click a file and choose Send to > the new shortcut, the same thing happens in all three cases: on the first line, Python prints out the containing directory of the .py file, and on the second line, Python prints out a repr of a list containing two elements: the absolute path to the .py file and the absolute path of the file that I chose.
I wrote the following batch file for testing:
cd C:\folder\of\py\file\
C:\folder\of\py\file\argv_dump.py "C:\path\to\a\file.txt"
When I run this batch file, either from the command prompt or by double-clicking it in Windows Explorer, I get the same output from before. on the first line, Python prints out the containing directory of the .py file, and on the second line, Python prints out a repr of a list containing two elements: the absolute path to the .py file and C:\path\to\a\file.txt.
Now, I am trying to initiate a Bluetooth transfer on Windows 10 from a script. I have written a three-line batch file for testing:
cd C:\Windows\system32\
C:\Windows\system32\fsquirt.exe "C:\path\to\a\file.txt"
pause
This does not appear to send "C:\path\to\a\file.txt" to fsquirt.exe. The Bluetooth File Transfer dialog box appears, but it still asks me to choose what file I want to send. Running these commands in a command prompt window yields the same result.
How does fsquirt.exe know the difference between when I drag-and-drop or use Send to and when I manually pass it an absolute file path from a batch file or the command prompt? How can I get fsquirt.exe to notice the file path that I am giving it?
I did look at this other question, which suggested checking the current working directory of the batch file. Wouldn't cding inside my batch file take care of that, though?
In the past, I have used the Bluetooth Command Line Tools via Bluetooth Installer. They allowed me to automate a Bluetooth workflow via the Windows command line.

Running exe file behind a txt file

Was reading about ADS(Alternate data streams) in windows, so came across this thing.
Working on Windows XP to test the following.
I created a file with name - sample.txt with the content - "hello there"
Created another file with name - second.txt with content - "I am hidden"
At command prompt did the following:-
type second.txt > sample.txt:newfile.txt
It executes successfully
Now, if i type this:-
notepad sample.txt:newfile.txt
Notepad opens newfile.txt with the content - "I am hidden"
Till here, its normal
Now, I tried to hide an exe file,lets say - notepad.exe behind a txt sample like this:--
(i have copied notepad.exe in the same folder)
type notepad.exe > sample.txt:newexe.exe
Above command runs successfully.
Question is, now how can i run that exe file which is now behind text file. My guess is I need some form of interpreter (like notepad.exe in the first example) to make my exe run. Does exe files require some sort of interpreter to run(like text files need some editor)??
I know, exe files can be run directly, but here in this case thats not working. Even tried:--
start second.txt:newexe.exe
but it doesnt work. Help will be greatly appreciated, as I dont know much about windows internals.
Sensibly, Windows will not allow this and will throw an Access Denied if you try to execute it, similarly there is no easy way to copy a binary stream.
With administrative privs you can mklink blah.exe sample.txt:newexe.exe which would create a symlink to the executable ADS, executable via blah.exe.
Under Windows XP you must give the full qualified name of the ADS to start (in this case I have 'hidden' depends.exe in dbgview.exe):

cannot run a .svb file

I am using Windows 7 and have associated .svb files with C:\Windows\System32\wscript.exe which is the Windows Script Host program
But when I click the file I get the popup "There is no script engine for file extension .svb"
I have googled it but cold find a solution
Any ideas?
wscript.exe seems to be just a stub which itself calls the interpreter for Visual Basic Script, JavaScript etc. based on the file extension. You have associated the .svb extension with wscript.exe, but wscript.exe does not know what to do with it. That said, you probably have a typo there, and the file should be named .vbs (for Visual Basic Script) instead of .svb.
Create a VBScript file to play the sound.
Open Notepad and paste the following lines into it:
Set oVoice = CreateObject("SAPI.SpVoice")
set oSpFileStream = CreateObject("SAPI.SpFileStream")
oSpFileStream.Open "C:\Windows\Media\YourSoundHere.wav"
oVoice.SpeakStream oSpFileStream
oSpFileStream.Close
Save this file anywhere with a .VBS extension. For example, LogonSound.vbs.
Double-click the file you've created and ensure that it plays your sound file.
https://winaero.com/play-logon-sound-windows-10/

Run an arbitrary file as a windows bat file from Firefox?

Say I've got a windows batch file with a file extension .batscript, and say I've got a web server set up to serve it with a content-type of application/x-batscript. How can I set up Firefox 4 to "always open" this file so that it runs as a standard batch file would? (I have access to the PCs but want avoid creating/installing external programs/extensions/plugins)
I can't use a straight .bat because Firefox doesn't allow those to be opened directly; they have to be saved and run separately (for obvious security reasons).
The standard way to run a batch file is cmd /c %1 where %1 is the path to the bat file, but this doesn't work when %1 is a path to a file with a custom file extension (such as .batscript) because cmd looks up what application %1 should be opened with, so adding a direct file association via the registry doesn't work (it recursively opens cmd prompts).
Instead, I found a work around where I temporarily copy my .batscript file to a .bat file and run it, using registry entries like this, which allows me to double click on a .batscript file to run it. But Firefox doesn't respect these platform file associations!
Ok, so Firefox wants me to define my file type associations separately for each content-type: fair enough, file extensions & content-types aren't always the same. But I can only get Firefox to run discrete applications, and a batch script needs to be run with cmd.exe /c %1 not just cmd %1.
Does anybody know of a way to force Firefox to either use the system file association or allow launching arbitrary programs with command line parameters?
(For those wondering, the use case is similar to "an internal web app which allows users to run a local program with a varying amount of command line parameters by clicking on a link" - alternative solutions to these small "breaking out of the sandbox" problems are also welcome!)
I think you need to edit the MimeTypes.rdf file. For more information see also this page.

What is the simplest program I can write to invoke a batch script?

It is very stupid that windows will not let you add batch files to your quick launch or whatever they call in in windows 7. That bar across the bar, i can attach firefox there, command prompt, even paint (my favorite), but not a batch file. I can "pin" it to another program, which is just weird. I want it to standalone, the batch file does enough work by itself.
So lets say i have batch file. What is the simplest executable program I can write to invoke said script. Then I can finally pin all my useful batch files on that quick-jump-pin-bar.
If I remember right, you can do this by first pinning a shortcut to CMD.exe to the taskbar. Then edit the command, and change CMD.exe to CMD.exe /c MyBatchFile.bat. I believe this will execute the batch file.
Since you can only pin one cmd, here's an alternative, assuming you have the .NET framework installed - a very simple C# application:
Populate a textfile with the following contents:
class Program {
static void Main() {
System.Diagnostics.Process.Start(#"c:\test.bat");
}
}
where Program is the name of the executable you want to create, and c:\test.bat is the full path to the batch file. Save the file as Program.cs. Execute the following from the command line:
c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe Program.cs
You can replace 2.0.50727 with whatever directory exists on your machine - might be 1.1.something or 3.5 or 4.0.something - it's all the same for this scenario.
This will generate Program.exe. You can put that exe file wherever you want, and pin that executable to the taskbar. You can discard the .cs file once you're done making your executables.
Kind of a crappy solution, but it should work, assuming you don't find anything better. And if you don't have the .NET framework (which I'm not sure is even possible in Windows 7), you can install it pretty easily.
The easiest way is to create a folder, put your batch files in it, and pin the folder to the menu bar :D
You can also write a startup script, so the batch file will be executed on startup, but I don't know if you want to run those scripts on every bootup... You can also add the command prompt to the bar, and edit the startup path..
Use command switches on CMD.exe.
cmd.exe /c "myscript.bat"
As a sort of workaround you could use the following trick. Pin an arbitrary application to the task bar, Shift+right click on the pinned icon, select Properties, change 'Target' and 'Start in' accordingly. Rename it too if you like.
You can pin more than one .bat file using this technique.

Resources