A while ago, I had to adapt a DOS app that used one specific version of Excel to do some batch processing so it would support multiple versions of Excel on multiple versions of Windows.
One of the big drawbacks of DOS applications is that the command lines you can use are even shorter than Windows applications, which depending you how you call an application are:
- 32767 characters when you call CreateProcess (the limit is UNICODE_STRING structure)
- 8192 characters when you use Cmd.exe
- 2048+32 characters when you use ShellExecute or ShellExecuteEx (the limit is INTERNET_MAX_URL_LENGTH)
- 260 characters for the Windows 95 family of products (the limit is MAX_PATH)
- 127 characters for DOS (the upper limit of a signed byte) often excluding the length of “cmd.exe” or “command.exe”
This is how the DOS app written in Clipper (those were the days, it was even linked with Blinker :) started Excel:
c:\progra~1\micros~2\office11\excel.exe parameters 01234567890123456789012345678901234567890 1 2 3 4
The above depends on 8.3 short file names that in turn depend on the order in which similar named files and directories have been created.
The trick around this, and around different locations/versions of an application, is to use START to find the right version of Excel.
The reason it works is because in addition to PATH, it checks the App Paths portions in the registry in this order to find an executable:
- HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths
(only Windows 7 and up) - HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
(all Windows versions)
So when Excel is registered correctly, it will find it.
I fixed the Clipper app to start Excel like this:
C:\WINDOWS\system32\cmd.exe/c start /wait Excel parameters 012345678901234567890123456789012345678901234567 1 2 3 4
Actually, for C:\WINDOWS\system32\cmd.exe
, I used %ComSpec%, so it is location independent.
Anyway, the prefix grew from 39 to 47 characters, so now there are only 80 characters left for actual parameters (not 88 as we had first). Just enough for the longest parameter list we were sending.
Since starting Excel by itself waited for Excel, I needed the /WAIT parameter for START:
- /WAIT : Start application and wait for it to terminate
Another thing to note is that there is no space in front of the /C: it will automatically be inserted.
–jeroen
via
- Start – Start a program.
- What is the command line length limit? – The Old New Thing – Site Home – MSDN Blogs.
Filed under: Batch-Files, Development, Encoding, Power User, Scripting, Software Development, Unicode, Windows, Windows 7, Windows 8, Windows Server 2000, Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, Windows Server 2008 R2, Windows Vista, Windows XP
