The function to do so is apparently in User32.dll. I've been trying to tinker with rundll32.exe, but when I run this:
rundll32 User32.dll,GetActiveWindow
It exits without error but does nothing. The exit code will of course only say whether or not there were errors running rundll32.
Is there a bat scripting trick to retrieve that value and stuff it into a variable?