如何使用ShellExecute在'config'模式下运行屏幕保护程序?操作系统会覆盖我的ShellExecute调用

如何使用ShellExecute在'config'模式下运行屏幕保护程序?操作系统会覆盖我的ShellExecute调用

问题描述:

我想用ShellExec在'config'模式下运行一个屏幕保护程序。我用这个(德尔福)电话:如何使用ShellExecute在'config'模式下运行屏幕保护程序?操作系统会覆盖我的ShellExecute调用

i:= ShellExecute(0, 'open', PChar('c:\temp\test.scr'), PChar('/c'), NIL, SW_SHOWNORMAL) 

然而,由SCR文件中收到的参数是“/ S”,这样的地方在道路上的Windows拦截我的电话,并取代我的参数与“/ S”。


更新
我做了一个实验:
我建立一个应用程序(mytest.exe),显示参数。我用/ c作为参数启动mytest.exe。/c参数被正确接收。
然后我将mytest.exe重命名为mytest.scr。现在发送的参数被操作系统覆盖。收到的参数现在是'/ S'。

有趣!

脏修复:执行CMD文件,执行/ c模式下的屏幕保护程序工作!

+0

这是错误的方式启动一个进程。正确的功能是CreateProcess –

+0

是的。我知道。 (ShellExecuteEx也是比ShellExecute更好的选项)。但我需要一个快速(这样,肮脏也是可以接受的)方法来解决这个问题(我有一个完整的生态系统构建了一个ShellExecute)。 - 或者你认为调用CreateProcess会解决这个问题?无论如何,我会在未来的几天里用CreateProcess替换ShellExecute。 – Ampere

+0

CreateProcess始终是启动可执行文件的方式。你应该总是这样做。不要让外壳把你拧过来。 –

如果您在注册表中查找,您将看到open动词为.SCR文件扩展名注册调用文件与/S参数默认为:

image

所以,你/c参数被忽略。

如果你想调用.scr文件的配置屏幕,使用config动词,而不是open

image

ShellExecute(0, 'config', PChar('c:\temp\test.scr'), nil, nil, SW_SHOWNORMAL); 

运行.scr文件不带任何参数是类似于运行它根据文档,/c参数仅仅没有前景形式:

INFO: Screen Saver Command Line Arguments

 
    ScreenSaver   - Show the Settings dialog box. 
    ScreenSaver /c  - Show the Settings dialog box, modal to the 
          foreground window. 
    ScreenSaver /p <HWND> - Preview Screen Saver as child of window <HWND>. 
    ScreenSaver /s  - Run the Screen Saver. 

否则,运行.scr文件,CreateProcess()而不是ShellExecute()这样你就可以直接指定/c参数:

var 
    Cmd: string; 
    SI: TStartupInfo; 
    PI: TProcessInformation; 
begin 
    Cmd := 'c:\temp\test.scr /c'; 
    UniqueString(Cmd); 

    ZeroMemory(@SI, SizeOf(SI)); 
    SI.cb := SizeOf(SI); 
    SI.dwFlags := STARTF_USESHOWWINDOW; 
    SI.wShowWindow := SW_SHOWNORMAL; 

    if CreateProcess(nil, PChar(Cmd), nil, nil, False, 0, nil, nil, SI, PI) then 
    begin 
    CloseHandle(PI.hThread); 
    CloseHandle(PI.hProcess); 
    end; 
end; 
+0

很多感谢您的完整答案! – Ampere