Saturday, December 6, 2008

Watchmen


I finally completed a reading of "Watchmen", a well acclaimed DC comic written back in 1987. I never knew it existed till I saw the trailer for it's upcoming movie adaptation for March 2009. I must say it is one of the two best comics I ever read with the other being Kingdom Come. Here's the Watchmen trailer that got me interested. The movie adaptation is directed by Zack Snyder that previously did 300. I'm definitely catching this one.



Thursday, December 4, 2008

Hidden multiple argument BAT file execution with VBS


I have been writing a program that calls external programs using batch files in windows. Every time the call is made, the ugly command line box appears. After searching the net it seems that a simple way would be to use Visual Basic Script (VBS) to call the batch file in a special shell. Since the VBS interpreter is available on WinXP and above systems, I decided to try this vbs script that I found somewhere off the net.

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & WScript.Arguments(0) & Chr(34), 0
Set WshShell = Nothing


Put the above code in a file called "invi.vbs" and then call the following from command line:

$>wscript.exe "invi.vbs" "mybatchfile.bat"


The batch file runs without a window! Thinking that this was great, I tried to run a batch file with arguments. Now that fails somehow because the interpreter seems to think that the entire WScript.Arguments(0) is a program name, i.e. "mybatchfile.bat arg1" will be treated as a program name instead of a batch file with a single argument.

Having not written VB since eons ago, I used my limited knowledge to concatenate the WScript.Arguments into a string. Somehow it just worked after replacing "invi.vbs" with my edits below.

Set WshShell = CreateObject("WScript.Shell")
dim toexec
For Each x in WScript.Arguments
   toexec = toexec & x & " "
Next
WshShell.Run toexec, 0, 1
Set WshShell = Nothing


Then, running it without quotes for the batch file, i.e.:

$>wscript.exe "invi.vbs" mybatchfile.bat arg1 arg2

Monday, December 1, 2008

MinGW to MSVC -- the case of rint()


I have recently been working on shifting a project over from compiling with the MinGW implementation of the GNU C++ compiler to the Microsoft Visual C++ (MSVC) compiler. The most glaring difference is the lack of the rint() function in MSVC. rint(x) takes a double, x, and returns the double value of the closest integer to x. Being a lazy bum I opened the math.h file for MinGW to copy the rint() function to a global win32 compatibility header. Unfortunately, it is written using an assembly instruction and a bunch of macros. Not wanting to copy the macros over, I did the next lazy thing and used Google.

More unfortunately for me, I copied a version of rint() that was not compatible with the implementation in MinGW. I had:

double rint(double x)
{
     return floor(x < 0.0 ? x - 0.5 : x + 0.5);
}

It turns out, that this is wrong as -1.5 should be rounded to -1.0 and not -2.0. After wasting a few hours debugging, I finally realised it should be the much simpler:

double rint(double x)
{
     return floor(x + 0.5);
}

So much for being a lazy bum...