Take control of batch files with WinAPIExec

WinAPIExec

If you're always struggling to make scripts and batch files do what you need, then you could try extending Windows' own capabilities with a few command line tools. There are plenty of freebies around to help you manipulate processes, work with application windows, and generally automate just about any Windows task.

You'll have to find them first, of course. And it may require a sizeable library of tools before your batch files are able to do everything you'd like.  So if you'd prefer your system to stay uncluttered then it may be easier simply to download WinAPIExec, a tiny program (3.5KB) that allows you to call any Windows API function from the command line.

Of course if you're not a developer then having to learn about the Windows API probably isn't what you'd call "easy", exactly, but fortunately the author has included some useful examples in the program's documentation, and these alone are probably enough to justify giving the program a try.

Would you like to programmatically open the door of your optical drive, for instance? Here's the WinAPIExec command to use (it'll probably wrap on your display, but all these commands must be entered on a single line):

winapiexec.exe winmm.dll@mciSendStringW "open cdaudio" 0 0 0 , winmm.dll@mciSendStringW "set cdaudio door open" 0 0 0 , winmm.dll@mciSendStringW "close cdaudio" 0 0 0

If you've more than one optical drive then this will probably only open the first, but a quick revision will allow you to open any drive you like. This command opens the door of our test PC's Blu-ray drive F: , for example:

winapiexec.exe winmm.dll@mciSendStringW "open F: type cdaudio alias DriveF" 0 0 0 , winmm.dll@mciSendStringW "set DriveF door open" 0 0 0 , winmm.dll@mciSendStringW "close DriveF" 0 0 0

This API access allows you to manipulate processes in some interesting ways. Here's a command to launch Notepad, wait for 5 seconds, then close it down again:

winapiexec.exe CreateProcessW 0 notepad 0 0 0 0?-20 0 0 $a:0?-44,,,,,,,,,,,,,,,, $b:16 , Sleep 5000 , TerminateProcess $$:11@0 0

Extend that idea and you could create a batch file that runs a program just for an hour, say, then closes the app if it hadn't shut down itself.

You can also use the Windows API to collect information and display it to the user. Here's how to display a message box containing the current TEMP path, for instance.

winapiexec.exe GetTempPathW 260 $b:520 , u@MessageBoxW 0 $$:3 $$:0 0?-40

This command will programmatically launch the Start menu:

winapiexec.exe u@SendMessageW ( u@FindWindowW Shell_TrayWnd 0 ) 0?-111 305 0

While this one pops up the Windows clock window:

winapiexec.exe u@PostMessageW ( u@FindWindowExW ( u@FindWindowExW ( u@FindWindowW Shell_TrayWnd 0 ) 0 TrayNotifyWnd 0 ) 0 TrayClockWClass 0 ) 0?-466 1 0

And this launches the Search box:

winapiexec.exe u@SendMessageW ( u@FindWindowW Shell_TrayWnd 0 ) 0?-111 41093 0

Need more? The WinAPIExec author points to a CodeProject page which offers more useful Windows messages. Take the last example, for instance, replace 41093 with 415, and you'll have a command to programmatically minimise all open windows.

And Googling for interesting chunks of these commands, like "set cdaudio door open", may also give you more ideas about what you can do. Or you can check out the relevant Microsoft references, although beware, they don't exactly make light reading.

Be careful if you start experimenting, though. WinAPIExec gives you full access to all kinds of powerful functionality, and misusing this could create various problems, perhaps making your system unstable or crashing running applications. This isn't likely, but play safe anyway, and save any open documents before you start playing around with new WinAPIExec commands.

4 Responses to Take control of batch files with WinAPIExec

© 1998-2024 BetaNews, Inc. All Rights Reserved. Privacy Policy - Cookie Policy.