[Original]Proof of Concept: Bat & Vbs Hybrid Programming
Posted | archive
Proof of Concept: Bat & Vbs Hybrid Programming
By est, electroniXtar (from) GMail.com
vbs.bat
:On Error Resume Next
Sub bat
echo Batching_codez_here_following_vbs_rules & pause
start wscript -e:vbs "%~f0"
Exit Sub
End Sub
MsgBox "This is vbs"
Save the code above as vbs.bat, double click it, and you'll find a Batch echo:
Batching_codez_here_following_vbs_rules
and after press any key, you'll find a vbs prompt saying "This is vbs"
These little code costed me a whole night >_< So I think it's worth commentating.
:On Error Resume Next
In batching, any character that follows the colon : will be neglected. So the first line means same as
rem On Error Resume Next
But this line fucking useful for the 6 lines bellow.
start wscript -e:vbs "%~f0"
First I thought about
wscript //e:vbscript ~%f0"
And the vbscript interpreter could not recognize // even I "Resume Next". So after many trials and errors, finally I got this line both right for cmd.exe and wscript.exe. For cmd.exe, it launched the batch file itself, "%~f0". Instead of using %0, I choose %~f0. For example, you save this vbs.bat in directory D:\test, then you type these commands in cmd.exe
D:
cd test
vbs
Stop! Now, %0 is vbs , and for %~f0 is D:\test\vbs.bat ! wscript.exe will not start vbs.bat if you just run
wscript -e:vbs vbs
Another problem, if I save this vbs.bat in a directory like C:\Documents and Settings\Administrator\Desktop\vbs.bat, quotes are needed to make "Documents and Settings" complete string.
So what may wscript.exe regard this line?
start wscript -e:vbs "%~f0"
"start" here possibly means a undefined Function() or Sub(), it's called with the variant named wscript, after this Function(), it's return value substract "e", "e" here is, maybe, another undefined variant like "wscript". The following : is a colon separating two statements in vbs grammar. The statement on the right of the colon : asks wscript.exe to call a function named "vbs" the the parameter "%~f0". After all, our dear wscript.exe is cheated, and cmd.exe is fooled!
Now this little code is using wscript to call itself to run like a real vbs, but, on the other hand, cmd.exe is trying to understand every line of the fake bat file. I have to do something to stop that, using "exit", but in vbs exit is a keyword, you must exit Exit Do, Exit For, Exit Function, Exit Property, or Exit Sub. And to me, Exit Sub looks good, so in the second line of my code I wrote
Sub bat
cmd.exe may look for a external program called sub.exe, and normally this program does not exist, so go on, after
start wscript -e:vbs "%~f0"
we
Exit Sub
To cmd.exe, the batch quits. To wscript.exe, the Sub() exits. And after that
End Sub
cmd.exe is over now, so wscript will just regard an end of a usual Sub(), but as a bat file, it succesfully launched itself just like a vbs.
You may ask: why must I do this after all? I just use a bat to "echo" out a genuine *.vbs file, and wscript that *.vbs and that's all? Truely this is the normal ways people call scripts between bat and vbs. But think about an extra large vbs? I have an experience here. I wrote a vbs that can spawn a 400KB file, the vbs itself is 600KB, and I tried to "echo" out the vbs in a 700KB bat file, what happened? My XP SP2 rush up to 1.5GB memroy cost for 1 minute, and failed to spawn that 400KB file saying: "vbs error in line (2,7): Not enough memory to create a string". If I use the little codez above, wscript will run quite well just like a real vbs. Another advantage of my code is, no tmp file needed for switching between two languages. simply write batch code above
start wscript -e:vbs "%~f0"
and write vbs code below it!
Hope everyone can find extra usage of my code. Thanks for taking time reading my article. If you have any better ideas, please let me know.
Comments