Search code examples
windowswinapicreateprocessscreensaverwindows-screensaver

CreateProcess from within Screen Saver


I need to start "explorer.exe" from within my screensaver, but whatever I tried it wont stay opened when my screen saver closes.

CreateProcess(nil, 'explorer.exe /select,"' + fileName + '"', nil, nil, false, 0, nil, '', StartupInfo, ProcessInfo)

I tried multitude of combination of flags and parameters, no luck :) Explorer starts briefly and closes with my screensaver app...

There is a solution in here but it requires windows service and that is too much for a simple screen saver...

Anyone got a clue?

Thanks!


Solution

  • To answer myself, I create a Windows Task with current logged in user to start in 2sec after ss closes. And it works great.

    Here is the command for creating the task:

    powershell -command "& {Register-ScheduledTask -Action $(New-ScheduledTaskAction -Execute "explorer.exe" -Argument '/select,"d:/test/txt" ') -Trigger $(New-ScheduledTaskTrigger -Once -At (Get-Date).AddSeconds(2)) -TaskName "MyTask" -TaskPath "MyTasks" -Principal $(New-ScheduledTaskPrincipal -UserId $(Get-CimInstance –ClassName Win32_ComputerSystem | Select-Object -expand UserName))}"
    

    and dont forget to delete the task before create the new one - or the create fails

    powershell -command "& {Unregister-ScheduledTask -TaskName "MyTask" -Confirm:$false}"