Page 1 of 1

Virtual Desktops

Posted: Mon May 01, 2006 8:05 pm
by Num3
I've stripped this from a Delphi complex source code.

It works in Delphi, but not in PB, can anyone help !?

Code: Select all

DesktopName.s="test 1"

Desk=CreateDesktop_(DesktopName, 0, 0, #DF_ALLOWOTHERACCOUNTHOOK, #MAXIMUM_ALLOWED, 0)
Debug Desk
Debug SwitchDesktop_(Desk)

Posted: Mon May 01, 2006 8:26 pm
by Trond
The CreateDesktop function creates a new desktop on the window station associated with the calling process.
Before calling CreateDesktop_() you need to associate a window station with your process. This is very easy if you've got the handle to one, just do this:

Code: Select all

SetProcessWindowStation(hWinSta)
But first you need the handle to a window station. This is almost tricky, but not really, since I tell you that the name of the standard window station is WinSta0.

Code: Select all

hWinSta = OpenWindowStation_("WinSta0", 0, #WINSTA_ALL)
(
Here's how to get all window stations in the system:

Code: Select all

Procedure EnumWindowStationProc(Name.s, lParam.l)
  Debug Name
  ProcedureReturn 1
EndProcedure

EnumWindowStations_(@EnumWindowStationProc(), 0)
)[/color]

It is now pretty straightforward to create a new desktop and switch to it:

Code: Select all

#DESKTOP_ALL = #DESKTOP_WRITEOBJECTS | #DESKTOP_SWITCHDESKTOP | #DESKTOP_READOBJECTS | #DESKTOP_JOURNALRECORD | #DESKTOP_JOURNALPLAYBACK | #DESKTOP_HOOKCONTROL | #DESKTOP_ENUMERATE | #DESKTOP_CREATEWINDOW | #DESKTOP_CREATEMENU
hDesk = CreateDesktop_("Kool Desktop 2", 0, 0, #DF_ALLOWOTHERACCOUNTHOOK, #DESKTOP_ALL, 0)
SwitchDesktop_(hDesk)
Then a little delay:

Code: Select all

Delay(2000)
Just one "little" problem: How do we get back?? First we need the handle to the standard desktop. This one's named "Default".

Code: Select all

hDefaultDesk = OpenDesktop_("Default", #DF_ALLOWOTHERACCOUNTHOOK, 0, #DESKTOP_SWITCHDESKTOP)
(
See all desktops in a window station:

Code: Select all

Procedure EnumDesktopProc(Name.s, lParam.l)
  Debug Name
  ProcedureReturn 1
EndProcedure
hWinSta = OpenWindowStation_("WinSta0", 0, #WINSTA_ENUMDESKTOPS)
EnumDesktops_(hWinSta, @EnumDesktopProc(), 0)
CloseWindowStation_(hWinSta)
)[/color]

Then we switch back:

Code: Select all

SwitchDesktop_(hDefaultDesk)
And clean up after us (important):

Code: Select all

CloseDesktop_(hDesk) ; This DELETES the desktop we created
CloseDesktop_(hDefaultDesk) ; This closes the handle to the default desktop
CloseWindowStation_(hWinSta) ; This closes the handle to the window station.
Full code:

Code: Select all

#DESKTOP_ALL = #DESKTOP_WRITEOBJECTS | #DESKTOP_SWITCHDESKTOP | #DESKTOP_READOBJECTS | #DESKTOP_JOURNALRECORD | #DESKTOP_JOURNALPLAYBACK | #DESKTOP_HOOKCONTROL | #DESKTOP_ENUMERATE | #DESKTOP_CREATEWINDOW | #DESKTOP_CREATEMENU

;Show an error
Procedure Abort(s.s)
  MessageRequester("", "Error: "+s.s)
  End
EndProcedure

;Check a result and ev. abort
Procedure Chk(a.l, s.s)
  ;If a is false, abort.
  ;If a is false and s contains an error message, show it before abort.
  If Not(a)
    If s.s
      Abort(s.s)
    Else
      End
    EndIf
  EndIf
EndProcedure

hWinSta = OpenWindowStation_("WinSta0", 0, #WINSTA_ALL)
Chk( SetProcessWindowStation_(hWinSta) , "Failed to set window station")

hDefaultDesk = OpenDesktop_("Default", #DF_ALLOWOTHERACCOUNTHOOK, 0, #DESKTOP_SWITCHDESKTOP)
Chk(hDefaultDesk, "Failed to open default desktop")
hDesk = CreateDesktop_("My Desktop 2", 0, 0, #DF_ALLOWOTHERACCOUNTHOOK, #DESKTOP_ALL, 0)
Chk( hDesk, "Failed to create desktop")
Chk( SwitchDesktop_(hDesk), "Failed to switch desktop")

Delay(2000)

Chk( SwitchDesktop_(hDefaultDesk), "Failed to switch desktop")

CloseDesktop_(hDesk)
CloseDesktop_(hDefaultDesk)
CloseWindowStation_(hWinSta)

Posted: Mon May 01, 2006 8:31 pm
by Num3
Duh!

Exactly! I was missing the window station .

Thanks!

---EDIT---

Too weird!

Your code works great, but no new desktop appear, only windows background and mouse in wait...

After the 2 secs, back to normal :lol:

Posted: Mon May 01, 2006 9:06 pm
by dracflamloc
Nice avatar :lol:

Posted: Mon May 01, 2006 9:08 pm
by fsw
Num3 wrote:... only windows background and mouse in wait...
May I introduce: This it your new desktop :shock:

And yes, it's totally empty :D

Posted: Mon May 01, 2006 9:11 pm
by Trond
fsw wrote:
Num3 wrote:... only windows background and mouse in wait...
May I introduce: This it your new desktop :shock:

And yes, it's totally empty :D
+1

Posted: Mon May 01, 2006 9:15 pm
by Num3
Too weird, if i run the Vdesktop program and after that the PB code it works :shock:

Num3 Slaps MS and drack :P

Posted: Tue May 02, 2006 2:30 am
by va!n
you missed to define the constant, so here we go... :wink:

Code: Select all

#WINSTA_ALL = #WINSTA_ACCESSCLIPBOARD | #WINSTA_ACCESSGLOBALATOMS | #WINSTA_CREATEDESKTOP | #WINSTA_ENUMDESKTOPS | #WINSTA_ENUMERATE | #WINSTA_EXITWINDOWS | #WINSTA_READATTRIBUTES | #WINSTA_READSCREEN | #WINSTA_WRITEATTRIBUTES | #DELETE | #READ_CONTROL | #WRITE_DAC | #WRITE_OWNER

Posted: Fri Sep 01, 2006 10:18 pm
by PWS32
howto create a window on the empty desktop?