Git conversion: Make reactos the root directory, move rosapps, rostests, wallpapers...
[reactos.git] / modules / rostests / winetests / user32 / class.c
1 /* Unit test suite for window classes.
2 *
3 * Copyright 2002 Mike McCormack
4 * Copyright 2003 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 /* To get CS_DROPSHADOW with the MSVC headers */
22 //#define _WIN32_WINNT 0x0501
23
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #include "wine/test.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
32 #include "winreg.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "commctrl.h"
36
37 #define NUMCLASSWORDS 4
38
39 #define IS_WNDPROC_HANDLE(x) (((ULONG_PTR)(x) >> 16) == (~0u >> 16))
40
41 static LRESULT WINAPI ClassTest_WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
42 {
43 if (msg == WM_NCCREATE) return 1;
44 return DefWindowProcW (hWnd, msg, wParam, lParam);
45 }
46
47 static LRESULT WINAPI ClassTest_WndProc2 (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
48 {
49 if (msg == WM_NCCREATE) return 1;
50 return DefWindowProcA (hWnd, msg, wParam, lParam);
51 }
52
53 /***********************************************************************
54 */
55 static void ClassTest(HINSTANCE hInstance, BOOL global)
56 {
57 WNDCLASSW cls, wc;
58 static const WCHAR className[] = {'T','e','s','t','C','l','a','s','s',0};
59 static const WCHAR winName[] = {'W','i','n','C','l','a','s','s','T','e','s','t',0};
60 ATOM test_atom;
61 HWND hTestWnd;
62 LONG i;
63 WCHAR str[20];
64 ATOM classatom;
65
66 cls.style = CS_HREDRAW | CS_VREDRAW | (global?CS_GLOBALCLASS:0);
67 cls.lpfnWndProc = ClassTest_WndProc;
68 cls.cbClsExtra = NUMCLASSWORDS*sizeof(DWORD);
69 cls.cbWndExtra = 12;
70 cls.hInstance = hInstance;
71 cls.hIcon = LoadIconW (0, (LPWSTR)IDI_APPLICATION);
72 cls.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
73 cls.hbrBackground = GetStockObject (WHITE_BRUSH);
74 cls.lpszMenuName = 0;
75 cls.lpszClassName = className;
76
77 classatom=RegisterClassW(&cls);
78 if (!classatom && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
79 return;
80 ok(classatom, "failed to register class\n");
81
82 ok(!RegisterClassW (&cls),
83 "RegisterClass of the same class should fail for the second time\n");
84
85 /* Setup windows */
86 hTestWnd = CreateWindowW (className, winName,
87 WS_OVERLAPPEDWINDOW + WS_HSCROLL + WS_VSCROLL,
88 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0,
89 0, hInstance, 0);
90
91 ok(hTestWnd!=0, "Failed to create window\n");
92
93 /* test initial values of valid classwords */
94 for(i=0; i<NUMCLASSWORDS; i++)
95 {
96 SetLastError(0);
97 ok(!GetClassLongW(hTestWnd,i*sizeof (DWORD)),
98 "GetClassLongW initial value nonzero!\n");
99 ok(!GetLastError(),
100 "GetClassLongW failed!\n");
101 }
102
103 if (0)
104 {
105 /*
106 * GetClassLongW(hTestWnd, NUMCLASSWORDS*sizeof(DWORD))
107 * does not fail on Win 98, though MSDN says it should
108 */
109 SetLastError(0);
110 GetClassLongW(hTestWnd, NUMCLASSWORDS*sizeof(DWORD));
111 ok(GetLastError(),
112 "GetClassLongW() with invalid offset did not fail\n");
113 }
114
115 /* set values of valid class words */
116 for(i=0; i<NUMCLASSWORDS; i++)
117 {
118 SetLastError(0);
119 ok(!SetClassLongW(hTestWnd,i*sizeof(DWORD),i+1),
120 "GetClassLongW(%d) initial value nonzero!\n",i);
121 ok(!GetLastError(),
122 "SetClassLongW(%d) failed!\n",i);
123 }
124
125 /* test values of valid classwords that we set */
126 for(i=0; i<NUMCLASSWORDS; i++)
127 {
128 SetLastError(0);
129 ok( (i+1) == GetClassLongW(hTestWnd,i*sizeof (DWORD)),
130 "GetClassLongW value doesn't match what was set!\n");
131 ok(!GetLastError(),
132 "GetClassLongW failed!\n");
133 }
134
135 /* check GetClassName */
136 i = GetClassNameW(hTestWnd, str, sizeof(str)/sizeof(str[0]));
137 ok(i == lstrlenW(className),
138 "GetClassName returned incorrect length\n");
139 ok(!lstrcmpW(className,str),
140 "GetClassName returned incorrect name for this window's class\n");
141
142 /* check GetClassInfo with our hInstance */
143 if((test_atom = GetClassInfoW(hInstance, str, &wc)))
144 {
145 ok(test_atom == classatom,
146 "class atom did not match\n");
147 ok(wc.cbClsExtra == cls.cbClsExtra,
148 "cbClsExtra did not match\n");
149 ok(wc.cbWndExtra == cls.cbWndExtra,
150 "cbWndExtra did not match\n");
151 ok(wc.hbrBackground == cls.hbrBackground,
152 "hbrBackground did not match\n");
153 ok(wc.hCursor== cls.hCursor,
154 "hCursor did not match\n");
155 ok(wc.hInstance== cls.hInstance,
156 "hInstance did not match\n");
157 }
158 else
159 ok(FALSE,"GetClassInfo (hinstance) failed!\n");
160
161 /* check GetClassInfo with zero hInstance */
162 if(global)
163 {
164 if((test_atom = GetClassInfoW(0, str, &wc)))
165 {
166 ok(test_atom == classatom,
167 "class atom did not match %x != %x\n", test_atom, classatom);
168 ok(wc.cbClsExtra == cls.cbClsExtra,
169 "cbClsExtra did not match %x!=%x\n",wc.cbClsExtra,cls.cbClsExtra);
170 ok(wc.cbWndExtra == cls.cbWndExtra,
171 "cbWndExtra did not match %x!=%x\n",wc.cbWndExtra,cls.cbWndExtra);
172 ok(wc.hbrBackground == cls.hbrBackground,
173 "hbrBackground did not match %p!=%p\n",wc.hbrBackground,cls.hbrBackground);
174 ok(wc.hCursor== cls.hCursor,
175 "hCursor did not match %p!=%p\n",wc.hCursor,cls.hCursor);
176 ok(!wc.hInstance,
177 "hInstance not zero for global class %p\n",wc.hInstance);
178 }
179 else
180 ok(FALSE,"GetClassInfo (0) failed for global class!\n");
181 }
182 else
183 {
184 ok(!GetClassInfoW(0, str, &wc),
185 "GetClassInfo (0) succeeded for local class!\n");
186 }
187
188 ok(!UnregisterClassW(className, hInstance),
189 "Unregister class succeeded with window existing\n");
190
191 ok(DestroyWindow(hTestWnd),
192 "DestroyWindow() failed!\n");
193
194 ok(UnregisterClassW(className, hInstance),
195 "UnregisterClass() failed\n");
196
197 return;
198 }
199
200 static void check_style( const char *name, int must_exist, UINT style, UINT ignore )
201 {
202 WNDCLASSA wc;
203
204 if (GetClassInfoA( 0, name, &wc ))
205 {
206 ok( !(~wc.style & style & ~ignore), "System class %s is missing bits %x (%08x/%08x)\n",
207 name, ~wc.style & style, wc.style, style );
208 ok( !(wc.style & ~style), "System class %s has extra bits %x (%08x/%08x)\n",
209 name, wc.style & ~style, wc.style, style );
210 }
211 else
212 ok( !must_exist, "System class %s does not exist\n", name );
213 }
214
215 /* test styles of system classes */
216 static void test_styles(void)
217 {
218 /* check style bits */
219 check_style( "Button", 1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, 0 );
220 check_style( "ComboBox", 1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, 0 );
221 check_style( "Edit", 1, CS_PARENTDC | CS_DBLCLKS, 0 );
222 check_style( "ListBox", 1, CS_PARENTDC | CS_DBLCLKS, CS_PARENTDC /*FIXME*/ );
223 check_style( "MDIClient", 1, 0, 0 );
224 check_style( "ScrollBar", 1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, 0 );
225 check_style( "Static", 1, CS_PARENTDC | CS_DBLCLKS, 0 );
226 check_style( "ComboLBox", 1, CS_SAVEBITS | CS_DBLCLKS, 0 );
227 check_style( "DDEMLEvent", 0, 0, 0 );
228 check_style( "Message", 0, 0, 0 );
229 check_style( "#32768", 1, CS_DROPSHADOW | CS_SAVEBITS | CS_DBLCLKS, CS_DROPSHADOW ); /* menu */
230 check_style( "#32769", 1, CS_DBLCLKS, 0 ); /* desktop */
231 check_style( "#32770", 1, CS_SAVEBITS | CS_DBLCLKS, 0 ); /* dialog */
232 todo_wine { check_style( "#32771", 1, CS_SAVEBITS | CS_HREDRAW | CS_VREDRAW, 0 ); } /* task switch */
233 check_style( "#32772", 1, 0, 0 ); /* icon title */
234 }
235
236 static void check_class_(int line, HINSTANCE inst, const char *name, const char *menu_name)
237 {
238 WNDCLASSA wc;
239 UINT atom = GetClassInfoA(inst,name,&wc);
240 ok_(__FILE__,line)( atom, "Class %s %p not found\n", name, inst );
241 if (atom)
242 {
243 if (wc.lpszMenuName && menu_name)
244 ok_(__FILE__,line)( !strcmp( menu_name, wc.lpszMenuName ),
245 "Wrong name %s/%s for class %s %p\n",
246 wc.lpszMenuName, menu_name, name, inst );
247 else
248 ok_(__FILE__,line)( !menu_name == !wc.lpszMenuName, "Wrong name %p/%p for class %s %p\n",
249 wc.lpszMenuName, menu_name, name, inst );
250 }
251 }
252 #define check_class(inst,name,menu) check_class_(__LINE__,inst,name,menu)
253
254 static void check_instance_( int line, const char *name, HINSTANCE inst,
255 HINSTANCE info_inst, HINSTANCE gcl_inst )
256 {
257 WNDCLASSA wc;
258 HWND hwnd;
259
260 ok_(__FILE__,line)( GetClassInfoA( inst, name, &wc ), "Couldn't find class %s inst %p\n", name, inst );
261 ok_(__FILE__,line)( wc.hInstance == info_inst, "Wrong info instance %p/%p for class %s\n",
262 wc.hInstance, info_inst, name );
263 hwnd = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, inst, 0 );
264 ok_(__FILE__,line)( hwnd != NULL, "Couldn't create window for class %s inst %p\n", name, inst );
265 ok_(__FILE__,line)( (HINSTANCE)GetClassLongPtrA( hwnd, GCLP_HMODULE ) == gcl_inst,
266 "Wrong GCL instance %p/%p for class %s\n",
267 (HINSTANCE)GetClassLongPtrA( hwnd, GCLP_HMODULE ), gcl_inst, name );
268 ok_(__FILE__,line)( (HINSTANCE)GetWindowLongPtrA( hwnd, GWLP_HINSTANCE ) == inst,
269 "Wrong GWL instance %p/%p for window %s\n",
270 (HINSTANCE)GetWindowLongPtrA( hwnd, GWLP_HINSTANCE ), inst, name );
271 ok_(__FILE__,line)(!UnregisterClassA(name, inst),
272 "UnregisterClassA should fail while exists a class window\n");
273 ok_(__FILE__,line)(GetLastError() == ERROR_CLASS_HAS_WINDOWS,
274 "GetLastError() should be set to ERROR_CLASS_HAS_WINDOWS not %d\n", GetLastError());
275 DestroyWindow(hwnd);
276 }
277 #define check_instance(name,inst,info_inst,gcl_inst) check_instance_(__LINE__,name,inst,info_inst,gcl_inst)
278
279 struct class_info
280 {
281 const char *name;
282 HINSTANCE inst, info_inst, gcl_inst;
283 };
284
285 static DWORD WINAPI thread_proc(void *param)
286 {
287 struct class_info *class_info = param;
288
289 check_instance(class_info->name, class_info->inst, class_info->info_inst, class_info->gcl_inst);
290
291 return 0;
292 }
293
294 static void check_thread_instance( const char *name, HINSTANCE inst, HINSTANCE info_inst, HINSTANCE gcl_inst )
295 {
296 HANDLE hThread;
297 DWORD tid;
298 struct class_info class_info;
299
300 class_info.name = name;
301 class_info.inst = inst;
302 class_info.info_inst = info_inst;
303 class_info.gcl_inst = gcl_inst;
304
305 hThread = CreateThread(NULL, 0, thread_proc, &class_info, 0, &tid);
306 ok(hThread != NULL, "CreateThread failed, error %d\n", GetLastError());
307 ok(WaitForSingleObject(hThread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
308 CloseHandle(hThread);
309 }
310
311 /* test various instance parameters */
312 static void test_instances(void)
313 {
314 WNDCLASSA cls, wc;
315 WNDCLASSEXA wcexA;
316 HWND hwnd, hwnd2;
317 const char *name = "__test__";
318 HINSTANCE kernel32 = GetModuleHandleA("kernel32");
319 HINSTANCE user32 = GetModuleHandleA("user32");
320 HINSTANCE main_module = GetModuleHandleA(NULL);
321 HINSTANCE zero_instance = 0;
322 DWORD r;
323 char buffer[0x10];
324
325 memset( &cls, 0, sizeof(cls) );
326 cls.style = CS_HREDRAW | CS_VREDRAW;
327 cls.lpfnWndProc = ClassTest_WndProc;
328 cls.cbClsExtra = 0;
329 cls.cbWndExtra = 0;
330 cls.lpszClassName = name;
331
332 cls.lpszMenuName = "main_module";
333 cls.hInstance = main_module;
334
335 ok( RegisterClassA( &cls ), "Failed to register local class for main module\n" );
336 check_class( main_module, name, "main_module" );
337 check_instance( name, main_module, main_module, main_module );
338 check_thread_instance( name, main_module, main_module, main_module );
339
340 cls.lpszMenuName = "kernel32";
341 cls.hInstance = kernel32;
342 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
343 check_class( kernel32, name, "kernel32" );
344 check_class( main_module, name, "main_module" );
345 check_instance( name, kernel32, kernel32, kernel32 );
346 check_thread_instance( name, kernel32, kernel32, kernel32 );
347 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
348
349 ZeroMemory(&wcexA, sizeof(wcexA));
350 wcexA.lpfnWndProc = DefWindowProcA;
351 wcexA.lpszClassName = "__classex_test__";
352 SetLastError(0xdeadbeef);
353 wcexA.cbSize = sizeof(wcexA) - 1;
354 ok( ((RegisterClassExA( &wcexA ) == 0) && (GetLastError() == ERROR_INVALID_PARAMETER)),
355 "Succeeded with invalid number of cbSize bytes\n");
356 SetLastError(0xdeadbeef);
357 wcexA.cbSize = sizeof(wcexA) + 1;
358 ok( ((RegisterClassExA( &wcexA ) == 0) && (GetLastError() == ERROR_INVALID_PARAMETER)),
359 "Succeeded with invalid number of cbSize bytes\n");
360 SetLastError(0xdeadbeef);
361 wcexA.cbSize = sizeof(wcexA);
362 ok( RegisterClassExA( &wcexA ), "Failed with valid number of cbSize bytes\n");
363 wcexA.cbSize = 0xdeadbeef;
364 ok( GetClassInfoExA(main_module, wcexA.lpszClassName, &wcexA), "GetClassInfoEx failed\n");
365 ok( wcexA.cbSize == 0xdeadbeef, "GetClassInfoEx returned wrong cbSize value %d\n", wcexA.cbSize);
366 UnregisterClassA(wcexA.lpszClassName, main_module);
367
368 /* Bug 2631 - Supplying an invalid number of bytes fails */
369 cls.cbClsExtra = 0;
370 cls.cbWndExtra = -1;
371 SetLastError(0xdeadbeef);
372 ok( ((RegisterClassA( &cls ) == 0) && (GetLastError() == ERROR_INVALID_PARAMETER)),
373 "Failed with invalid number of WndExtra bytes\n");
374
375 cls.cbClsExtra = -1;
376 cls.cbWndExtra = 0;
377 SetLastError(0xdeadbeef);
378 ok( ((RegisterClassA( &cls ) == 0) && (GetLastError() == ERROR_INVALID_PARAMETER)),
379 "Failed with invalid number of ClsExtra bytes\n");
380
381 cls.cbClsExtra = -1;
382 cls.cbWndExtra = -1;
383 SetLastError(0xdeadbeef);
384 ok( ((RegisterClassA( &cls ) == 0) && (GetLastError() == ERROR_INVALID_PARAMETER)),
385 "Failed with invalid number of ClsExtra and cbWndExtra bytes\n");
386
387 cls.cbClsExtra = 0;
388 cls.cbWndExtra = 0;
389 SetLastError(0xdeadbeef);
390
391 /* setting global flag doesn't change status of class */
392 hwnd = CreateWindowExA( 0, name, "test", 0, 0, 0, 0, 0, 0, 0, main_module, 0 );
393 ok( hwnd != 0, "CreateWindow failed error %u\n", GetLastError());
394 SetClassLongA( hwnd, GCL_STYLE, CS_GLOBALCLASS );
395 cls.lpszMenuName = "kernel32";
396 cls.hInstance = kernel32;
397 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
398 check_class( kernel32, name, "kernel32" );
399 check_class( main_module, name, "main_module" );
400 check_instance( name, kernel32, kernel32, kernel32 );
401 check_instance( name, main_module, main_module, main_module );
402 check_thread_instance( name, kernel32, kernel32, kernel32 );
403 check_thread_instance( name, main_module, main_module, main_module );
404 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
405
406 /* changing the instance doesn't make it global */
407 SetClassLongPtrA( hwnd, GCLP_HMODULE, 0 );
408 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
409 check_class( kernel32, name, "kernel32" );
410 check_instance( name, kernel32, kernel32, kernel32 );
411 check_thread_instance( name, kernel32, kernel32, kernel32 );
412 ok( !GetClassInfoA( 0, name, &wc ), "Class found with null instance\n" );
413 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
414
415 /* GetClassInfo with instance 0 finds user32 instance */
416 SetClassLongPtrA( hwnd, GCLP_HMODULE, (LONG_PTR)user32 );
417 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
418 if (!GetClassInfoA( 0, name, &wc )) zero_instance = user32; /* instance 0 not supported on wow64 */
419 else
420 {
421 check_instance( name, 0, 0, kernel32 );
422 check_thread_instance( name, 0, 0, kernel32 );
423 }
424 check_class( kernel32, name, "kernel32" );
425 check_class( user32, name, "main_module" );
426 check_class( zero_instance, name, "main_module" );
427 check_instance( name, kernel32, kernel32, kernel32 );
428 check_instance( name, user32, zero_instance, user32 );
429 check_thread_instance( name, kernel32, kernel32, kernel32 );
430 check_thread_instance( name, user32, zero_instance, user32 );
431 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
432
433 SetClassLongPtrA( hwnd, GCLP_HMODULE, 0x12345678 );
434 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
435 check_class( kernel32, name, "kernel32" );
436 check_class( (HINSTANCE)0x12345678, name, "main_module" );
437 check_instance( name, kernel32, kernel32, kernel32 );
438 check_instance( name, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678 );
439 check_thread_instance( name, kernel32, kernel32, kernel32 );
440 check_thread_instance( name, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678 );
441 ok( !GetClassInfoA( 0, name, &wc ), "Class found with null instance\n" );
442
443 /* creating a window with instance 0 uses the first class found */
444 cls.hInstance = (HINSTANCE)0xdeadbeef;
445 cls.lpszMenuName = "deadbeef";
446 cls.style = 3;
447 ok( RegisterClassA( &cls ), "Failed to register local class for deadbeef\n" );
448 hwnd2 = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, NULL, 0 );
449 ok( GetClassLongPtrA( hwnd2, GCLP_HMODULE ) == 0xdeadbeef,
450 "Didn't get deadbeef class for null instance\n" );
451 DestroyWindow( hwnd2 );
452 ok( UnregisterClassA( name, (HINSTANCE)0xdeadbeef ), "Unregister failed for deadbeef\n" );
453
454 hwnd2 = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, NULL, 0 );
455 ok( (HINSTANCE)GetClassLongPtrA( hwnd2, GCLP_HMODULE ) == kernel32,
456 "Didn't get kernel32 class for null instance\n" );
457 DestroyWindow( hwnd2 );
458
459 r = GetClassNameA( hwnd, buffer, 4 );
460 ok( r == 3, "expected 3, got %d\n", r );
461 ok( !strcmp( buffer, "__t"), "name wrong: %s\n", buffer );
462
463 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
464
465 hwnd2 = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, NULL, 0 );
466 ok( GetClassLongPtrA( hwnd2, GCLP_HMODULE ) == 0x12345678,
467 "Didn't get 12345678 class for null instance\n" );
468 DestroyWindow( hwnd2 );
469
470 SetClassLongPtrA( hwnd, GCLP_HMODULE, (LONG_PTR)main_module );
471 DestroyWindow( hwnd );
472
473 /* null handle means the same thing as main module */
474 cls.lpszMenuName = "null";
475 cls.hInstance = 0;
476 ok( !RegisterClassA( &cls ), "Succeeded registering local class for null instance\n" );
477 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %d\n", GetLastError() );
478 ok( UnregisterClassA( name, main_module ), "Unregister failed for main module\n" );
479
480 ok( RegisterClassA( &cls ), "Failed to register local class for null instance\n" );
481 /* must be found with main module handle */
482 check_class( main_module, name, "null" );
483 check_instance( name, main_module, main_module, main_module );
484 check_thread_instance( name, main_module, main_module, main_module );
485 ok( !GetClassInfoA( 0, name, &wc ), "Class found with null instance\n" );
486 ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %d\n", GetLastError() );
487 ok( UnregisterClassA( name, 0 ), "Unregister failed for null instance\n" );
488
489 /* registering for user32 always fails */
490 cls.lpszMenuName = "user32";
491 cls.hInstance = user32;
492 ok( !RegisterClassA( &cls ), "Succeeded registering local class for user32\n" );
493 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error code %d\n", GetLastError() );
494 cls.style |= CS_GLOBALCLASS;
495 ok( !RegisterClassA( &cls ), "Succeeded registering global class for user32\n" );
496 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error code %d\n", GetLastError() );
497
498 /* unregister is OK though */
499 cls.hInstance = main_module;
500 ok( RegisterClassA( &cls ), "Failed to register global class for main module\n" );
501 ok( UnregisterClassA( name, user32 ), "Unregister failed for user32\n" );
502
503 /* instance doesn't matter for global class */
504 cls.style |= CS_GLOBALCLASS;
505 cls.lpszMenuName = "main_module";
506 cls.hInstance = main_module;
507 ok( RegisterClassA( &cls ), "Failed to register global class for main module\n" );
508 cls.lpszMenuName = "kernel32";
509 cls.hInstance = kernel32;
510 ok( !RegisterClassA( &cls ), "Succeeded registering local class for kernel32\n" );
511 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %d\n", GetLastError() );
512 /* even if global flag is cleared */
513 hwnd = CreateWindowExA( 0, name, "test", 0, 0, 0, 0, 0, 0, 0, main_module, 0 );
514 SetClassLongA( hwnd, GCL_STYLE, 0 );
515 ok( !RegisterClassA( &cls ), "Succeeded registering local class for kernel32\n" );
516 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %d\n", GetLastError() );
517
518 check_class( main_module, name, "main_module" );
519 check_class( kernel32, name, "main_module" );
520 check_class( 0, name, "main_module" );
521 check_class( (HINSTANCE)0x12345678, name, "main_module" );
522 check_instance( name, main_module, main_module, main_module );
523 check_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, main_module );
524 check_thread_instance( name, main_module, main_module, main_module );
525 check_thread_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, main_module );
526
527 /* changing the instance for global class doesn't make much difference */
528 SetClassLongPtrA( hwnd, GCLP_HMODULE, 0xdeadbeef );
529 check_instance( name, main_module, main_module, (HINSTANCE)0xdeadbeef );
530 check_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef );
531 check_thread_instance( name, main_module, main_module, (HINSTANCE)0xdeadbeef );
532 check_thread_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef );
533
534 DestroyWindow( hwnd );
535 ok( UnregisterClassA( name, (HINSTANCE)0x87654321 ), "Unregister failed for main module global\n" );
536 ok( !UnregisterClassA( name, (HINSTANCE)0x87654321 ), "Unregister succeeded the second time\n" );
537 ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %d\n", GetLastError() );
538
539 cls.hInstance = (HINSTANCE)0x12345678;
540 ok( RegisterClassA( &cls ), "Failed to register global class for dummy instance\n" );
541 check_instance( name, main_module, main_module, (HINSTANCE)0x12345678 );
542 check_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0x12345678 );
543 check_thread_instance( name, main_module, main_module, (HINSTANCE)0x12345678 );
544 check_thread_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0x12345678 );
545 ok( UnregisterClassA( name, (HINSTANCE)0x87654321 ), "Unregister failed for main module global\n" );
546
547 /* check system classes */
548
549 /* we cannot register a global class with the name of a system class */
550 cls.style |= CS_GLOBALCLASS;
551 cls.lpszMenuName = "button_main_module";
552 cls.lpszClassName = "BUTTON";
553 cls.hInstance = main_module;
554 ok( !RegisterClassA( &cls ), "Succeeded registering global button class for main module\n" );
555 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %d\n", GetLastError() );
556 cls.hInstance = kernel32;
557 ok( !RegisterClassA( &cls ), "Succeeded registering global button class for kernel32\n" );
558 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %d\n", GetLastError() );
559
560 /* local class is OK however */
561 cls.style &= ~CS_GLOBALCLASS;
562 cls.lpszMenuName = "button_main_module";
563 cls.hInstance = main_module;
564 ok( RegisterClassA( &cls ), "Failed to register local button class for main module\n" );
565 check_class( main_module, "BUTTON", "button_main_module" );
566 cls.lpszMenuName = "button_kernel32";
567 cls.hInstance = kernel32;
568 ok( RegisterClassA( &cls ), "Failed to register local button class for kernel32\n" );
569 check_class( kernel32, "BUTTON", "button_kernel32" );
570 check_class( main_module, "BUTTON", "button_main_module" );
571 ok( UnregisterClassA( "BUTTON", kernel32 ), "Unregister failed for kernel32 button\n" );
572 ok( UnregisterClassA( "BUTTON", main_module ), "Unregister failed for main module button\n" );
573 /* GetClassInfo sets instance to passed value for global classes */
574 check_instance( "BUTTON", 0, 0, user32 );
575 check_instance( "BUTTON", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
576 check_instance( "BUTTON", user32, zero_instance, user32 );
577 check_thread_instance( "BUTTON", 0, 0, user32 );
578 check_thread_instance( "BUTTON", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
579 check_thread_instance( "BUTTON", user32, zero_instance, user32 );
580
581 /* we can unregister system classes */
582 ok( GetClassInfoA( 0, "BUTTON", &wc ), "Button class not found with null instance\n" );
583 ok( GetClassInfoA( kernel32, "BUTTON", &wc ), "Button class not found with kernel32\n" );
584 ok( UnregisterClassA( "BUTTON", (HINSTANCE)0x12345678 ), "Failed to unregister button\n" );
585 ok( !UnregisterClassA( "BUTTON", (HINSTANCE)0x87654321 ), "Unregistered button a second time\n" );
586 ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %d\n", GetLastError() );
587 ok( !GetClassInfoA( 0, "BUTTON", &wc ), "Button still exists\n" );
588 /* last error not set reliably */
589
590 /* we can change the instance of a system class */
591 check_instance( "EDIT", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
592 check_thread_instance( "EDIT", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
593 hwnd = CreateWindowExA( 0, "EDIT", "test", 0, 0, 0, 0, 0, 0, 0, main_module, 0 );
594 SetClassLongPtrA( hwnd, GCLP_HMODULE, 0xdeadbeef );
595 check_instance( "EDIT", (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0xdeadbeef );
596 check_thread_instance( "EDIT", (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0xdeadbeef );
597 DestroyWindow(hwnd);
598 }
599
600 static void test_builtinproc(void)
601 {
602 /* Edit behaves differently */
603 static const CHAR NORMAL_CLASSES[][10] = {
604 "Button",
605 "Static",
606 "ComboBox",
607 "ComboLBox",
608 "ListBox",
609 "ScrollBar",
610 "#32770", /* dialog */
611 };
612 static const int NUM_NORMAL_CLASSES = (sizeof(NORMAL_CLASSES)/sizeof(NORMAL_CLASSES[0]));
613 static const char classA[] = "deftest";
614 static const WCHAR classW[] = {'d','e','f','t','e','s','t',0};
615 WCHAR unistring[] = {0x142, 0x40e, 0x3b4, 0}; /* a string that would be destroyed by a W->A->W conversion */
616 WNDPROC pDefWindowProcA, pDefWindowProcW;
617 WNDPROC pNtdllDefWindowProcA, pNtdllDefWindowProcW;
618 WNDPROC oldproc;
619 WNDCLASSEXA cls; /* the memory layout of WNDCLASSEXA and WNDCLASSEXW is the same */
620 WCHAR buf[128];
621 ATOM atom;
622 HWND hwnd;
623 int i;
624
625 pDefWindowProcA = (void *)GetProcAddress(GetModuleHandleA("user32.dll"), "DefWindowProcA");
626 pDefWindowProcW = (void *)GetProcAddress(GetModuleHandleA("user32.dll"), "DefWindowProcW");
627 pNtdllDefWindowProcA = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtdllDefWindowProc_A");
628 pNtdllDefWindowProcW = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtdllDefWindowProc_W");
629
630 /* On Vista+, the user32.dll export DefWindowProcA/W is forwarded to */
631 /* ntdll.NtdllDefWindowProc_A/W. However, the wndproc returned by */
632 /* GetClassLong/GetWindowLong points to an unexported user32 function */
633 if (pDefWindowProcA == pNtdllDefWindowProcA &&
634 pDefWindowProcW == pNtdllDefWindowProcW)
635 skip("user32.DefWindowProcX forwarded to ntdll.NtdllDefWindowProc_X\n");
636 else
637 {
638 for (i = 0; i < 4; i++)
639 {
640 ZeroMemory(&cls, sizeof(cls));
641 cls.cbSize = sizeof(cls);
642 cls.hInstance = GetModuleHandleA(NULL);
643 cls.hbrBackground = GetStockObject (WHITE_BRUSH);
644 if (i & 1)
645 cls.lpfnWndProc = pDefWindowProcA;
646 else
647 cls.lpfnWndProc = pDefWindowProcW;
648
649 if (i & 2)
650 {
651 cls.lpszClassName = classA;
652 atom = RegisterClassExA(&cls);
653 }
654 else
655 {
656 cls.lpszClassName = (LPCSTR)classW;
657 atom = RegisterClassExW((WNDCLASSEXW *)&cls);
658 }
659 ok(atom != 0, "Couldn't register class, i=%d, %d\n", i, GetLastError());
660
661 hwnd = CreateWindowA(classA, NULL, 0, 0, 0, 100, 100, NULL, NULL, GetModuleHandleA(NULL), NULL);
662 ok(hwnd != NULL, "Couldn't create window i=%d\n", i);
663
664 ok(GetWindowLongPtrA(hwnd, GWLP_WNDPROC) == (LONG_PTR)pDefWindowProcA, "Wrong ANSI wndproc: %p vs %p\n",
665 (void *)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), pDefWindowProcA);
666 ok(GetClassLongPtrA(hwnd, GCLP_WNDPROC) == (ULONG_PTR)pDefWindowProcA, "Wrong ANSI wndproc: %p vs %p\n",
667 (void *)GetClassLongPtrA(hwnd, GCLP_WNDPROC), pDefWindowProcA);
668
669 ok(GetWindowLongPtrW(hwnd, GWLP_WNDPROC) == (LONG_PTR)pDefWindowProcW, "Wrong Unicode wndproc: %p vs %p\n",
670 (void *)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), pDefWindowProcW);
671 ok(GetClassLongPtrW(hwnd, GCLP_WNDPROC) == (ULONG_PTR)pDefWindowProcW, "Wrong Unicode wndproc: %p vs %p\n",
672 (void *)GetClassLongPtrW(hwnd, GCLP_WNDPROC), pDefWindowProcW);
673
674 DestroyWindow(hwnd);
675 UnregisterClassA((LPSTR)(DWORD_PTR)atom, GetModuleHandleA(NULL));
676 }
677 }
678
679 /* built-in winproc - window A/W type automatically detected */
680 ZeroMemory(&cls, sizeof(cls));
681 cls.cbSize = sizeof(cls);
682 cls.hInstance = GetModuleHandleA(NULL);
683 cls.hbrBackground = GetStockObject (WHITE_BRUSH);
684 cls.lpszClassName = classA;
685 cls.lpfnWndProc = pDefWindowProcW;
686 atom = RegisterClassExA(&cls);
687
688 hwnd = CreateWindowExW(0, classW, unistring, WS_OVERLAPPEDWINDOW,
689 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleW(NULL), 0);
690 ok(IsWindowUnicode(hwnd) ||
691 broken(!IsWindowUnicode(hwnd)) /* Windows 8 and 10 */,
692 "Windows should be Unicode\n");
693 SendMessageW(hwnd, WM_GETTEXT, sizeof(buf) / sizeof(buf[0]), (LPARAM)buf);
694 if (IsWindowUnicode(hwnd))
695 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
696 else
697 ok(memcmp(buf, unistring, sizeof(unistring)) != 0, "WM_GETTEXT invalid return\n");
698 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)pDefWindowProcA);
699 ok(IsWindowUnicode(hwnd), "Windows should have remained Unicode\n");
700 if (GetWindowLongPtrW(hwnd, GWLP_WNDPROC) == (LONG_PTR)pDefWindowProcA)
701 {
702 /* DefWindowProc isn't magic on wow64 */
703 ok(IS_WNDPROC_HANDLE(GetWindowLongPtrA(hwnd, GWLP_WNDPROC)), "Ansi winproc is not a handle\n");
704 }
705 else
706 {
707 ok(GetWindowLongPtrW(hwnd, GWLP_WNDPROC) == (LONG_PTR)pDefWindowProcW, "Invalid Unicode winproc\n");
708 ok(GetWindowLongPtrA(hwnd, GWLP_WNDPROC) == (LONG_PTR)pDefWindowProcA, "Invalid Ansi winproc\n");
709 }
710 SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
711 ok(IsWindowUnicode(hwnd) == FALSE, "SetWindowLongPtrA should have switched window to ANSI\n");
712
713 DestroyWindow(hwnd);
714 UnregisterClassA((LPSTR)(DWORD_PTR)atom, GetModuleHandleA(NULL));
715
716 /* custom winproc - the same function can be used as both A and W*/
717 ZeroMemory(&cls, sizeof(cls));
718 cls.cbSize = sizeof(cls);
719 cls.hInstance = GetModuleHandleA(NULL);
720 cls.hbrBackground = GetStockObject (WHITE_BRUSH);
721 cls.lpszClassName = classA;
722 cls.lpfnWndProc = ClassTest_WndProc2;
723 atom = RegisterClassExA(&cls);
724
725 hwnd = CreateWindowExW(0, classW, NULL, WS_OVERLAPPEDWINDOW,
726 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
727 ok(IsWindowUnicode(hwnd) == FALSE, "Window should be ANSI\n");
728 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
729 ok(IsWindowUnicode(hwnd), "SetWindowLongPtrW should have changed window to Unicode\n");
730 SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
731 ok(IsWindowUnicode(hwnd) == FALSE, "SetWindowLongPtrA should have changed window to ANSI\n");
732
733 DestroyWindow(hwnd);
734 UnregisterClassA((LPSTR)(DWORD_PTR)atom, GetModuleHandleA(NULL));
735
736 /* For most of the builtin controls both GetWindowLongPtrA and W returns a pointer that is executed directly
737 * by CallWindowProcA/W */
738 for (i = 0; i < NUM_NORMAL_CLASSES; i++)
739 {
740 WNDPROC procA, procW;
741 hwnd = CreateWindowExA(0, NORMAL_CLASSES[i], classA, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 680, 260,
742 NULL, NULL, NULL, 0);
743 ok(hwnd != NULL, "Couldn't create window of class %s\n", NORMAL_CLASSES[i]);
744 SetWindowTextA(hwnd, classA); /* ComboBox needs this */
745 procA = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC);
746 procW = (WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC);
747 ok(!IS_WNDPROC_HANDLE(procA), "procA should not be a handle for %s (%p)\n", NORMAL_CLASSES[i], procA);
748 ok(!IS_WNDPROC_HANDLE(procW), "procW should not be a handle for %s (%p)\n", NORMAL_CLASSES[i], procW);
749 CallWindowProcA(procA, hwnd, WM_GETTEXT, 120, (LPARAM)buf);
750 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT A/A invalid return for class %s\n", NORMAL_CLASSES[i]);
751 CallWindowProcA(procW, hwnd, WM_GETTEXT, 120, (LPARAM)buf);
752 ok(memcmp(buf, classW, sizeof(classW)) == 0, "WM_GETTEXT A/W invalid return for class %s\n", NORMAL_CLASSES[i]);
753 CallWindowProcW(procA, hwnd, WM_GETTEXT, 120, (LPARAM)buf);
754 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT W/A invalid return for class %s\n", NORMAL_CLASSES[i]);
755 CallWindowProcW(procW, hwnd, WM_GETTEXT, 120, (LPARAM)buf);
756 ok(memcmp(buf, classW, sizeof(classW)) == 0, "WM_GETTEXT W/W invalid return for class %s\n", NORMAL_CLASSES[i]);
757
758 oldproc = (WNDPROC)SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
759 ok(IS_WNDPROC_HANDLE(oldproc) == FALSE, "Class %s shouldn't return a handle\n", NORMAL_CLASSES[i]);
760 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)oldproc);
761 DestroyWindow(hwnd);
762 }
763
764 /* Edit controls are special - they return a wndproc handle when GetWindowLongPtr is called with a different A/W.
765 * On the other hand there is no W->A->W conversion so this control is treated specially. */
766 hwnd = CreateWindowW(WC_EDITW, unistring, WS_OVERLAPPEDWINDOW,
767 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, NULL, 0);
768 /* GetClassLongPtr returns that both the Unicode and ANSI wndproc */
769 ok(IS_WNDPROC_HANDLE(GetClassLongPtrA(hwnd, GCLP_WNDPROC)) == FALSE, "Edit control class should have a Unicode wndproc\n");
770 ok(IS_WNDPROC_HANDLE(GetClassLongPtrW(hwnd, GCLP_WNDPROC)) == FALSE, "Edit control class should have a ANSI wndproc\n");
771 /* But GetWindowLongPtr returns only a handle for the ANSI one */
772 ok(IS_WNDPROC_HANDLE(GetWindowLongPtrA(hwnd, GWLP_WNDPROC)), "Edit control should return a wndproc handle\n");
773 ok(!IS_WNDPROC_HANDLE(GetWindowLongPtrW(hwnd, GWLP_WNDPROC)), "Edit control shouldn't return a W wndproc handle\n");
774 CallWindowProcW((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
775 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
776 CallWindowProcA((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
777 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
778 CallWindowProcW((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
779 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
780
781 SetWindowTextW(hwnd, classW);
782 CallWindowProcA((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
783 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
784
785 oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc2);
786 /* SetWindowLongPtr returns a wndproc handle - like GetWindowLongPtr */
787 ok(IS_WNDPROC_HANDLE(oldproc), "Edit control should return a wndproc handle\n");
788 ok(IsWindowUnicode(hwnd) == FALSE, "SetWindowLongPtrA should have changed window to ANSI\n");
789 SetWindowTextA(hwnd, classA); /* Windows resets the title to WideStringToMultiByte(unistring) */
790 memset(buf, 0, sizeof(buf));
791 CallWindowProcA((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
792 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
793 CallWindowProcA((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
794 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
795 CallWindowProcW((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
796 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
797
798 CallWindowProcW((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
799 ok(memcmp(buf, classW, sizeof(classW)) == 0, "WM_GETTEXT invalid return\n");
800
801 SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)oldproc);
802
803 DestroyWindow(hwnd);
804
805 hwnd = CreateWindowA(WC_EDITA, classA, WS_OVERLAPPEDWINDOW,
806 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, NULL, 0);
807
808 /* GetClassLongPtr returns that both the Unicode and ANSI wndproc */
809 ok(!IS_WNDPROC_HANDLE(GetClassLongPtrA(hwnd, GCLP_WNDPROC)), "Edit control class should have a Unicode wndproc\n");
810 ok(!IS_WNDPROC_HANDLE(GetClassLongPtrW(hwnd, GCLP_WNDPROC)), "Edit control class should have a ANSI wndproc\n");
811 /* But GetWindowLongPtr returns only a handle for the Unicode one */
812 ok(!IS_WNDPROC_HANDLE(GetWindowLongPtrA(hwnd, GWLP_WNDPROC)), "Edit control shouldn't return an A wndproc handle\n");
813 ok(IS_WNDPROC_HANDLE(GetWindowLongPtrW(hwnd, GWLP_WNDPROC)), "Edit control should return a wndproc handle\n");
814 CallWindowProcA((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
815 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
816 CallWindowProcA((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
817 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
818 CallWindowProcW((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
819 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
820
821 CallWindowProcW((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
822 ok(memcmp(buf, classW, sizeof(classW)) == 0, "WM_GETTEXT invalid return\n");
823
824 oldproc = (WNDPROC)SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
825 SetWindowTextW(hwnd, unistring);
826 CallWindowProcW((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
827 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
828 CallWindowProcA((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
829 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
830 CallWindowProcW((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
831 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
832
833 SetWindowTextW(hwnd, classW);
834 CallWindowProcA((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
835 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
836
837 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)oldproc);
838
839 DestroyWindow(hwnd);
840 }
841
842
843 static LRESULT WINAPI TestDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
844 {
845 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
846 }
847
848 static BOOL RegisterTestDialog(HINSTANCE hInstance)
849 {
850 WNDCLASSEXA wcx;
851 ATOM atom = 0;
852
853 ZeroMemory(&wcx, sizeof(WNDCLASSEXA));
854 wcx.cbSize = sizeof(wcx);
855 wcx.lpfnWndProc = TestDlgProc;
856 wcx.cbClsExtra = 0;
857 wcx.cbWndExtra = DLGWINDOWEXTRA;
858 wcx.hInstance = hInstance;
859 wcx.hIcon = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
860 wcx.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
861 wcx.hbrBackground = GetStockObject(WHITE_BRUSH);
862 wcx.lpszClassName = "TestDialog";
863 wcx.lpszMenuName = "TestDialog";
864 wcx.hIconSm = LoadImageA(hInstance, (LPCSTR)MAKEINTRESOURCE(5), IMAGE_ICON,
865 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
866 LR_DEFAULTCOLOR);
867
868 atom = RegisterClassExA(&wcx);
869 ok(atom != 0, "RegisterClassEx returned 0\n");
870
871 return atom;
872 }
873
874 /* test registering a dialog box created by using the CLASS directive in a
875 resource file, then test creating the dialog using CreateDialogParam. */
876 static void CreateDialogParamTest(HINSTANCE hInstance)
877 {
878 HWND hWndMain;
879
880 if (RegisterTestDialog(hInstance))
881 {
882 hWndMain = CreateDialogParamA(hInstance, "CLASS_TEST_DIALOG", NULL, 0, 0);
883 ok(hWndMain != NULL, "CreateDialogParam returned NULL\n");
884 ShowWindow(hWndMain, SW_SHOW);
885 DestroyWindow(hWndMain);
886 }
887 }
888
889 static const struct
890 {
891 const char name[9];
892 int value;
893 int badvalue;
894 } extra_values[] =
895 {
896 {"#32770",30,30}, /* Dialog */
897 #ifdef _WIN64
898 {"Edit",8,8},
899 #else
900 {"Edit",6,8}, /* Windows XP 64-bit returns 8 also to 32-bit applications */
901 #endif
902 };
903
904 static void test_extra_values(void)
905 {
906 int i;
907 for(i=0; i< sizeof(extra_values)/sizeof(extra_values[0]); i++)
908 {
909 WNDCLASSEXA wcx;
910 BOOL ret = GetClassInfoExA(NULL,extra_values[i].name,&wcx);
911
912 ok( ret, "GetClassInfo (0) failed for global class %s\n", extra_values[i].name);
913 if (!ret) continue;
914 ok(extra_values[i].value == wcx.cbWndExtra || broken(extra_values[i].badvalue == wcx.cbWndExtra),
915 "expected %d, got %d\n", extra_values[i].value, wcx.cbWndExtra);
916 }
917 }
918
919 static void test_GetClassInfo(void)
920 {
921 static const WCHAR staticW[] = {'s','t','a','t','i','c',0};
922 WNDCLASSA wc;
923 WNDCLASSEXA wcx;
924 BOOL ret;
925
926 SetLastError(0xdeadbeef);
927 ret = GetClassInfoA(0, "static", &wc);
928 ok(ret, "GetClassInfoA() error %d\n", GetLastError());
929
930 if (0) { /* crashes under XP */
931 SetLastError(0xdeadbeef);
932 ret = GetClassInfoA(0, "static", NULL);
933 ok(ret, "GetClassInfoA() error %d\n", GetLastError());
934
935 SetLastError(0xdeadbeef);
936 ret = GetClassInfoW(0, staticW, NULL);
937 ok(ret, "GetClassInfoW() error %d\n", GetLastError());
938 }
939
940 wcx.cbSize = sizeof(wcx);
941 SetLastError(0xdeadbeef);
942 ret = GetClassInfoExA(0, "static", &wcx);
943 ok(ret, "GetClassInfoExA() error %d\n", GetLastError());
944
945 SetLastError(0xdeadbeef);
946 ret = GetClassInfoExA(0, "static", NULL);
947 ok(!ret, "GetClassInfoExA() should fail\n");
948 ok(GetLastError() == ERROR_NOACCESS ||
949 broken(GetLastError() == 0xdeadbeef), /* win9x */
950 "expected ERROR_NOACCESS, got %d\n", GetLastError());
951
952 SetLastError(0xdeadbeef);
953 ret = GetClassInfoExW(0, staticW, NULL);
954 ok(!ret, "GetClassInfoExW() should fail\n");
955 ok(GetLastError() == ERROR_NOACCESS ||
956 broken(GetLastError() == 0xdeadbeef) /* NT4 */ ||
957 broken(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), /* win9x */
958 "expected ERROR_NOACCESS, got %d\n", GetLastError());
959
960 wcx.cbSize = 0;
961 wcx.lpfnWndProc = NULL;
962 SetLastError(0xdeadbeef);
963 ret = GetClassInfoExA(0, "static", &wcx);
964 ok(ret, "GetClassInfoExA() error %d\n", GetLastError());
965 ok(wcx.cbSize == 0, "expected 0, got %u\n", wcx.cbSize);
966 ok(wcx.lpfnWndProc != NULL, "got null proc\n");
967
968 wcx.cbSize = sizeof(wcx) - 1;
969 wcx.lpfnWndProc = NULL;
970 SetLastError(0xdeadbeef);
971 ret = GetClassInfoExA(0, "static", &wcx);
972 ok(ret, "GetClassInfoExA() error %d\n", GetLastError());
973 ok(wcx.cbSize == sizeof(wcx) - 1, "expected sizeof(wcx)-1, got %u\n", wcx.cbSize);
974 ok(wcx.lpfnWndProc != NULL, "got null proc\n");
975
976 wcx.cbSize = sizeof(wcx) + 1;
977 wcx.lpfnWndProc = NULL;
978 SetLastError(0xdeadbeef);
979 ret = GetClassInfoExA(0, "static", &wcx);
980 ok(ret, "GetClassInfoExA() error %d\n", GetLastError());
981 ok(wcx.cbSize == sizeof(wcx) + 1, "expected sizeof(wcx)+1, got %u\n", wcx.cbSize);
982 ok(wcx.lpfnWndProc != NULL, "got null proc\n");
983 }
984
985 static void test_icons(void)
986 {
987 WNDCLASSEXW wcex, ret_wcex;
988 WCHAR cls_name[] = {'I','c','o','n','T','e','s','t','C','l','a','s','s',0};
989 HWND hwnd;
990 HINSTANCE hinst = GetModuleHandleW(0);
991 HICON hsmicon, hsmallnew;
992 ICONINFO icinf;
993
994 memset(&wcex, 0, sizeof wcex);
995 wcex.cbSize = sizeof wcex;
996 wcex.lpfnWndProc = ClassTest_WndProc;
997 wcex.hIcon = LoadIconW(0, (LPCWSTR)IDI_APPLICATION);
998 wcex.hInstance = hinst;
999 wcex.lpszClassName = cls_name;
1000 ok(RegisterClassExW(&wcex), "RegisterClassExW returned 0\n");
1001 hwnd = CreateWindowExW(0, cls_name, NULL, WS_OVERLAPPEDWINDOW,
1002 0, 0, 0, 0, NULL, NULL, hinst, 0);
1003 ok(hwnd != NULL, "Window was not created\n");
1004
1005 ok(GetClassInfoExW(hinst, cls_name, &ret_wcex), "Class info was not retrieved\n");
1006 ok(wcex.hIcon == ret_wcex.hIcon, "Icons don't match\n");
1007 ok(ret_wcex.hIconSm != NULL, "hIconSm should be non-zero handle\n");
1008
1009 hsmicon = (HICON)GetClassLongPtrW(hwnd, GCLP_HICONSM);
1010 ok(hsmicon != NULL, "GetClassLong should return non-zero handle\n");
1011
1012 ok(SendMessageA(hwnd, WM_GETICON, ICON_BIG, 0) == 0,
1013 "WM_GETICON with ICON_BIG should not return the class icon\n");
1014 ok(SendMessageA(hwnd, WM_GETICON, ICON_SMALL, 0) == 0,
1015 "WM_GETICON with ICON_SMALL should not return the class icon\n");
1016 ok(SendMessageA(hwnd, WM_GETICON, ICON_SMALL2, 0) == 0,
1017 "WM_GETICON with ICON_SMALL2 should not return the class icon\n");
1018
1019 hsmallnew = CopyImage(wcex.hIcon, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
1020 GetSystemMetrics(SM_CYSMICON), 0);
1021 ok(!SetClassLongPtrW(hwnd, GCLP_HICONSM, (LONG_PTR)hsmallnew),
1022 "Previous hIconSm should be zero\n");
1023 ok(hsmallnew == (HICON)GetClassLongPtrW(hwnd, GCLP_HICONSM),
1024 "Should return explicitly assigned small icon\n");
1025 ok(!GetIconInfo(hsmicon, &icinf), "Previous small icon should be destroyed\n");
1026
1027 SetClassLongPtrW(hwnd, GCLP_HICONSM, 0);
1028 hsmicon = (HICON)GetClassLongPtrW(hwnd, GCLP_HICONSM);
1029 ok( hsmicon != NULL, "GetClassLong should return non-zero handle\n");
1030
1031 SetClassLongPtrW(hwnd, GCLP_HICON, 0);
1032 ok(!GetClassLongPtrW(hwnd, GCLP_HICONSM), "GetClassLong should return zero handle\n");
1033
1034 SetClassLongPtrW(hwnd, GCLP_HICON, (LONG_PTR)LoadIconW(NULL, (LPCWSTR)IDI_QUESTION));
1035 hsmicon = (HICON)GetClassLongPtrW(hwnd, GCLP_HICONSM);
1036 ok(hsmicon != NULL, "GetClassLong should return non-zero handle\n");
1037 UnregisterClassW(cls_name, hinst);
1038 ok(GetIconInfo(hsmicon, &icinf), "Icon should NOT be destroyed\n");
1039
1040 DestroyIcon(hsmallnew);
1041 DestroyWindow(hwnd);
1042 }
1043
1044 static void test_comctl32_class( const char *name )
1045 {
1046 WNDCLASSA wcA;
1047 WNDCLASSW wcW;
1048 BOOL ret;
1049 HMODULE module;
1050 WCHAR nameW[20];
1051 HWND hwnd;
1052
1053 module = GetModuleHandleA( "comctl32" );
1054 ok( !module, "comctl32 already loaded\n" );
1055 ret = GetClassInfoA( 0, name, &wcA );
1056 ok( ret || broken(!ret) /* <= winxp */, "GetClassInfoA failed for %s\n", name );
1057 if (!ret) return;
1058 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, sizeof(nameW)/sizeof(WCHAR) );
1059 ret = GetClassInfoW( 0, nameW, &wcW );
1060 ok( ret, "GetClassInfoW failed for %s\n", name );
1061 module = GetModuleHandleA( "comctl32" );
1062 ok( module != 0, "comctl32 not loaded\n" );
1063 FreeLibrary( module );
1064 module = GetModuleHandleA( "comctl32" );
1065 ok( !module, "comctl32 still loaded\n" );
1066 hwnd = CreateWindowA( name, "test", WS_OVERLAPPEDWINDOW, 0, 0, 10, 10, NULL, NULL, NULL, 0 );
1067 ok( hwnd != 0, "failed to create window for %s\n", name );
1068 module = GetModuleHandleA( "comctl32" );
1069 ok( module != 0, "comctl32 not loaded\n" );
1070 }
1071
1072 /* verify that comctl32 classes are automatically loaded by user32 */
1073 static void test_comctl32_classes(void)
1074 {
1075 char path_name[MAX_PATH];
1076 PROCESS_INFORMATION info;
1077 STARTUPINFOA startup;
1078 char **argv;
1079 int i;
1080
1081 static const char *classes[] =
1082 {
1083 ANIMATE_CLASSA,
1084 WC_COMBOBOXEXA,
1085 DATETIMEPICK_CLASSA,
1086 WC_HEADERA,
1087 HOTKEY_CLASSA,
1088 WC_IPADDRESSA,
1089 WC_LISTVIEWA,
1090 MONTHCAL_CLASSA,
1091 WC_NATIVEFONTCTLA,
1092 WC_PAGESCROLLERA,
1093 PROGRESS_CLASSA,
1094 REBARCLASSNAMEA,
1095 STATUSCLASSNAMEA,
1096 WC_TABCONTROLA,
1097 TOOLBARCLASSNAMEA,
1098 TOOLTIPS_CLASSA,
1099 TRACKBAR_CLASSA,
1100 WC_TREEVIEWA,
1101 UPDOWN_CLASSA
1102 };
1103
1104 winetest_get_mainargs( &argv );
1105 for (i = 0; i < sizeof(classes) / sizeof(classes[0]); i++)
1106 {
1107 memset( &startup, 0, sizeof(startup) );
1108 startup.cb = sizeof( startup );
1109 sprintf( path_name, "%s class %s", argv[0], classes[i] );
1110 ok( CreateProcessA( NULL, path_name, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info ),
1111 "CreateProcess failed.\n" );
1112 winetest_wait_child_process( info.hProcess );
1113 CloseHandle( info.hProcess );
1114 CloseHandle( info.hThread );
1115 }
1116 }
1117
1118 static void test_IME(void)
1119 {
1120 static const WCHAR ime_classW[] = {'I','M','E',0};
1121
1122 char module_name[MAX_PATH], *ptr;
1123 MEMORY_BASIC_INFORMATION mbi;
1124 WNDCLASSW wnd_classw;
1125 WNDCLASSA wnd_class;
1126 SIZE_T size;
1127 BOOL ret;
1128
1129 if (!GetProcAddress(GetModuleHandleA("user32.dll"), "BroadcastSystemMessageExA"))
1130 {
1131 win_skip("BroadcastSystemMessageExA not available, skipping IME class test\n");
1132 return;
1133 }
1134
1135 ok(GetModuleHandleA("imm32") != 0, "imm32.dll is not loaded\n");
1136
1137 ret = GetClassInfoA(NULL, "IME", &wnd_class);
1138 ok(ret, "GetClassInfo failed: %d\n", GetLastError());
1139
1140 size = VirtualQuery(wnd_class.lpfnWndProc, &mbi, sizeof(mbi));
1141 ok(size == sizeof(mbi), "VirtualQuery returned %ld\n", size);
1142 if (size == sizeof(mbi)) {
1143 size = GetModuleFileNameA(mbi.AllocationBase, module_name, sizeof(module_name));
1144 ok(size, "GetModuleFileName failed\n");
1145 for (ptr = module_name+size-1; ptr > module_name; ptr--)
1146 if (*ptr == '\\' || *ptr == '/') break;
1147 if (*ptr == '\\' || *ptr=='/') ptr++;
1148 ok(!lstrcmpiA(ptr, "user32.dll") || !lstrcmpiA(ptr, "ntdll.dll"), "IME window proc implemented in %s\n", ptr);
1149 }
1150
1151 ret = GetClassInfoW(NULL, ime_classW, &wnd_classw);
1152 ok(ret, "GetClassInfo failed: %d\n", GetLastError());
1153
1154 size = VirtualQuery(wnd_classw.lpfnWndProc, &mbi, sizeof(mbi));
1155 ok(size == sizeof(mbi), "VirtualQuery returned %ld\n", size);
1156 size = GetModuleFileNameA(mbi.AllocationBase, module_name, sizeof(module_name));
1157 ok(size, "GetModuleFileName failed\n");
1158 for (ptr = module_name+size-1; ptr > module_name; ptr--)
1159 if (*ptr == '\\' || *ptr == '/') break;
1160 if (*ptr == '\\' || *ptr=='/') ptr++;
1161 ok(!lstrcmpiA(ptr, "user32.dll") || !lstrcmpiA(ptr, "ntdll.dll"), "IME window proc implemented in %s\n", ptr);
1162 }
1163
1164 START_TEST(class)
1165 {
1166 char **argv;
1167 HANDLE hInstance = GetModuleHandleA( NULL );
1168 int argc = winetest_get_mainargs( &argv );
1169
1170 if (argc >= 3)
1171 {
1172 test_comctl32_class( argv[2] );
1173 return;
1174 }
1175
1176 test_IME();
1177 test_GetClassInfo();
1178 test_extra_values();
1179
1180 if (!GetModuleHandleW(0))
1181 {
1182 trace("Class test is incompatible with Win9x implementation, skipping\n");
1183 return;
1184 }
1185
1186 ClassTest(hInstance,FALSE);
1187 ClassTest(hInstance,TRUE);
1188 CreateDialogParamTest(hInstance);
1189 test_styles();
1190 test_builtinproc();
1191 test_icons();
1192 test_comctl32_classes();
1193
1194 /* this test unregisters the Button class so it should be executed at the end */
1195 test_instances();
1196 }