2 * Unit tests for window stations and desktops
4 * Copyright 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "wine/test.h"
26 #define DESKTOP_ALL_ACCESS 0x01ff
28 static void print_object( HANDLE obj )
33 strcpy( buffer, "foobar" );
34 if (!GetUserObjectInformationA( obj, UOI_NAME, buffer, sizeof(buffer), &size ))
35 trace( "could not get info for %p\n", obj );
37 trace( "obj %p name '%s'\n", obj, buffer );
38 strcpy( buffer, "foobar" );
39 if (!GetUserObjectInformationA( obj, UOI_TYPE, buffer, sizeof(buffer), &size ))
40 trace( "could not get type for %p\n", obj );
42 trace( "obj %p type '%s'\n", obj, buffer );
45 static HDESK initial_desktop;
47 static DWORD CALLBACK thread( LPVOID arg )
50 HWND hwnd = CreateWindowExA(0,"BUTTON","test",WS_POPUP,0,0,100,100,GetDesktopWindow(),0,0,0);
51 ok( hwnd != 0, "CreateWindow failed\n" );
52 d1 = GetThreadDesktop(GetCurrentThreadId());
53 trace( "thread %p desktop: %p\n", arg, d1 );
54 ok( d1 == initial_desktop, "thread %p doesn't use initial desktop\n", arg );
56 SetLastError( 0xdeadbeef );
57 ok( !CloseHandle( d1 ), "CloseHandle succeeded\n" );
58 ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %ld\n", GetLastError() );
59 SetLastError( 0xdeadbeef );
60 ok( !CloseDesktop( d1 ), "CloseDesktop succeeded\n" );
61 ok( GetLastError() == ERROR_BUSY, "bad last error %ld\n", GetLastError() );
63 d2 = CreateDesktop( "foobar2", NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
64 trace( "created desktop %p\n", d2 );
65 ok( d2 != 0, "CreateDesktop failed\n" );
69 SetLastError( 0xdeadbeef );
70 ok( !SetThreadDesktop( d2 ), "set thread desktop succeeded with existing window\n" );
71 ok( GetLastError() == ERROR_BUSY, "bad last error %ld\n", GetLastError() );
74 DestroyWindow( hwnd );
75 ok( SetThreadDesktop( d2 ), "set thread desktop failed\n" );
76 d1 = GetThreadDesktop(GetCurrentThreadId());
77 ok( d1 == d2, "GetThreadDesktop did not return set desktop %p/%p\n", d1, d2 );
81 HANDLE hthread = CreateThread( NULL, 0, thread, (char *)arg + 1, 0, NULL );
83 WaitForSingleObject( hthread, INFINITE );
84 CloseHandle( hthread );
89 static void test_handles(void)
98 w1 = GetProcessWindowStation();
99 ok( GetProcessWindowStation() == w1, "GetProcessWindowStation returned different handles\n" );
100 ok( !CloseWindowStation(w1), "closing process win station succeeded\n" );
101 SetLastError( 0xdeadbeef );
102 ok( !CloseHandle(w1), "closing process win station handle succeeded\n" );
103 ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %ld\n", GetLastError() );
107 ok( GetHandleInformation( w1, &flags ), "GetHandleInformation failed\n" );
108 ok( !(flags & HANDLE_FLAG_PROTECT_FROM_CLOSE), "handle %p PROTECT_FROM_CLOSE set\n", w1 );
110 ok( DuplicateHandle( GetCurrentProcess(), w1, GetCurrentProcess(), (PHANDLE)&w2, 0,
111 TRUE, DUPLICATE_SAME_ACCESS ), "DuplicateHandle failed\n" );
112 ok( CloseWindowStation(w2), "closing dup win station failed\n" );
114 ok( DuplicateHandle( GetCurrentProcess(), w1, GetCurrentProcess(), (PHANDLE)&w2, 0,
115 TRUE, DUPLICATE_SAME_ACCESS ), "DuplicateHandle failed\n" );
116 ok( CloseHandle(w2), "closing dup win station handle failed\n" );
118 w2 = CreateWindowStation("WinSta0", 0, WINSTA_ALL_ACCESS, NULL );
119 ok( w2 != 0, "CreateWindowStation failed\n" );
120 ok( w2 != w1, "CreateWindowStation returned default handle\n" );
121 SetLastError( 0xdeadbeef );
122 ok( !CloseDesktop( (HDESK)w2 ), "CloseDesktop succeeded on win station\n" );
123 ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %ld\n", GetLastError() );
124 ok( CloseWindowStation( w2 ), "CloseWindowStation failed\n" );
126 w2 = CreateWindowStation("WinSta0", 0, WINSTA_ALL_ACCESS, NULL );
127 ok( CloseHandle( w2 ), "CloseHandle failed\n" );
129 w2 = OpenWindowStation("winsta0", TRUE, WINSTA_ALL_ACCESS );
130 ok( w2 != 0, "OpenWindowStation failed\n" );
131 ok( w2 != w1, "OpenWindowStation returned default handle\n" );
132 ok( CloseWindowStation( w2 ), "CloseWindowStation failed\n" );
134 w2 = OpenWindowStation("dummy name", TRUE, WINSTA_ALL_ACCESS );
135 ok( !w2, "open dummy win station succeeded\n" );
137 CreateMutexA( NULL, 0, "foobar" );
138 w2 = CreateWindowStation("foobar", 0, WINSTA_ALL_ACCESS, NULL );
139 ok( w2 != 0, "create foobar station failed\n" );
141 w3 = OpenWindowStation("foobar", TRUE, WINSTA_ALL_ACCESS );
142 ok( w3 != 0, "open foobar station failed\n" );
143 ok( w3 != w2, "open foobar station returned same handle\n" );
144 ok( CloseWindowStation( w2 ), "CloseWindowStation failed\n" );
145 ok( CloseWindowStation( w3 ), "CloseWindowStation failed\n" );
147 w3 = OpenWindowStation("foobar", TRUE, WINSTA_ALL_ACCESS );
148 ok( !w3, "open foobar station succeeded\n" );
151 d1 = GetThreadDesktop(GetCurrentThreadId());
152 initial_desktop = d1;
153 ok( GetThreadDesktop(GetCurrentThreadId()) == d1,
154 "GetThreadDesktop returned different handles\n" );
157 ok( GetHandleInformation( d1, &flags ), "GetHandleInformation failed\n" );
158 ok( !(flags & HANDLE_FLAG_PROTECT_FROM_CLOSE), "handle %p PROTECT_FROM_CLOSE set\n", d1 );
160 SetLastError( 0xdeadbeef );
161 ok( !CloseDesktop(d1), "closing thread desktop succeeded\n" );
162 ok( GetLastError() == ERROR_BUSY, "bad last error %ld\n", GetLastError() );
164 SetLastError( 0xdeadbeef );
165 ok( !CloseHandle(d1), "closing thread desktop handle failed\n" );
166 ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %ld\n", GetLastError() );
168 ok( DuplicateHandle( GetCurrentProcess(), d1, GetCurrentProcess(), (PHANDLE)&d2, 0,
169 TRUE, DUPLICATE_SAME_ACCESS ), "DuplicateHandle failed\n" );
170 ok( CloseDesktop(d2), "closing dup desktop failed\n" );
172 ok( DuplicateHandle( GetCurrentProcess(), d1, GetCurrentProcess(), (PHANDLE)&d2, 0,
173 TRUE, DUPLICATE_SAME_ACCESS ), "DuplicateHandle failed\n" );
174 ok( CloseHandle(d2), "closing dup desktop handle failed\n" );
176 d2 = OpenDesktop( "dummy name", 0, TRUE, DESKTOP_ALL_ACCESS );
177 ok( !d2, "open dummy desktop succeeded\n" );
179 d2 = CreateDesktop( "foobar", NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL );
180 ok( d2 != 0, "create foobar desktop failed\n" );
181 SetLastError( 0xdeadbeef );
182 ok( !CloseWindowStation( (HWINSTA)d2 ), "CloseWindowStation succeeded on desktop\n" );
183 ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %ld\n", GetLastError() );
185 d3 = OpenDesktop( "foobar", 0, TRUE, DESKTOP_ALL_ACCESS );
186 ok( d3 != 0, "open foobar desktop failed\n" );
187 ok( d3 != d2, "open foobar desktop returned same handle\n" );
188 ok( CloseDesktop( d2 ), "CloseDesktop failed\n" );
189 ok( CloseDesktop( d3 ), "CloseDesktop failed\n" );
191 d3 = OpenDesktop( "foobar", 0, TRUE, DESKTOP_ALL_ACCESS );
192 ok( !d3, "open foobar desktop succeeded\n" );
194 ok( !CloseHandle(d1), "closing thread desktop handle succeeded\n" );
195 d2 = GetThreadDesktop(GetCurrentThreadId());
196 ok( d1 == d2, "got different handles after close\n" );
198 trace( "thread 1 desktop: %p\n", d1 );
200 hthread = CreateThread( NULL, 0, thread, (LPVOID)2, 0, &id );
202 trace( "get other thread desktop: %p\n", GetThreadDesktop(id) );
203 WaitForSingleObject( hthread, INFINITE );
204 CloseHandle( hthread );
207 START_TEST(winstation)