Previous Thread
Next Thread
Print Thread
Executing Windows command-prompt commands #5532 05 Feb 09 01:50 PM
Joined: Jun 2001
Posts: 11,794
J
Jack McGregor Offline OP
Member
OP Offline
Member
J
Joined: Jun 2001
Posts: 11,794
Periodically I am asked how to execute some Windows command-prompt command (like dir, ipconfig, etc.) from within A-Shell. The solution is to use MX_SHELLEX with the following parameters:

objectspec = "cmd.exe"
action = "open"
parms =

You may want to precede your command with one or more of the cmd.exe switches so you can control options such as whether the command prompt window stays open, closes itself, etc. The general syntax of cmd.exe is:

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
[[/S] [/C | /K] string]

To get the full details, enter cmd /? from the Start > Run prompt.

For example, let's say we want to capture the output of the ipconfig command into a file ipcfg.lst, without ever displaying the command prompt window. We could execute the following:

xcall MIAMEX, MX_SHELLEX, status, "cmd.exe", "open", "/c ipconfig > ipcfg.lst", "", SW_HIDE, 1

In the above command, the final 1 is to force us to wait for the completion of the command, and the SW_HIDE causes the command prompt window to display invisibly. The "/c" at the start of the command line tells the command prompt to close itself on completion of the command. (Otherwise you would accumulate invisible command prompt windows, which wouldn't be very tidy.)

Re: Executing Windows command-prompt commands #5533 21 Jun 11 05:07 PM
Joined: Jun 2001
Posts: 11,794
J
Jack McGregor Offline OP
Member
OP Offline
Member
J
Joined: Jun 2001
Posts: 11,794
The above technique has a potential problem, in that if you are using UNC paths in your DEVICE statement in the MIAME.INI, the default working directory for the cmd.exe will be a UNC path (e.g. \\server\sharename\dsk0\123445).

It turns out that cmd.exe, by default does not allow running from a UNC directory, so it will spit out an error message and proceed to log to c:\windows, but in the process will probably fail to execute your command.

You can specify an explicit working directory in the dir parameter of MX_SHELLEX . But if you are using UNC paths and want to execute the target command from your current directory, that's not very convenient (since it isn't easy, or necessarily even possible, to convert a UNC path to a drive-letter equivalent).

Fortunately, this restriction in CMD.EXE can be disabled by setting the registry value HKCU\SOFTWARE\Microsoft\Command Processor\DisableUNCCheck

To do that programmatically:

Code
map1 reg'value,b,4,1
map1 hkey'id,b,4
map1 status,f

xcall MIAMEX, MX_REGISTRY, REGOP_OPEN, HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Command Processor", &
    RGKEY_SET_VALUE, hkey'id, status
xcall MIAMEX, MX_REGISTRY, REGOP_SET, hkey'id, "DisableUNCCheck", REG_DWORD, reg'value, status
Thanks goes to Jorge Tavares for discovering the problem and the solution, and sharing it!


Moderated by  Jack McGregor, Ty Griffin 

Powered by UBB.threads™ PHP Forum Software 7.7.3