windows 为什么 CreateProcess 给出错误 193(%1 不是有效的 Win32 应用程序)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12637203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:56:13  来源:igfitidea点击:

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

windowsdelphiwinapidelphi-xe2delphi-2007

提问by Jan Doggen

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.

下面的代码无法启动文档。我收到错误 193(%1 不是有效的 Win32 应用程序)。启动可执行文件工作正常。文件正确关联,双击时它们会启动相应的应用程序。我已经在 SO 和其他地方搜索了错误消息、createprocess 内容等(例如,为什么 CreateProcess 在 64 位 Windows Server 2003 中失败?我知道引用命令行。

  • 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.

  • 这是 Win7 64 位 VMWare VM 中的 Delphi XE2(更新 4)Win32 应用程序。

  • 该代码在主机(Win7 64 位)和具有 32 位 XP 的 Virtual PC VM 上也失败。

  • 应在 Win7 VM(Excel 2003 和 Crimson Editor)中启动的应用程序是 32 位的。

  • 从 IDE 启动或独立运行测试应用程序时都会发生故障

  • 它曾经是 Delphi2007 代码,此代码来自的已编译 D2007 应用程序在任何地方都可以正常工作。

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;

回答by David Heffernan

The most likely explanations for that error are:

该错误最可能的解释是:

  1. The file you are attempting to load is not an executable file. CreateProcessrequires you to provide an executable file. If you wish to be able to open any file with its associated application then you need ShellExecuterather 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'sprofile mode to check exactly what is going wrong.
  1. 您尝试加载的文件不是可执行文件。CreateProcess要求您提供一个可执行文件。如果您希望能够使用其关联的应用程序打开任何文件,那么您需要ShellExecute而不是CreateProcess.
  2. 加载可执行文件的依赖项之一时出现问题,即链接到可执行文件的 DLL。最常见的原因是 32 位可执行文件和 64 位 DLL 之间不匹配,反之亦然。要进行调查,请使用Dependency Walker 的配置文件模式来准确检查出了什么问题。

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

仔细阅读代码的底部,我可以看到问题是第 1 点。

回答by Frerich Raabe

Your Button2Clickand Button3Clickfunctions pass klad.xlsand smimime.txt. These files most likely aren't actual executables indeed.

您的Button2ClickandButton3Click函数通过klad.xlsand smimime.txt。这些文件很可能确实不是实际的可执行文件。

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

要使用与其关联的应用程序打开任意文件,请使用 ShellExecute

回答by Eugene

Let me add an example here:

让我在这里添加一个例子:

I'm trying to build Alluxioon windows platform and got the same issue, it's because the pom.xmlcontains below step:

我正在尝试Alluxio在 Windows 平台上构建并遇到相同的问题,这是因为pom.xml包含以下步骤:

      <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 .shfile is not executable on windows so the error throws.

.sh文件在 Windows 上不可执行,因此会引发错误。

Comment it out if you do want build Alluxioon windows.

如果您确实想Alluxio在 Windows 上构建,请将其注释掉。

回答by Bogdan Dvorianov

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

如果您是 Clion/anyOtherJetBrainsIDE 用户,并且yourFile.exe导致此问题,只需将其删除并让应用程序从头开始创建并将其与库链接。它有助于。