gpt4 book ai didi

delphi - 为什么无法使用 WinExec 启动 Windows Update 控制面板?

转载 作者:行者123 更新时间:2023-12-02 03:56:27 25 4
gpt4 key购买 nike

Executing Control Panel Items ,MSDN 是这样说的:

Windows Vista Canonical Names

In Windows Vista and later, the preferred method of launching a Control Panel item from a command line is to use the Control Panel item's canonical name.

根据 Microsoft 网站,这应该有效:

The following example shows how an application can start the Control Panel item Windows Update with WinExec.

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 网站

this function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function

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

关于delphi - 为什么无法使用 WinExec 启动 Windows Update 控制面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2908288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com