您好我有一个函数使得CreateProcess加载程序在这种情况下尝试上传文件的路径为c:/test.mp3 c:/test.exe,我尝试加载我的程序控制台但从未加载程序
代码:
program test;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, ShellApi, Windows;
procedure cargar(ProgramName: String; Wait: Boolean);
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CreateOK: Boolean;
begin
{ fill with known state }
FillChar(StartInfo, SizeOf(TStartupInfo), #0);
FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
StartInfo.cb := SizeOf(TStartupInfo);
CreateOK := CreateProcess(nil, PChar(ProgramName), nil, nil, False,
CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil, nil, StartInfo,
ProcInfo);
{ check to see if successful }
if CreateOK then
begin
// may or may not be needed. Usually wait for child processes
if Wait then
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end
else
begin
// ShowMessage('Unable to run '+ProgramName);
end;
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;
begin
try
cargar('c:/test.mp3', True);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
我做错了什么?
答案 0 :(得分:2)
如果你实现错误检查(例如通过在失败时调用RaiseLastOSError),那么Windows将告诉你出了什么问题:'系统错误。代码:193。%1不是有效的Win32应用程序'。
您不能使用CreateProcess直接启动mp3文件。您必须使用mp3文件作为命令行参数启动播放器应用程序。