comctl32/tests: Use BOOL type where appropriate.
[wine.git] / dlls / user32 / tests / class.c
blob1d3583279b4a7c56d4ace7dc4414f68258e7b74d
1 /* Unit test suite for window classes.
3 * Copyright 2002 Mike McCormack
4 * Copyright 2003 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 /* To get CS_DROPSHADOW with the MSVC headers */
22 #define _WIN32_WINNT 0x0501
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
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"
37 #define NUMCLASSWORDS 4
39 #define IS_WNDPROC_HANDLE(x) (((ULONG_PTR)(x) >> 16) == (~0u >> 16))
41 static LRESULT WINAPI ClassTest_WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
43 if (msg == WM_NCCREATE) return 1;
44 return DefWindowProcW (hWnd, msg, wParam, lParam);
47 static LRESULT WINAPI ClassTest_WndProc2 (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
49 if (msg == WM_NCCREATE) return 1;
50 return DefWindowProcA (hWnd, msg, wParam, lParam);
53 /***********************************************************************
55 static void ClassTest(HINSTANCE hInstance, BOOL global)
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;
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;
77 classatom=RegisterClassW(&cls);
78 if (!classatom && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
79 return;
80 ok(classatom, "failed to register class\n");
82 ok(!RegisterClassW (&cls),
83 "RegisterClass of the same class should fail for the second time\n");
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);
91 ok(hTestWnd!=0, "Failed to create window\n");
93 /* test initial values of valid classwords */
94 for(i=0; i<NUMCLASSWORDS; i++)
96 SetLastError(0);
97 ok(!GetClassLongW(hTestWnd,i*sizeof (DWORD)),
98 "GetClassLongW initial value nonzero!\n");
99 ok(!GetLastError(),
100 "GetClassLongW failed!\n");
103 if (0)
106 * GetClassLongW(hTestWnd, NUMCLASSWORDS*sizeof(DWORD))
107 * does not fail on Win 98, though MSDN says it should
109 SetLastError(0);
110 GetClassLongW(hTestWnd, NUMCLASSWORDS*sizeof(DWORD));
111 ok(GetLastError(),
112 "GetClassLongW() with invalid offset did not fail\n");
115 /* set values of valid class words */
116 for(i=0; i<NUMCLASSWORDS; i++)
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);
125 /* test values of valid classwords that we set */
126 for(i=0; i<NUMCLASSWORDS; i++)
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");
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");
142 /* check GetClassInfo with our hInstance */
143 if((test_atom = GetClassInfoW(hInstance, str, &wc)))
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");
158 else
159 ok(FALSE,"GetClassInfo (hinstance) failed!\n");
161 /* check GetClassInfo with zero hInstance */
162 if(global)
164 if((test_atom = GetClassInfoW(0, str, &wc)))
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);
179 else
180 ok(FALSE,"GetClassInfo (0) failed for global class!\n");
182 else
184 ok(!GetClassInfoW(0, str, &wc),
185 "GetClassInfo (0) succeeded for local class!\n");
188 ok(!UnregisterClassW(className, hInstance),
189 "Unregister class succeeded with window existing\n");
191 ok(DestroyWindow(hTestWnd),
192 "DestroyWindow() failed!\n");
194 ok(UnregisterClassW(className, hInstance),
195 "UnregisterClass() failed\n");
197 return;
200 static void check_style( const char *name, int must_exist, UINT style, UINT ignore )
202 WNDCLASSA wc;
204 if (GetClassInfoA( 0, name, &wc ))
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 );
211 else
212 ok( !must_exist, "System class %s does not exist\n", name );
215 /* test styles of system classes */
216 static void test_styles(void)
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 */
236 static void check_class_(int line, HINSTANCE inst, const char *name, const char *menu_name)
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)
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 );
252 #define check_class(inst,name,menu) check_class_(__LINE__,inst,name,menu)
254 static void check_instance_( int line, const char *name, HINSTANCE inst,
255 HINSTANCE info_inst, HINSTANCE gcl_inst )
257 WNDCLASSA wc;
258 HWND hwnd;
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);
277 #define check_instance(name,inst,info_inst,gcl_inst) check_instance_(__LINE__,name,inst,info_inst,gcl_inst)
279 struct class_info
281 const char *name;
282 HINSTANCE inst, info_inst, gcl_inst;
285 static DWORD WINAPI thread_proc(void *param)
287 struct class_info *class_info = param;
289 check_instance(class_info->name, class_info->inst, class_info->info_inst, class_info->gcl_inst);
291 return 0;
294 static void check_thread_instance( const char *name, HINSTANCE inst, HINSTANCE info_inst, HINSTANCE gcl_inst )
296 HANDLE hThread;
297 DWORD tid;
298 struct class_info class_info;
300 class_info.name = name;
301 class_info.inst = inst;
302 class_info.info_inst = info_inst;
303 class_info.gcl_inst = gcl_inst;
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);
311 /* test various instance parameters */
312 static void test_instances(void)
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];
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;
332 cls.lpszMenuName = "main_module";
333 cls.hInstance = main_module;
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 );
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" );
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);
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");
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");
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");
387 cls.cbClsExtra = 0;
388 cls.cbWndExtra = 0;
389 SetLastError(0xdeadbeef);
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" );
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" );
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
421 check_instance( name, 0, 0, kernel32 );
422 check_thread_instance( name, 0, 0, kernel32 );
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" );
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" );
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" );
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 );
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 );
463 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
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 );
470 SetClassLongPtrA( hwnd, GCLP_HMODULE, (LONG_PTR)main_module );
471 DestroyWindow( hwnd );
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" );
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" );
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() );
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" );
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() );
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 );
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 );
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() );
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" );
547 /* check system classes */
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() );
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 );
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 */
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);
600 static void test_builtinproc(void)
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 */
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;
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");
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
638 for (i = 0; i < 4; i++)
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;
649 if (i & 2)
651 cls.lpszClassName = classA;
652 atom = RegisterClassExA(&cls);
654 else
656 cls.lpszClassName = (LPCSTR)classW;
657 atom = RegisterClassExW((WNDCLASSEXW *)&cls);
659 ok(atom != 0, "Couldn't register class, i=%d, %d\n", i, GetLastError());
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);
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);
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);
674 DestroyWindow(hwnd);
675 UnregisterClassA((LPSTR)(DWORD_PTR)atom, GetModuleHandleA(NULL));
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);
688 hwnd = CreateWindowExW(0, classW, NULL, WS_OVERLAPPEDWINDOW,
689 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleW(NULL), 0);
690 ok(IsWindowUnicode(hwnd), "Windows should be Unicode\n");
691 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)pDefWindowProcA);
692 ok(IsWindowUnicode(hwnd), "Windows should have remained Unicode\n");
693 if (GetWindowLongPtrW(hwnd, GWLP_WNDPROC) == (LONG_PTR)pDefWindowProcA)
695 /* DefWindowProc isn't magic on wow64 */
696 ok(IS_WNDPROC_HANDLE(GetWindowLongPtrA(hwnd, GWLP_WNDPROC)), "Ansi winproc is not a handle\n");
698 else
700 ok(GetWindowLongPtrW(hwnd, GWLP_WNDPROC) == (LONG_PTR)pDefWindowProcW, "Invalid Unicode winproc\n");
701 ok(GetWindowLongPtrA(hwnd, GWLP_WNDPROC) == (LONG_PTR)pDefWindowProcA, "Invalid Ansi winproc\n");
703 SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
704 ok(IsWindowUnicode(hwnd) == FALSE, "SetWindowLongPtrA should have switched window to ANSI\n");
706 DestroyWindow(hwnd);
707 UnregisterClassA((LPSTR)(DWORD_PTR)atom, GetModuleHandleA(NULL));
709 /* custom winproc - the same function can be used as both A and W*/
710 ZeroMemory(&cls, sizeof(cls));
711 cls.cbSize = sizeof(cls);
712 cls.hInstance = GetModuleHandleA(NULL);
713 cls.hbrBackground = GetStockObject (WHITE_BRUSH);
714 cls.lpszClassName = classA;
715 cls.lpfnWndProc = ClassTest_WndProc2;
716 atom = RegisterClassExA(&cls);
718 hwnd = CreateWindowExW(0, classW, NULL, WS_OVERLAPPEDWINDOW,
719 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
720 ok(IsWindowUnicode(hwnd) == FALSE, "Window should be ANSI\n");
721 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
722 ok(IsWindowUnicode(hwnd), "SetWindowLongPtrW should have changed window to Unicode\n");
723 SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
724 ok(IsWindowUnicode(hwnd) == FALSE, "SetWindowLongPtrA should have changed window to ANSI\n");
726 DestroyWindow(hwnd);
727 UnregisterClassA((LPSTR)(DWORD_PTR)atom, GetModuleHandleA(NULL));
729 /* For most of the builtin controls both GetWindowLongPtrA and W returns a pointer that is executed directly
730 * by CallWindowProcA/W */
731 for (i = 0; i < NUM_NORMAL_CLASSES; i++)
733 WNDPROC procA, procW;
734 hwnd = CreateWindowExA(0, NORMAL_CLASSES[i], classA, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 680, 260,
735 NULL, NULL, NULL, 0);
736 ok(hwnd != NULL, "Couldn't create window of class %s\n", NORMAL_CLASSES[i]);
737 SetWindowTextA(hwnd, classA); /* ComboBox needs this */
738 procA = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC);
739 procW = (WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC);
740 ok(!IS_WNDPROC_HANDLE(procA), "procA should not be a handle for %s (%p)\n", NORMAL_CLASSES[i], procA);
741 ok(!IS_WNDPROC_HANDLE(procW), "procW should not be a handle for %s (%p)\n", NORMAL_CLASSES[i], procW);
742 CallWindowProcA(procA, hwnd, WM_GETTEXT, 120, (LPARAM)buf);
743 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT A/A invalid return for class %s\n", NORMAL_CLASSES[i]);
744 CallWindowProcA(procW, hwnd, WM_GETTEXT, 120, (LPARAM)buf);
745 ok(memcmp(buf, classW, sizeof(classW)) == 0, "WM_GETTEXT A/W invalid return for class %s\n", NORMAL_CLASSES[i]);
746 CallWindowProcW(procA, hwnd, WM_GETTEXT, 120, (LPARAM)buf);
747 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT W/A invalid return for class %s\n", NORMAL_CLASSES[i]);
748 CallWindowProcW(procW, hwnd, WM_GETTEXT, 120, (LPARAM)buf);
749 ok(memcmp(buf, classW, sizeof(classW)) == 0, "WM_GETTEXT W/W invalid return for class %s\n", NORMAL_CLASSES[i]);
751 oldproc = (WNDPROC)SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
752 ok(IS_WNDPROC_HANDLE(oldproc) == FALSE, "Class %s shouldn't return a handle\n", NORMAL_CLASSES[i]);
753 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)oldproc);
754 DestroyWindow(hwnd);
757 /* Edit controls are special - they return a wndproc handle when GetWindowLongPtr is called with a different A/W.
758 * On the other hand there is no W->A->W conversion so this control is treated specially. */
759 hwnd = CreateWindowW(WC_EDITW, unistring, WS_OVERLAPPEDWINDOW,
760 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, NULL, 0);
761 /* GetClassLongPtr returns that both the Unicode and ANSI wndproc */
762 ok(IS_WNDPROC_HANDLE(GetClassLongPtrA(hwnd, GCLP_WNDPROC)) == FALSE, "Edit control class should have a Unicode wndproc\n");
763 ok(IS_WNDPROC_HANDLE(GetClassLongPtrW(hwnd, GCLP_WNDPROC)) == FALSE, "Edit control class should have a ANSI wndproc\n");
764 /* But GetWindowLongPtr returns only a handle for the ANSI one */
765 ok(IS_WNDPROC_HANDLE(GetWindowLongPtrA(hwnd, GWLP_WNDPROC)), "Edit control should return a wndproc handle\n");
766 ok(!IS_WNDPROC_HANDLE(GetWindowLongPtrW(hwnd, GWLP_WNDPROC)), "Edit control shouldn't return a W wndproc handle\n");
767 CallWindowProcW((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
768 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
769 CallWindowProcA((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
770 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
771 CallWindowProcW((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
772 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
774 SetWindowTextW(hwnd, classW);
775 CallWindowProcA((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
776 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
778 oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc2);
779 /* SetWindowLongPtr returns a wndproc handle - like GetWindowLongPtr */
780 ok(IS_WNDPROC_HANDLE(oldproc), "Edit control should return a wndproc handle\n");
781 ok(IsWindowUnicode(hwnd) == FALSE, "SetWindowLongPtrA should have changed window to ANSI\n");
782 SetWindowTextA(hwnd, classA); /* Windows resets the title to WideStringToMultiByte(unistring) */
783 memset(buf, 0, sizeof(buf));
784 CallWindowProcA((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
785 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
786 CallWindowProcA((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
787 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
788 CallWindowProcW((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
789 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
791 CallWindowProcW((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
792 ok(memcmp(buf, classW, sizeof(classW)) == 0, "WM_GETTEXT invalid return\n");
794 SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)oldproc);
796 DestroyWindow(hwnd);
798 hwnd = CreateWindowA(WC_EDITA, classA, WS_OVERLAPPEDWINDOW,
799 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, NULL, 0);
801 /* GetClassLongPtr returns that both the Unicode and ANSI wndproc */
802 ok(!IS_WNDPROC_HANDLE(GetClassLongPtrA(hwnd, GCLP_WNDPROC)), "Edit control class should have a Unicode wndproc\n");
803 ok(!IS_WNDPROC_HANDLE(GetClassLongPtrW(hwnd, GCLP_WNDPROC)), "Edit control class should have a ANSI wndproc\n");
804 /* But GetWindowLongPtr returns only a handle for the Unicode one */
805 ok(!IS_WNDPROC_HANDLE(GetWindowLongPtrA(hwnd, GWLP_WNDPROC)), "Edit control shouldn't return an A wndproc handle\n");
806 ok(IS_WNDPROC_HANDLE(GetWindowLongPtrW(hwnd, GWLP_WNDPROC)), "Edit control should return a wndproc handle\n");
807 CallWindowProcA((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
808 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
809 CallWindowProcA((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
810 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
811 CallWindowProcW((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
812 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
814 CallWindowProcW((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
815 ok(memcmp(buf, classW, sizeof(classW)) == 0, "WM_GETTEXT invalid return\n");
817 oldproc = (WNDPROC)SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
818 SetWindowTextW(hwnd, unistring);
819 CallWindowProcW((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
820 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
821 CallWindowProcA((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
822 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
823 CallWindowProcW((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
824 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
826 SetWindowTextW(hwnd, classW);
827 CallWindowProcA((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
828 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
830 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)oldproc);
832 DestroyWindow(hwnd);
836 static LRESULT WINAPI TestDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
838 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
841 static BOOL RegisterTestDialog(HINSTANCE hInstance)
843 WNDCLASSEXA wcx;
844 ATOM atom = 0;
846 ZeroMemory(&wcx, sizeof(WNDCLASSEXA));
847 wcx.cbSize = sizeof(wcx);
848 wcx.lpfnWndProc = TestDlgProc;
849 wcx.cbClsExtra = 0;
850 wcx.cbWndExtra = DLGWINDOWEXTRA;
851 wcx.hInstance = hInstance;
852 wcx.hIcon = LoadIconA(NULL, (LPCSTR)IDI_APPLICATION);
853 wcx.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
854 wcx.hbrBackground = GetStockObject(WHITE_BRUSH);
855 wcx.lpszClassName = "TestDialog";
856 wcx.lpszMenuName = "TestDialog";
857 wcx.hIconSm = LoadImageA(hInstance, (LPCSTR)MAKEINTRESOURCE(5), IMAGE_ICON,
858 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
859 LR_DEFAULTCOLOR);
861 atom = RegisterClassExA(&wcx);
862 ok(atom != 0, "RegisterClassEx returned 0\n");
864 return atom;
867 /* test registering a dialog box created by using the CLASS directive in a
868 resource file, then test creating the dialog using CreateDialogParam. */
869 static void CreateDialogParamTest(HINSTANCE hInstance)
871 HWND hWndMain;
873 if (RegisterTestDialog(hInstance))
875 hWndMain = CreateDialogParamA(hInstance, "CLASS_TEST_DIALOG", NULL, 0, 0);
876 ok(hWndMain != NULL, "CreateDialogParam returned NULL\n");
877 ShowWindow(hWndMain, SW_SHOW);
878 DestroyWindow(hWndMain);
882 static const struct
884 const char name[9];
885 int value;
886 int badvalue;
887 } extra_values[] =
889 {"#32770",30,30}, /* Dialog */
890 #ifdef _WIN64
891 {"Edit",8,8},
892 #else
893 {"Edit",6,8}, /* Windows XP 64-bit returns 8 also to 32-bit applications */
894 #endif
897 static void test_extra_values(void)
899 int i;
900 for(i=0; i< sizeof(extra_values)/sizeof(extra_values[0]); i++)
902 WNDCLASSEXA wcx;
903 BOOL ret = GetClassInfoExA(NULL,extra_values[i].name,&wcx);
905 ok( ret, "GetClassInfo (0) failed for global class %s\n", extra_values[i].name);
906 if (!ret) continue;
907 ok(extra_values[i].value == wcx.cbWndExtra || broken(extra_values[i].badvalue == wcx.cbWndExtra),
908 "expected %d, got %d\n", extra_values[i].value, wcx.cbWndExtra);
912 static void test_GetClassInfo(void)
914 static const WCHAR staticW[] = {'s','t','a','t','i','c',0};
915 WNDCLASSA wc;
916 WNDCLASSEXA wcx;
917 BOOL ret;
919 SetLastError(0xdeadbeef);
920 ret = GetClassInfoA(0, "static", &wc);
921 ok(ret, "GetClassInfoA() error %d\n", GetLastError());
923 if (0) { /* crashes under XP */
924 SetLastError(0xdeadbeef);
925 ret = GetClassInfoA(0, "static", NULL);
926 ok(ret, "GetClassInfoA() error %d\n", GetLastError());
928 SetLastError(0xdeadbeef);
929 ret = GetClassInfoW(0, staticW, NULL);
930 ok(ret, "GetClassInfoW() error %d\n", GetLastError());
933 wcx.cbSize = sizeof(wcx);
934 SetLastError(0xdeadbeef);
935 ret = GetClassInfoExA(0, "static", &wcx);
936 ok(ret, "GetClassInfoExA() error %d\n", GetLastError());
938 SetLastError(0xdeadbeef);
939 ret = GetClassInfoExA(0, "static", NULL);
940 ok(!ret, "GetClassInfoExA() should fail\n");
941 ok(GetLastError() == ERROR_NOACCESS ||
942 broken(GetLastError() == 0xdeadbeef), /* win9x */
943 "expected ERROR_NOACCESS, got %d\n", GetLastError());
945 SetLastError(0xdeadbeef);
946 ret = GetClassInfoExW(0, staticW, NULL);
947 ok(!ret, "GetClassInfoExW() should fail\n");
948 ok(GetLastError() == ERROR_NOACCESS ||
949 broken(GetLastError() == 0xdeadbeef) /* NT4 */ ||
950 broken(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), /* win9x */
951 "expected ERROR_NOACCESS, got %d\n", GetLastError());
953 wcx.cbSize = 0;
954 wcx.lpfnWndProc = NULL;
955 SetLastError(0xdeadbeef);
956 ret = GetClassInfoExA(0, "static", &wcx);
957 ok(ret, "GetClassInfoExA() error %d\n", GetLastError());
958 ok(wcx.cbSize == 0, "expected 0, got %u\n", wcx.cbSize);
959 ok(wcx.lpfnWndProc != NULL, "got null proc\n");
961 wcx.cbSize = sizeof(wcx) - 1;
962 wcx.lpfnWndProc = NULL;
963 SetLastError(0xdeadbeef);
964 ret = GetClassInfoExA(0, "static", &wcx);
965 ok(ret, "GetClassInfoExA() error %d\n", GetLastError());
966 ok(wcx.cbSize == sizeof(wcx) - 1, "expected sizeof(wcx)-1, got %u\n", wcx.cbSize);
967 ok(wcx.lpfnWndProc != NULL, "got null proc\n");
969 wcx.cbSize = sizeof(wcx) + 1;
970 wcx.lpfnWndProc = NULL;
971 SetLastError(0xdeadbeef);
972 ret = GetClassInfoExA(0, "static", &wcx);
973 ok(ret, "GetClassInfoExA() error %d\n", GetLastError());
974 ok(wcx.cbSize == sizeof(wcx) + 1, "expected sizeof(wcx)+1, got %u\n", wcx.cbSize);
975 ok(wcx.lpfnWndProc != NULL, "got null proc\n");
978 static void test_icons(void)
980 WNDCLASSEXW wcex, ret_wcex;
981 WCHAR cls_name[] = {'I','c','o','n','T','e','s','t','C','l','a','s','s',0};
982 HWND hwnd;
983 HINSTANCE hinst = GetModuleHandleW(0);
984 HICON hsmicon, hsmallnew;
985 ICONINFO icinf;
987 memset(&wcex, 0, sizeof wcex);
988 wcex.cbSize = sizeof wcex;
989 wcex.lpfnWndProc = ClassTest_WndProc;
990 wcex.hIcon = LoadIconW(0, (LPCWSTR)IDI_APPLICATION);
991 wcex.hInstance = hinst;
992 wcex.lpszClassName = cls_name;
993 ok(RegisterClassExW(&wcex), "RegisterClassExW returned 0\n");
994 hwnd = CreateWindowExW(0, cls_name, NULL, WS_OVERLAPPEDWINDOW,
995 0, 0, 0, 0, NULL, NULL, hinst, 0);
996 ok(hwnd != NULL, "Window was not created\n");
998 ok(GetClassInfoExW(hinst, cls_name, &ret_wcex), "Class info was not retrieved\n");
999 ok(wcex.hIcon == ret_wcex.hIcon, "Icons don't match\n");
1000 ok(ret_wcex.hIconSm != NULL, "hIconSm should be non-zero handle\n");
1002 hsmicon = (HICON)GetClassLongPtrW(hwnd, GCLP_HICONSM);
1003 ok(hsmicon != NULL, "GetClassLong should return non-zero handle\n");
1005 hsmallnew = CopyImage(wcex.hIcon, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
1006 GetSystemMetrics(SM_CYSMICON), 0);
1007 ok(!SetClassLongPtrW(hwnd, GCLP_HICONSM, (LONG_PTR)hsmallnew),
1008 "Previous hIconSm should be zero\n");
1009 ok(hsmallnew == (HICON)GetClassLongPtrW(hwnd, GCLP_HICONSM),
1010 "Should return explicitly assigned small icon\n");
1011 ok(!GetIconInfo(hsmicon, &icinf), "Previous small icon should be destroyed\n");
1013 SetClassLongPtrW(hwnd, GCLP_HICONSM, 0);
1014 hsmicon = (HICON)GetClassLongPtrW(hwnd, GCLP_HICONSM);
1015 ok( hsmicon != NULL, "GetClassLong should return non-zero handle\n");
1017 SetClassLongPtrW(hwnd, GCLP_HICON, 0);
1018 ok(!GetClassLongPtrW(hwnd, GCLP_HICONSM), "GetClassLong should return zero handle\n");
1020 SetClassLongPtrW(hwnd, GCLP_HICON, (LONG_PTR)LoadIconW(NULL, (LPCWSTR)IDI_QUESTION));
1021 hsmicon = (HICON)GetClassLongPtrW(hwnd, GCLP_HICONSM);
1022 ok(hsmicon != NULL, "GetClassLong should return non-zero handle\n");
1023 UnregisterClassW(cls_name, hinst);
1024 ok(GetIconInfo(hsmicon, &icinf), "Icon should NOT be destroyed\n");
1026 DestroyIcon(hsmallnew);
1027 DestroyWindow(hwnd);
1030 static void test_comctl32_class( const char *name )
1032 WNDCLASSA wcA;
1033 WNDCLASSW wcW;
1034 BOOL ret;
1035 HMODULE module;
1036 WCHAR nameW[20];
1037 HWND hwnd;
1039 module = GetModuleHandleA( "comctl32" );
1040 ok( !module, "comctl32 already loaded\n" );
1041 ret = GetClassInfoA( 0, name, &wcA );
1042 ok( ret || broken(!ret) /* <= winxp */, "GetClassInfoA failed for %s\n", name );
1043 if (!ret) return;
1044 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, sizeof(nameW)/sizeof(WCHAR) );
1045 ret = GetClassInfoW( 0, nameW, &wcW );
1046 ok( ret, "GetClassInfoW failed for %s\n", name );
1047 module = GetModuleHandleA( "comctl32" );
1048 ok( module != 0, "comctl32 not loaded\n" );
1049 FreeLibrary( module );
1050 module = GetModuleHandleA( "comctl32" );
1051 ok( !module, "comctl32 still loaded\n" );
1052 hwnd = CreateWindowA( name, "test", WS_OVERLAPPEDWINDOW, 0, 0, 10, 10, NULL, NULL, NULL, 0 );
1053 ok( hwnd != 0, "failed to create window for %s\n", name );
1054 module = GetModuleHandleA( "comctl32" );
1055 ok( module != 0, "comctl32 not loaded\n" );
1058 /* verify that comctl32 classes are automatically loaded by user32 */
1059 static void test_comctl32_classes(void)
1061 char path_name[MAX_PATH];
1062 PROCESS_INFORMATION info;
1063 STARTUPINFOA startup;
1064 char **argv;
1065 int i;
1067 static const char *classes[] =
1069 ANIMATE_CLASSA,
1070 WC_COMBOBOXEXA,
1071 DATETIMEPICK_CLASSA,
1072 WC_HEADERA,
1073 HOTKEY_CLASSA,
1074 WC_IPADDRESSA,
1075 WC_LISTVIEWA,
1076 MONTHCAL_CLASSA,
1077 WC_NATIVEFONTCTLA,
1078 WC_PAGESCROLLERA,
1079 PROGRESS_CLASSA,
1080 REBARCLASSNAMEA,
1081 STATUSCLASSNAMEA,
1082 WC_TABCONTROLA,
1083 TOOLBARCLASSNAMEA,
1084 TOOLTIPS_CLASSA,
1085 TRACKBAR_CLASSA,
1086 WC_TREEVIEWA,
1087 UPDOWN_CLASSA
1090 winetest_get_mainargs( &argv );
1091 for (i = 0; i < sizeof(classes) / sizeof(classes[0]); i++)
1093 memset( &startup, 0, sizeof(startup) );
1094 startup.cb = sizeof( startup );
1095 sprintf( path_name, "%s class %s", argv[0], classes[i] );
1096 ok( CreateProcessA( NULL, path_name, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info ),
1097 "CreateProcess failed.\n" );
1098 winetest_wait_child_process( info.hProcess );
1099 CloseHandle( info.hProcess );
1100 CloseHandle( info.hThread );
1104 START_TEST(class)
1106 char **argv;
1107 HANDLE hInstance = GetModuleHandleA( NULL );
1108 int argc = winetest_get_mainargs( &argv );
1110 if (argc >= 3)
1112 test_comctl32_class( argv[2] );
1113 return;
1116 test_GetClassInfo();
1117 test_extra_values();
1119 if (!GetModuleHandleW(0))
1121 trace("Class test is incompatible with Win9x implementation, skipping\n");
1122 return;
1125 ClassTest(hInstance,FALSE);
1126 ClassTest(hInstance,TRUE);
1127 CreateDialogParamTest(hInstance);
1128 test_styles();
1129 test_builtinproc();
1130 test_icons();
1131 test_comctl32_classes();
1133 /* this test unregisters the Button class so it should be executed at the end */
1134 test_instances();