-2

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
Community
  • 1
  • 1
user508402
  • 496
  • 1
  • 4
  • 19
  • 5
    It would be great if we could stop calling console apps DOS programs. There is no DOS anymore. – David Heffernan Nov 15 '14 at 12:03
  • Or rather, DOS does still exist, but most of what is called DOS really isn't. –  Nov 15 '14 at 12:05
  • 1
    The solution is simple, use the [Sysnative](http://www.samlogic.net/articles/sysnative-folder-64-bit-windows.htm) folder instead of the system32 directory... – whosrdaddy Nov 15 '14 at 12:46
  • As I understand it, SysNative wasn't introduced before Vista. The target machine runs server 2003x64. – user508402 Nov 15 '14 at 13:03
  • I believe there was a hotfix for 2003/XP, google it... – whosrdaddy Nov 15 '14 at 13:08
  • You must be getting different search results than I do... – user508402 Nov 15 '14 at 13:25
  • @whosrdaddy I'm not so sure that will work out. Can be tricky. – David Heffernan Nov 15 '14 at 14:02
  • [Not so hard to find](http://support2.microsoft.com/kb/942589)? @DavidHeffernan, never had problems with SysNative... – whosrdaddy Nov 15 '14 at 15:11
  • @who It's fine for reading/writing files. I just remember have difficulties using it to escape the emulator. – David Heffernan Nov 15 '14 at 15:55
  • 1
    Yes, prompted by your perseverance I searched further. At first I only found more statements that it wouldn't be possible in this version, by then I arrived at the same kb-page you have posted while I was away. Honour is due: it works as a charm. I cannot be tested through the CLI, as it seems, for then WOW64 is not involved. But calling cmd.exe as "C:\windows\Sysnative\cmd.exe" does the trick. – user508402 Nov 15 '14 at 17:19

1 Answers1

3

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490