Jump to content
amidis

Run in new desktop

Recommended Posts

Hi, I try to build a console app to run an exe in a new Windows desktop. This is my code:

var
  deskHnd : HDESK;
  curDesk : HDESK;
  pstart : STARTUPINFO;
  pinfo : PROCESS_INFORMATION;
  newDesk : string;
begin
  try
    if ParamCount<1 then
    begin
      writeln('Usage: deskrun [exe file]');
      Exit;
    end;
    if not(FileExists(ParamStr(1))) or (LowerCase(ExtractFileExt(ParamStr(1)))<>'.exe') then
    begin
      writeln('Executable not found!');
      Exit;
    end;
    curDesk := GetThreadDesktop(0);

    Randomize;
    newDesk := 'Run-In-New-Desktop-'+IntToStr(Random(MaxInt));
    deskHnd := CreateDesktop(PChar(newDesk),nil,nil,0,GENERIC_ALL,nil);

    ZeroMemory(@pstart, SizeOf(pstart));
    pstart.cb := SizeOf(pstart);
    pstart.lpDesktop := PChar(newDesk);
    CreateProcess(PChar(ParamStr(1)),nil,nil,nil,false,0,nil,nil,pstart,pinfo);
    CloseHandle(pinfo.hThread);
    SwitchDesktop(deskHnd);
    WaitForSingleObject(pinfo.hProcess, INFINITE);
    CloseDesktop(deskHnd);
    SwitchDesktop(curDesk);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

 

The code run ok, it's just an issue which bothers me. Everytime I close the exe run by this program, the desktop does not switch back to original desktop (SwitchDesktop(curDesk)) does not work, even the CloseDesktop(deskHnd) seems like does not working as well. I need to press Ctrl+Alt+Del and click on Cancel for me to return to original desktop. Any idea how to fix this?

Edited by amidis

Share this post


Link to post

If you think the functions are "failing" try testing their return parameter and detect the "getlasterror" to see if there are more precise indications.

  • Like 1

Share this post


Link to post
5 hours ago, amidis said:

The code run ok, it's just an issue which bothers me. Everytime I close the exe run by this program, the desktop does not switch back to original desktop (SwitchDesktop(curDesk)) does not work, even the CloseDesktop(deskHnd) seems like does not working as well. I need to press Ctrl+Alt+Del and click on Cancel for me to return to original desktop. Any idea how to fix this?

Can't reproduce this behavior on Windows 10.0.17134.706 .

Tried many exe's and it worked flawlessly, aka returned to current desktop without a problem, tried some with GUI and console application some finish fast some takes time, everything is OK.

 

One thing though, shouldn't be SwitchDesktop(curDesk) before CloseDesktop(deskHnd), to me this makes more sense instead of leaving it to the OS, also if the exe your are running is locking or using the Desktop it might interact with switching.

 

Now to the guess part : something locked the newly created desktop, something like screen recording or Antivirus, most likely piece of software OS or non-OS based, locked the desktop or ran something on it and prevented desktop closing it, in other words the that desktop was busy.

Observation : if i comment SwitchDesktop(curDesk); then after closing the newly ran process the desktop return automatically to my desktop, this indicate closing desktop worked and switching is even not needed.

Even when i comment both lines Switching and closing, i still get return to my default desktop as this application after WaitForSingleObject does exit, hence auto closing the desktop handle and return me to my default desktop.

 

Share this post


Link to post

:classic_huh: Terrible 😱  ... in Windows 11 23H2 that produce a black screen with application, but when you close the application there is no way (or better, I don't know how) to come back to previous Desktop ... close some process, launch new explorer ... all shortcuts ... only shutdown and restart Windows (from Task Manager -> "New operation") ... also everything I launch is not visible, may be is attached to old Desktop ...

 

This is a good and simple hacking app ...

Share this post


Link to post
17 hours ago, Kas Ob. said:

One thing though, shouldn't be SwitchDesktop(curDesk) before CloseDesktop(deskHnd), to me this makes more sense instead of leaving it to the OS, also if the exe your are running is locking or using the Desktop it might interact with switching.

Will do this, thanks. I think this is an issue with Windows 11, because apparently you didn't face similar problem.

 

15 hours ago, DelphiUdIT said:

:classic_huh: Terrible 😱  ... in Windows 11 23H2 that produce a black screen with application, but when you close the application there is no way (or better, I don't know how) to come back to previous Desktop ... close some process, launch new explorer ... all shortcuts ... only shutdown and restart Windows (from Task Manager -> "New operation") ... also everything I launch is not visible, may be is attached to old Desktop ...

 

This is a good and simple hacking app ...

As I wrote earlier, after closing the app, you need to press Ctrl+Alt+Del to show lock screen, and then click Cancel. You will then return to original desktop. On rare cases though (at least for me) this is failed, you might need to repeat cancelling lock screen a few times, or if you run the exe through a cmd line (not directly from Delphi), you need to kill the cmd task that running the script. Afterward, try repeat the lock screen method again.

Share this post


Link to post
5 hours ago, amidis said:

Will do this, thanks. I think this is an issue with Windows 11, because apparently you didn't face similar problem.

 

As I wrote earlier, after closing the app, you need to press Ctrl+Alt+Del to show lock screen, and then click Cancel. You will then return to original desktop. On rare cases though (at least for me) this is failed, you might need to repeat cancelling lock screen a few times, or if you run the exe through a cmd line (not directly from Delphi), you need to kill the cmd task that running the script. Afterward, try repeat the lock screen method again. 

Uhmm ... you used a wrong words .... for me is "many times", not "few times" :classic_biggrin:, 4 times I went in the lock screen. Like you wrote, something about Windows 11 is wrong. I cannot help 'cause I have not experience in this case. I never used CreateDesktop or similar api.

Good luck and let us knows if you'll resolve the question.

  • Like 1

Share this post


Link to post

The first application i ran was ProcessExplorer https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer ,that way i can see what is going on, also i can do many tasks like kill the Windows Explorer and start it again, this like a rule of thumb for me, as this application most likely went through Microsoft testing also being used by millions, and followed the best practice to the letter.

After it worked fine, and i failed to reproduce your black screen, tried few other application with GUI and console and no problem too.

So try with it for easier get out of locked desktop.

  • Like 1

Share this post


Link to post

Finally found the solution. Apparently, I need to close the handle to the thread and process of the executable run in the new desktop in order to properly terminate the new desktop. So I added

CloseHandle(pinfo.hProcess);
CloseHandle(pinfo.hThread);

After WaitForSingleObject, before switching to original desktop. Case closed 🙂

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×