为什么我不能使用WinExec启动Windows Update控制面板?

时间:2010-05-25 20:29:45

标签: delphi controlpanel

Executing Control Panel Items中,MSDN说:

  

Windows Vista规范名称

     

在Windows Vista及更高版本中,从命令行启动“控制面板”项的首选方法是使用“控制面板”项的规范名称。

根据微软网站的说法,这应该有效:

  

以下示例显示应用程序如何使用WinExec启动“控制面板”项目Windows Update。

WinExec("%systemroot%\system32\control.exe /name Microsoft.WindowsUpdate", SW_NORMAL);

对于Delphi 2010,我尝试过:

var
  CaptionString: string;
  Applet: string;
  Result: integer;
  ParamString: string;
CaptionString := ListviewApplets1.Items.Item[ ListviewApplets1.ItemIndex ].Caption;
if CaptionString = 'Folder Options' then
    { 6DFD7C5C-2451-11d3-A299-00C04F8EF6AF }
    Applet := 'Microsoft.FolderOptions'
  else if CaptionString = 'Fonts' then
    {93412589-74D4-4E4E-AD0E-E0CB621440FD}
    Applet := 'Microsoft.Fonts'
  else if CaptionString = 'Windows Update' then
    { 93412589-74D4-4E4E-AD0E-E0CB621440FD }
    Applet := 'Microsoft.WindowsUpdate'
  else if CaptionString = 'Game Controllers' then
    { 259EF4B1-E6C9-4176-B574-481532C9BCE8 }
    Applet := 'Microsoft.GameControllers'
  else if CaptionString = 'Get Programs' then
    { 15eae92e-f17a-4431-9f28-805e482dafd4 }
    Applet := 'Microsoft.GetPrograms'
//...

ParamString := ( SystemFolder + '\control.exe /name ' ) + Applet;
WinExec( ParamString, SW_NORMAL); <= This does not execute and when I trapped the error it returned ERROR_FILE_NOT_FOUND.

我尝试了一个ExecAndWait(ParamString)方法,它与WinExec使用的相同ParamString完美配合:

ParamString := ( SystemFolder + '\control.exe /name ' ) + Applet;
ExecAndWait( ParamString ); <= This executes and Runs perfectly

我使用的ExecAndWait方法调用Windows.CreateProcess:

if Windows.CreateProcess( nil, PChar( CommandLine ), nil, nil, False, 0, nil, nil, StartupInfo, ProcessInfo ) then
  begin
    try

WinExec是否需要不同的ParamString,或者我是否在使用WinExec时出错?我没有发布完整的ExecAndWait方法,但我可以,如果有人想看到它。

2 个答案:

答案 0 :(得分:3)

@Bill WinExec函数已弃用,

来自MSDN网站的

  

此功能仅供参考   与16位Windows的兼容性。   应用程序应该使用   CreateProcess函数

使用CreateProcess函数

尝试此示例
program ProjectTest;

{$APPTYPE CONSOLE}

uses
  Windows,
  SysUtils;

var
  App        : String;
  Params     : String;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  try
   App    := 'control.exe';
   Params := '/Name Microsoft.GetPrograms';
   FillChar(StartupInfo, SizeOf(StartupInfo), 0);
   StartupInfo.cb := SizeOf(StartupInfo);
   if not CreateProcess(nil, PChar(App+' '+Params), nil, nil, False, 0, nil, nil, StartupInfo, ProcessInfo) then
    RaiseLastOSError;
    //Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

答案 1 :(得分:0)

您是否可以尝试修改ParamString,使其只包含'control.exe / name'+ Applet,然后通过WinExec运行它?