【问题标题】:Why can't I start the Windows Update control panel with WinExec?为什么我无法使用 WinExec 启动 Windows Update 控制面板?
【发布时间】:2010-05-25 20:29:45
【问题描述】:

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 方法,但如果有人想看,我可以发布。

【问题讨论】:

  • 为什么要使用 WinExec()?这是一个旧的,已弃用的功能。 MSDN 说“注意此功能仅用于与 16 位 Windows 兼容。”
  • @ldsandon,他使用了 WinExec,因为这是 MSDN 中的示例代码使用的。

标签: delphi controlpanel


【解决方案1】:

@Bill 的WinExec 函数已弃用,

来自 MSDN 网站

此功能仅提供给 与 16 位 Windows 的兼容性。 应用程序应使用 创建进程函数

使用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.

【讨论】:

  • 是的,谢谢。我不知道 WinExec 已贬值。我原以为微软会保持最新状态,但我想不会。
  • Deprecated 表示该功能很可能不是最新的。
  • WinExec 本身在 Delphi 中并未标记为已弃用。它应该。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2018-05-24
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 2011-01-14
    • 2020-11-16
    • 2017-10-21
    相关资源
    最近更新 更多