[windows] Why does CreateProcess give error 193 (%1 is not a valid Win32 app)

The code below fails to start documents. I get error 193 (%1 is not a valid Win32 app). Starting executables work fine. The files are properly associated, they start the corresponding app when double clicked. I have searched SO and elsewhere for the error message, createprocess stuff etc. (E.g. Why is CreateProcess failing in Windows Server 2003 64-bit? I know about quoting the command line.

  • This is a Delphi XE2 (Update 4) Win32 app in a Win7 64bit VMWare VM.

  • The code also fails on the host machine (Win7 64 bit) and in a Virtual PC VM with 32bit XP.

  • The apps that should start in the Win7 VM (Excel 2003 and Crimson Editor) are 32 bit.

  • The failure occurs both when starting from the IDE or when running the test app standalone

  • It used to be Delphi2007 code, the compiled D2007 app where this code comes from works fine everywhere.

What's wrong with the code? It's almost as if I'm overlooking something very obvious....

Thanks in advance,

Jan

procedure StartProcess(WorkDir, Filename: string; Arguments : string = '');
var
  StartupInfo  : TStartupInfo;
  ProcessInfo  : TProcessInformation;
  lCmd         : string;
  lOK          : Boolean;
  LastErrorCode: Integer;
begin
  FillChar( StartupInfo, SizeOf( TStartupInfo ), 0 );
  StartupInfo.cb := SizeOf( TStartupInfo );
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := sw_Normal;

  FillChar( ProcessInfo, SizeOf( TProcessInformation ), 0 );

  lCmd := '"' +  WorkDir + FileName + '"';     // Quotes are needed https://stackoverflow.com/questions/265650/paths-and-createprocess
  if Arguments <> '' then lCmd := lCmd + ' ' + Arguments;

  lOk := CreateProcess(nil,
                       PChar(lCmd),
                       nil,
                       nil,
                       FALSE,  // TRUE makes no difference
                       0,      // e.g. CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS makes no difference
                       nil,
                       nil,    // PChar(WorkDir) makes no difference
                       StartupInfo,
                       ProcessInfo);

  if lOk then
  begin
    try
      WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
    finally
      CloseHandle( ProcessInfo.hThread );
      CloseHandle( ProcessInfo.hProcess );
    end;
  end
  else
  begin
    LastErrorCode := GetLastError;
    ShowMessage(IntToStr(LastErrorCode) + ': ' + SysErrorMessage(LastErrorCode));
  end;
end;

procedure TFrmStartProcess.Button1Click(Sender: TObject);
begin
   StartProcess('c:\program files (x86)\axe3\','axe.exe');    // Works
end;

procedure TFrmStartProcess.Button2Click(Sender: TObject);
begin
   StartProcess('d:\','klad.xls');                            // Fails
end;

procedure TFrmStartProcess.Button3Click(Sender: TObject);
begin
   StartProcess('d:\','smimime.txt');                         // Fails
end;

This question is related to windows delphi winapi delphi-xe2 delphi-2007

The answer is


The most likely explanations for that error are:

  1. The file you are attempting to load is not an executable file. CreateProcess requires you to provide an executable file. If you wish to be able to open any file with its associated application then you need ShellExecute rather than CreateProcess.
  2. There is a problem loading one of the dependencies of the executable, i.e. the DLLs that are linked to the executable. The most common reason for that is a mismatch between a 32 bit executable and a 64 bit DLL, or vice versa. To investigate, use Dependency Walker's profile mode to check exactly what is going wrong.

Reading down to the bottom of the code, I can see that the problem is number 1.


Your Button2Click and Button3Click functions pass klad.xls and smimime.txt. These files most likely aren't actual executables indeed.

In order to open arbitrary files using the application associated with them, use ShellExecute


Let me add an example here:

I'm trying to build Alluxio on windows platform and got the same issue, it's because the pom.xml contains below step:

      <plugin>
        <artifactId>exec-maven-plugin</artifactId>
        <groupId>org.codehaus.mojo</groupId>
        <inherited>false</inherited>
        <executions>
          <execution>
            <id>Check that there are no Windows line endings</id>
            <phase>compile</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>${build.path}/style/check_no_windows_line_endings.sh</executable>
            </configuration>
          </execution>
        </executions>
      </plugin>

The .sh file is not executable on windows so the error throws.

Comment it out if you do want build Alluxio on windows.


If you are Clion/anyOtherJetBrainsIDE user, and yourFile.exe cause this problem, just delete it and let the app create and link it with libs from a scratch. It helps.


Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to delphi

What do I do when my program crashes with exception 0xc0000005 at address 0? Why does CreateProcess give error 193 (%1 is not a valid Win32 app) The application was unable to start correctly (0xc000007b) Indentation shortcuts in Visual Studio Split a string into an array of strings based on a delimiter How to track down access violation "at address 00000000" Is there a program to decompile Delphi? How do I include a newline character in a string in Delphi?

Examples related to winapi

ImportError: no module named win32api Why does CreateProcess give error 193 (%1 is not a valid Win32 app) Dynamically load a function from a DLL How to check if directory exist using C++ and winAPI How to convert char* to wchar_t*? Get current cursor position Check whether a path is valid How do I link to a library with Code::Blocks? Where to find the win32api module for Python? Cannot open include file 'afxres.h' in VC2010 Express

Examples related to delphi-xe2

Why does CreateProcess give error 193 (%1 is not a valid Win32 app)

Examples related to delphi-2007

Why does CreateProcess give error 193 (%1 is not a valid Win32 app)