why does cmd.exe in CreateProcess behaves different from DOS-prompt?

1.3k views Asked by At

I use Altar's GetDOSOutput() (variant 1) to this question to call dos-commands by a simple delphi program. However, existing DOS program's such as DiskPart cannot be found when called by by CreateProcess whereas they present no problem when called from the DOS-prompt (Windows server 2003 X64). What could be the cause of this?

commandline: `ListVolumes.bat'

ListVolumes.bat:

path
C:\WINDOWS\SYSTEM32\DiskPart.exe /s ListVolumes.scr
dir C:\WINDOWS\SYSTEM32\DiskPart.exe

output through the program call:

I:\PartScan>path
PATH=C:\WINDOWS;C:\WINDOWS\System32;C:\WINDOWS\System32\wbem;C:\Program Files (x86)\Borland\Delphi7\Bin; C:\Program Files (x86)\Borland\Delphi7\Projects\Bpl\;

I:\PartScan>C:\WINDOWS\SYSTEM32\DiskPart.exe /s ListVolumes.scr
'C:\WINDOWS\SYSTEM32\DiskPart.exe' is not recognized as an internal or external command,
operable program or batch file.

I:\PartScan>dir C:\WINDOWS\SYSTEM32\DiskPart.exe
 Volume in drive C is system
 Volume Serial Number is 351F-0221

 Directory of C:\WINDOWS\SYSTEM32

File Not Found

output when called from the DOS prompt (note the final dir command):

PATH=C:\WINDOWS;C:\WINDOWS\System32;C:\WINDOWS\System32\wbem;C:\Program Files (x86)\Borland\Delphi7\Bin; C:\Program Files (x86)\Borl
and\Delphi7\Projects\Bpl\;

I:\PartScan>C:\WINDOWS\SYSTEM32\DiskPart.exe /s ListVolumes.scr

Microsoft DiskPart version 5.2.3790.3959
Copyright (C) 1999-2001 Microsoft Corporation.
On computer: ISOETES

  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
  Volume 0     F                       DVD-ROM         0 B  Healthy
  ...
  Volume 11    G                       DVD-ROM         0 B  Healthy

I:\PartScan>dir C:\WINDOWS\SYSTEM32\DiskPart.exe
 Volume in drive C is system
 Volume Serial Number is 351F-0221

 Directory of C:\WINDOWS\SYSTEM32

17-Feb-2007  08:17           263,680 diskpart.exe
               1 File(s)        263,680 bytes
               0 Dir(s)  33,111,334,912 bytes free
1

There are 1 answers

4
David Heffernan On BEST ANSWER

You've not shown code so we cannot diagnose this with 100% certainty. However, the likely cause is that your process is a 32 bit process running under the WOW64 emulator. When you create a cmd process under the emulator you get a 32 bit cmd process, also running under the emulator. You are comparing that with a 64 bit process. Do note that under the emulator, system32 is redirected to SysWOW64 by the file system redirector.

The way you deal with this is to create a 64 bit process. That's quite tricky to do for cmd when creating from inside the emulator. The easiest way to do so is to call CreateProcess from a 64 bit process.

Since you use Delphi 7 you may need to use a modern compiler to make a small 64 bit executable that will do the work for you. Call the small executable from your Delphi 7 program.

An alternative that may fit your needs is to use the sysnative alias to reach the 64 bit system directory from inside the emulator. That is described in the file system redirector documentation.