user32/tests: Replace GWL_WNDPROC by GWLP_WNDPROC.
[wine/multimedia.git] / dlls / user32 / tests / class.c
bloba988083cf58ffdd592ffd649687f3d7a8561212d
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 <assert.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include "wine/test.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "wingdi.h"
34 #include "winuser.h"
36 /* we don't want to include commctrl.h: */
37 static const CHAR WC_EDITA[] = "Edit";
38 static const WCHAR WC_EDITW[] = {'E','d','i','t',0};
40 #define NUMCLASSWORDS 4
42 static LRESULT WINAPI ClassTest_WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
44 return DefWindowProcW (hWnd, msg, wParam, lParam);
47 static LRESULT WINAPI ClassTest_WndProc2 (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
49 return DefWindowProcA (hWnd, msg, wParam, lParam);
52 /***********************************************************************
54 static void ClassTest(HINSTANCE hInstance, BOOL global)
56 WNDCLASSW cls, wc;
57 static const WCHAR className[] = {'T','e','s','t','C','l','a','s','s',0};
58 static const WCHAR winName[] = {'W','i','n','C','l','a','s','s','T','e','s','t',0};
59 ATOM test_atom;
60 HWND hTestWnd;
61 LONG i;
62 WCHAR str[20];
63 ATOM classatom;
65 cls.style = CS_HREDRAW | CS_VREDRAW | (global?CS_GLOBALCLASS:0);
66 cls.lpfnWndProc = ClassTest_WndProc;
67 cls.cbClsExtra = NUMCLASSWORDS*sizeof(DWORD);
68 cls.cbWndExtra = 12;
69 cls.hInstance = hInstance;
70 cls.hIcon = LoadIconW (0, (LPWSTR)IDI_APPLICATION);
71 cls.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
72 cls.hbrBackground = GetStockObject (WHITE_BRUSH);
73 cls.lpszMenuName = 0;
74 cls.lpszClassName = className;
76 classatom=RegisterClassW(&cls);
77 if (!classatom && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
78 return;
79 ok(classatom, "failed to register class\n");
81 ok(!RegisterClassW (&cls),
82 "RegisterClass of the same class should fail for the second time\n");
84 /* Setup windows */
85 hTestWnd = CreateWindowW (className, winName,
86 WS_OVERLAPPEDWINDOW + WS_HSCROLL + WS_VSCROLL,
87 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0,
88 0, hInstance, 0);
90 ok(hTestWnd!=0, "Failed to create window\n");
92 /* test initial values of valid classwords */
93 for(i=0; i<NUMCLASSWORDS; i++)
95 SetLastError(0);
96 ok(!GetClassLongW(hTestWnd,i*sizeof (DWORD)),
97 "GetClassLongW initial value nonzero!\n");
98 ok(!GetLastError(),
99 "GetClassLongW failed!\n");
102 if (0)
105 * GetClassLongW(hTestWnd, NUMCLASSWORDS*sizeof(DWORD))
106 * does not fail on Win 98, though MSDN says it should
108 SetLastError(0);
109 GetClassLongW(hTestWnd, NUMCLASSWORDS*sizeof(DWORD));
110 ok(GetLastError(),
111 "GetClassLongW() with invalid offset did not fail\n");
114 /* set values of valid class words */
115 for(i=0; i<NUMCLASSWORDS; i++)
117 SetLastError(0);
118 ok(!SetClassLongW(hTestWnd,i*sizeof(DWORD),i+1),
119 "GetClassLongW(%d) initial value nonzero!\n",i);
120 ok(!GetLastError(),
121 "SetClassLongW(%d) failed!\n",i);
124 /* test values of valid classwords that we set */
125 for(i=0; i<NUMCLASSWORDS; i++)
127 SetLastError(0);
128 ok( (i+1) == GetClassLongW(hTestWnd,i*sizeof (DWORD)),
129 "GetClassLongW value doesn't match what was set!\n");
130 ok(!GetLastError(),
131 "GetClassLongW failed!\n");
134 /* check GetClassName */
135 i = GetClassNameW(hTestWnd, str, sizeof(str));
136 ok(i == lstrlenW(className),
137 "GetClassName returned incorrect length\n");
138 ok(!lstrcmpW(className,str),
139 "GetClassName returned incorrect name for this window's class\n");
141 /* check GetClassInfo with our hInstance */
142 if((test_atom = GetClassInfoW(hInstance, str, &wc)))
144 ok(test_atom == classatom,
145 "class atom did not match\n");
146 ok(wc.cbClsExtra == cls.cbClsExtra,
147 "cbClsExtra did not match\n");
148 ok(wc.cbWndExtra == cls.cbWndExtra,
149 "cbWndExtra did not match\n");
150 ok(wc.hbrBackground == cls.hbrBackground,
151 "hbrBackground did not match\n");
152 ok(wc.hCursor== cls.hCursor,
153 "hCursor did not match\n");
154 ok(wc.hInstance== cls.hInstance,
155 "hInstance did not match\n");
157 else
158 ok(FALSE,"GetClassInfo (hinstance) failed!\n");
160 /* check GetClassInfo with zero hInstance */
161 if(global)
163 if((test_atom = GetClassInfoW(0, str, &wc)))
165 ok(test_atom == classatom,
166 "class atom did not match %x != %x\n", test_atom, classatom);
167 ok(wc.cbClsExtra == cls.cbClsExtra,
168 "cbClsExtra did not match %x!=%x\n",wc.cbClsExtra,cls.cbClsExtra);
169 ok(wc.cbWndExtra == cls.cbWndExtra,
170 "cbWndExtra did not match %x!=%x\n",wc.cbWndExtra,cls.cbWndExtra);
171 ok(wc.hbrBackground == cls.hbrBackground,
172 "hbrBackground did not match %p!=%p\n",wc.hbrBackground,cls.hbrBackground);
173 ok(wc.hCursor== cls.hCursor,
174 "hCursor did not match %p!=%p\n",wc.hCursor,cls.hCursor);
175 ok(!wc.hInstance,
176 "hInstance not zero for global class %p\n",wc.hInstance);
178 else
179 ok(FALSE,"GetClassInfo (0) failed for global class!\n");
181 else
183 ok(!GetClassInfoW(0, str, &wc),
184 "GetClassInfo (0) succeeded for local class!\n");
187 ok(!UnregisterClassW(className, hInstance),
188 "Unregister class succeeded with window existing\n");
190 ok(DestroyWindow(hTestWnd),
191 "DestroyWindow() failed!\n");
193 ok(UnregisterClassW(className, hInstance),
194 "UnregisterClass() failed\n");
196 return;
199 static void check_style( const char *name, int must_exist, UINT style, UINT ignore )
201 WNDCLASS wc;
203 if (GetClassInfo( 0, name, &wc ))
205 ok( !(~wc.style & style & ~ignore), "System class %s is missing bits %x (%08x/%08x)\n",
206 name, ~wc.style & style, wc.style, style );
207 ok( !(wc.style & ~style), "System class %s has extra bits %x (%08x/%08x)\n",
208 name, wc.style & ~style, wc.style, style );
210 else
211 ok( !must_exist, "System class %s does not exist\n", name );
214 /* test styles of system classes */
215 static void test_styles(void)
217 /* check style bits */
218 check_style( "Button", 1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, 0 );
219 check_style( "ComboBox", 1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, 0 );
220 check_style( "Edit", 1, CS_PARENTDC | CS_DBLCLKS, 0 );
221 check_style( "ListBox", 1, CS_PARENTDC | CS_DBLCLKS, CS_PARENTDC /*FIXME*/ );
222 check_style( "MDIClient", 1, 0, 0 );
223 check_style( "ScrollBar", 1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, 0 );
224 check_style( "Static", 1, CS_PARENTDC | CS_DBLCLKS, 0 );
225 check_style( "ComboLBox", 1, CS_SAVEBITS | CS_DBLCLKS, 0 );
226 check_style( "DDEMLEvent", 0, 0, 0 );
227 check_style( "Message", 0, 0, 0 );
228 check_style( "#32768", 1, CS_DROPSHADOW | CS_SAVEBITS | CS_DBLCLKS, CS_DROPSHADOW ); /* menu */
229 check_style( "#32769", 1, CS_DBLCLKS, 0 ); /* desktop */
230 check_style( "#32770", 1, CS_SAVEBITS | CS_DBLCLKS, 0 ); /* dialog */
231 todo_wine { check_style( "#32771", 1, CS_SAVEBITS | CS_HREDRAW | CS_VREDRAW, 0 ); } /* task switch */
232 check_style( "#32772", 1, 0, 0 ); /* icon title */
235 static void check_class(HINSTANCE inst, const char *name, const char *menu_name)
237 WNDCLASS wc;
238 UINT atom = GetClassInfo(inst,name,&wc);
239 ok( atom, "Class %s %p not found\n", name, inst );
240 if (atom)
242 if (wc.lpszMenuName && menu_name)
243 ok( !strcmp( menu_name, wc.lpszMenuName ), "Wrong name %s/%s for class %s %p\n",
244 wc.lpszMenuName, menu_name, name, inst );
245 else
246 ok( !menu_name == !wc.lpszMenuName, "Wrong name %p/%p for class %s %p\n",
247 wc.lpszMenuName, menu_name, name, inst );
251 static void check_instance( const char *name, HINSTANCE inst, HINSTANCE info_inst, HINSTANCE gcl_inst )
253 WNDCLASSA wc;
254 HWND hwnd;
256 ok( GetClassInfo( inst, name, &wc ), "Couldn't find class %s inst %p\n", name, inst );
257 ok( wc.hInstance == info_inst, "Wrong info instance %p/%p for class %s\n",
258 wc.hInstance, info_inst, name );
259 hwnd = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, inst, 0 );
260 ok( hwnd != NULL, "Couldn't create window for class %s inst %p\n", name, inst );
261 ok( (HINSTANCE)GetClassLongPtrA( hwnd, GCLP_HMODULE ) == gcl_inst,
262 "Wrong GCL instance %p/%p for class %s\n",
263 (HINSTANCE)GetClassLongPtrA( hwnd, GCLP_HMODULE ), gcl_inst, name );
264 ok( (HINSTANCE)GetWindowLongPtrA( hwnd, GWLP_HINSTANCE ) == inst,
265 "Wrong GWL instance %p/%p for window %s\n",
266 (HINSTANCE)GetWindowLongPtrA( hwnd, GWLP_HINSTANCE ), inst, name );
267 ok(!UnregisterClassA(name, inst), "UnregisterClassA should fail while exists a class window\n");
268 ok(GetLastError() == ERROR_CLASS_HAS_WINDOWS, "GetLastError() should be set to ERROR_CLASS_HAS_WINDOWS not %d\n", GetLastError());
269 DestroyWindow(hwnd);
272 struct class_info
274 const char *name;
275 HINSTANCE inst, info_inst, gcl_inst;
278 static DWORD WINAPI thread_proc(void *param)
280 struct class_info *class_info = (struct class_info *)param;
282 check_instance(class_info->name, class_info->inst, class_info->info_inst, class_info->gcl_inst);
284 return 0;
287 static void check_thread_instance( const char *name, HINSTANCE inst, HINSTANCE info_inst, HINSTANCE gcl_inst )
289 HANDLE hThread;
290 DWORD tid;
291 struct class_info class_info;
293 class_info.name = name;
294 class_info.inst = inst;
295 class_info.info_inst = info_inst;
296 class_info.gcl_inst = gcl_inst;
298 hThread = CreateThread(NULL, 0, thread_proc, &class_info, 0, &tid);
299 ok(hThread != NULL, "CreateThread failed, error %d\n", GetLastError());
300 ok(WaitForSingleObject(hThread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
301 CloseHandle(hThread);
304 /* test various instance parameters */
305 static void test_instances(void)
307 WNDCLASSA cls, wc;
308 HWND hwnd, hwnd2;
309 const char *name = "__test__";
310 HINSTANCE kernel32 = GetModuleHandleA("kernel32");
311 HINSTANCE user32 = GetModuleHandleA("user32");
312 HINSTANCE main_module = GetModuleHandleA(NULL);
313 DWORD r;
314 char buffer[0x10];
316 memset( &cls, 0, sizeof(cls) );
317 cls.style = CS_HREDRAW | CS_VREDRAW;
318 cls.lpfnWndProc = ClassTest_WndProc;
319 cls.cbClsExtra = 0;
320 cls.cbWndExtra = 0;
321 cls.lpszClassName = name;
323 cls.lpszMenuName = "main_module";
324 cls.hInstance = main_module;
326 ok( RegisterClassA( &cls ), "Failed to register local class for main module\n" );
327 check_class( main_module, name, "main_module" );
328 check_instance( name, main_module, main_module, main_module );
329 check_thread_instance( name, main_module, main_module, main_module );
331 cls.lpszMenuName = "kernel32";
332 cls.hInstance = kernel32;
333 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
334 check_class( kernel32, name, "kernel32" );
335 check_class( main_module, name, "main_module" );
336 check_instance( name, kernel32, kernel32, kernel32 );
337 check_thread_instance( name, kernel32, kernel32, kernel32 );
338 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
340 /* Bug 2631 - Supplying an invalid number of bytes fails */
341 cls.cbClsExtra = 0;
342 cls.cbWndExtra = -1;
343 SetLastError(0xdeadbeef);
344 ok( ((RegisterClassA( &cls ) == 0) && (GetLastError() == ERROR_INVALID_PARAMETER)),
345 "Failed with invalid number of WndExtra bytes\n");
347 cls.cbClsExtra = -1;
348 cls.cbWndExtra = 0;
349 SetLastError(0xdeadbeef);
350 ok( ((RegisterClassA( &cls ) == 0) && (GetLastError() == ERROR_INVALID_PARAMETER)),
351 "Failed with invalid number of ClsExtra bytes\n");
353 cls.cbClsExtra = -1;
354 cls.cbWndExtra = -1;
355 SetLastError(0xdeadbeef);
356 ok( ((RegisterClassA( &cls ) == 0) && (GetLastError() == ERROR_INVALID_PARAMETER)),
357 "Failed with invalid number of ClsExtra and cbWndExtra bytes\n");
359 cls.cbClsExtra = 0;
360 cls.cbWndExtra = 0;
361 SetLastError(0xdeadbeef);
363 /* setting global flag doesn't change status of class */
364 hwnd = CreateWindowExA( 0, name, "test", 0, 0, 0, 0, 0, 0, 0, main_module, 0 );
365 SetClassLongA( hwnd, GCL_STYLE, CS_GLOBALCLASS );
366 cls.lpszMenuName = "kernel32";
367 cls.hInstance = kernel32;
368 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
369 check_class( kernel32, name, "kernel32" );
370 check_class( main_module, name, "main_module" );
371 check_instance( name, kernel32, kernel32, kernel32 );
372 check_instance( name, main_module, main_module, main_module );
373 check_thread_instance( name, kernel32, kernel32, kernel32 );
374 check_thread_instance( name, main_module, main_module, main_module );
375 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
377 /* changing the instance doesn't make it global */
378 SetClassLongPtrA( hwnd, GCLP_HMODULE, 0 );
379 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
380 check_class( kernel32, name, "kernel32" );
381 check_instance( name, kernel32, kernel32, kernel32 );
382 check_thread_instance( name, kernel32, kernel32, kernel32 );
383 ok( !GetClassInfo( 0, name, &wc ), "Class found with null instance\n" );
384 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
386 /* GetClassInfo with instance 0 finds user32 instance */
387 SetClassLongPtrA( hwnd, GCLP_HMODULE, (LONG_PTR)user32 );
388 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
389 check_class( kernel32, name, "kernel32" );
390 check_class( user32, name, "main_module" );
391 check_class( 0, name, "main_module" );
392 check_instance( name, kernel32, kernel32, kernel32 );
393 check_instance( name, user32, 0, user32 );
394 check_instance( name, 0, 0, kernel32 );
395 check_thread_instance( name, kernel32, kernel32, kernel32 );
396 check_thread_instance( name, user32, 0, user32 );
397 check_thread_instance( name, 0, 0, kernel32 );
398 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
400 SetClassLongPtrA( hwnd, GCLP_HMODULE, 0x12345678 );
401 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
402 check_class( kernel32, name, "kernel32" );
403 check_class( (HINSTANCE)0x12345678, name, "main_module" );
404 check_instance( name, kernel32, kernel32, kernel32 );
405 check_instance( name, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678 );
406 check_thread_instance( name, kernel32, kernel32, kernel32 );
407 check_thread_instance( name, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678 );
408 ok( !GetClassInfo( 0, name, &wc ), "Class found with null instance\n" );
410 /* creating a window with instance 0 uses the first class found */
411 cls.hInstance = (HINSTANCE)0xdeadbeef;
412 cls.lpszMenuName = "deadbeef";
413 cls.style = 3;
414 ok( RegisterClassA( &cls ), "Failed to register local class for deadbeef\n" );
415 hwnd2 = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, NULL, 0 );
416 ok( (HINSTANCE)GetClassLongPtrA( hwnd2, GCLP_HMODULE ) == (HINSTANCE)0xdeadbeef,
417 "Didn't get deadbeef class for null instance\n" );
418 DestroyWindow( hwnd2 );
419 ok( UnregisterClassA( name, (HINSTANCE)0xdeadbeef ), "Unregister failed for deadbeef\n" );
421 hwnd2 = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, NULL, 0 );
422 ok( (HINSTANCE)GetClassLongPtrA( hwnd2, GCLP_HMODULE ) == kernel32,
423 "Didn't get kernel32 class for null instance\n" );
424 DestroyWindow( hwnd2 );
426 r = GetClassName( hwnd, buffer, 4 );
427 ok( r == 3, "expected 3, got %d\n", r );
428 ok( !strcmp( buffer, "__t"), "name wrong: %s\n", buffer );
430 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
432 hwnd2 = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, NULL, 0 );
433 ok( GetClassLongPtrA( hwnd2, GCLP_HMODULE ) == 0x12345678,
434 "Didn't get 12345678 class for null instance\n" );
435 DestroyWindow( hwnd2 );
437 SetClassLongPtrA( hwnd, GCLP_HMODULE, (LONG_PTR)main_module );
438 DestroyWindow( hwnd );
440 /* null handle means the same thing as main module */
441 cls.lpszMenuName = "null";
442 cls.hInstance = 0;
443 ok( !RegisterClassA( &cls ), "Succeeded registering local class for null instance\n" );
444 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %d\n", GetLastError() );
445 ok( UnregisterClassA( name, main_module ), "Unregister failed for main module\n" );
447 ok( RegisterClassA( &cls ), "Failed to register local class for null instance\n" );
448 /* must be found with main module handle */
449 check_class( main_module, name, "null" );
450 check_instance( name, main_module, main_module, main_module );
451 check_thread_instance( name, main_module, main_module, main_module );
452 ok( !GetClassInfo( 0, name, &wc ), "Class found with null instance\n" );
453 ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %d\n", GetLastError() );
454 ok( UnregisterClassA( name, 0 ), "Unregister failed for null instance\n" );
456 /* registering for user32 always fails */
457 cls.lpszMenuName = "user32";
458 cls.hInstance = user32;
459 ok( !RegisterClassA( &cls ), "Succeeded registering local class for user32\n" );
460 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error code %d\n", GetLastError() );
461 cls.style |= CS_GLOBALCLASS;
462 ok( !RegisterClassA( &cls ), "Succeeded registering global class for user32\n" );
463 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error code %d\n", GetLastError() );
465 /* unregister is OK though */
466 cls.hInstance = main_module;
467 ok( RegisterClassA( &cls ), "Failed to register global class for main module\n" );
468 ok( UnregisterClassA( name, user32 ), "Unregister failed for user32\n" );
470 /* instance doesn't matter for global class */
471 cls.style |= CS_GLOBALCLASS;
472 cls.lpszMenuName = "main_module";
473 cls.hInstance = main_module;
474 ok( RegisterClassA( &cls ), "Failed to register global class for main module\n" );
475 cls.lpszMenuName = "kernel32";
476 cls.hInstance = kernel32;
477 ok( !RegisterClassA( &cls ), "Succeeded registering local class for kernel32\n" );
478 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %d\n", GetLastError() );
479 /* even if global flag is cleared */
480 hwnd = CreateWindowExA( 0, name, "test", 0, 0, 0, 0, 0, 0, 0, main_module, 0 );
481 SetClassLongA( hwnd, GCL_STYLE, 0 );
482 ok( !RegisterClassA( &cls ), "Succeeded registering local class for kernel32\n" );
483 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %d\n", GetLastError() );
485 check_class( main_module, name, "main_module" );
486 check_class( kernel32, name, "main_module" );
487 check_class( 0, name, "main_module" );
488 check_class( (HINSTANCE)0x12345678, name, "main_module" );
489 check_instance( name, main_module, main_module, main_module );
490 check_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, main_module );
491 check_thread_instance( name, main_module, main_module, main_module );
492 check_thread_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, main_module );
494 /* changing the instance for global class doesn't make much difference */
495 SetClassLongPtrA( hwnd, GCLP_HMODULE, 0xdeadbeef );
496 check_instance( name, main_module, main_module, (HINSTANCE)0xdeadbeef );
497 check_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef );
498 check_thread_instance( name, main_module, main_module, (HINSTANCE)0xdeadbeef );
499 check_thread_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef );
501 DestroyWindow( hwnd );
502 ok( UnregisterClassA( name, (HINSTANCE)0x87654321 ), "Unregister failed for main module global\n" );
503 ok( !UnregisterClassA( name, (HINSTANCE)0x87654321 ), "Unregister succeeded the second time\n" );
504 ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %d\n", GetLastError() );
506 cls.hInstance = (HINSTANCE)0x12345678;
507 ok( RegisterClassA( &cls ), "Failed to register global class for dummy instance\n" );
508 check_instance( name, main_module, main_module, (HINSTANCE)0x12345678 );
509 check_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0x12345678 );
510 check_thread_instance( name, main_module, main_module, (HINSTANCE)0x12345678 );
511 check_thread_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0x12345678 );
512 ok( UnregisterClassA( name, (HINSTANCE)0x87654321 ), "Unregister failed for main module global\n" );
514 /* check system classes */
516 /* we cannot register a global class with the name of a system class */
517 cls.style |= CS_GLOBALCLASS;
518 cls.lpszMenuName = "button_main_module";
519 cls.lpszClassName = "BUTTON";
520 cls.hInstance = main_module;
521 ok( !RegisterClassA( &cls ), "Succeeded registering global button class for main module\n" );
522 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %d\n", GetLastError() );
523 cls.hInstance = kernel32;
524 ok( !RegisterClassA( &cls ), "Succeeded registering global button class for kernel32\n" );
525 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %d\n", GetLastError() );
527 /* local class is OK however */
528 cls.style &= ~CS_GLOBALCLASS;
529 cls.lpszMenuName = "button_main_module";
530 cls.hInstance = main_module;
531 ok( RegisterClassA( &cls ), "Failed to register local button class for main module\n" );
532 check_class( main_module, "BUTTON", "button_main_module" );
533 cls.lpszMenuName = "button_kernel32";
534 cls.hInstance = kernel32;
535 ok( RegisterClassA( &cls ), "Failed to register local button class for kernel32\n" );
536 check_class( kernel32, "BUTTON", "button_kernel32" );
537 check_class( main_module, "BUTTON", "button_main_module" );
538 ok( UnregisterClassA( "BUTTON", kernel32 ), "Unregister failed for kernel32 button\n" );
539 ok( UnregisterClassA( "BUTTON", main_module ), "Unregister failed for main module button\n" );
540 /* GetClassInfo sets instance to passed value for global classes */
541 check_instance( "BUTTON", 0, 0, user32 );
542 check_instance( "BUTTON", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
543 check_instance( "BUTTON", user32, 0, user32 );
544 check_thread_instance( "BUTTON", 0, 0, user32 );
545 check_thread_instance( "BUTTON", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
546 check_thread_instance( "BUTTON", user32, 0, user32 );
548 /* we can unregister system classes */
549 ok( GetClassInfo( 0, "BUTTON", &wc ), "Button class not found with null instance\n" );
550 ok( GetClassInfo( kernel32, "BUTTON", &wc ), "Button class not found with kernel32\n" );
551 ok( UnregisterClass( "BUTTON", (HINSTANCE)0x12345678 ), "Failed to unregister button\n" );
552 ok( !UnregisterClass( "BUTTON", (HINSTANCE)0x87654321 ), "Unregistered button a second time\n" );
553 ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %d\n", GetLastError() );
554 ok( !GetClassInfo( 0, "BUTTON", &wc ), "Button still exists\n" );
555 ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST ||
556 GetLastError() == ERROR_INVALID_PARAMETER /* W2K3 */,
557 "Wrong error code %d\n", GetLastError() );
559 /* we can change the instance of a system class */
560 check_instance( "EDIT", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
561 check_thread_instance( "EDIT", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
562 hwnd = CreateWindowExA( 0, "EDIT", "test", 0, 0, 0, 0, 0, 0, 0, main_module, 0 );
563 SetClassLongPtrA( hwnd, GCLP_HMODULE, 0xdeadbeef );
564 check_instance( "EDIT", (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0xdeadbeef );
565 check_thread_instance( "EDIT", (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0xdeadbeef );
568 static void test_defwndproc(void)
570 static const char classA[] = "deftest";
571 static const WCHAR classW[] = {'d','e','f','t','e','s','t',0};
572 WCHAR unistring[] = {0x142, 0x40e, 0x3b4, 0}; /* a string that would be destoryed by a W->A->W conversion */
573 WNDPROC pDefWindowProcA, pDefWindowProcW;
574 WNDCLASSEXA cls; /* the memory layout of WNDCLASSEXA and WNDCLASSEXW is the same */
575 WCHAR buf[128];
576 ATOM atom;
577 HWND hwnd;
578 int i;
580 pDefWindowProcA = (void *)GetProcAddress(GetModuleHandle("user32.dll"), "DefWindowProcA");
581 pDefWindowProcW = (void *)GetProcAddress(GetModuleHandle("user32.dll"), "DefWindowProcW");
583 for (i = 0; i < 4; i++)
585 ZeroMemory(&cls, sizeof(cls));
586 cls.cbSize = sizeof(cls);
587 cls.hInstance = GetModuleHandle(NULL);
588 cls.hbrBackground = GetStockObject (WHITE_BRUSH);
589 if (i & 1)
590 cls.lpfnWndProc = pDefWindowProcA;
591 else
592 cls.lpfnWndProc = pDefWindowProcW;
594 if (i & 2)
596 cls.lpszClassName = classA;
597 atom = RegisterClassExA(&cls);
599 else
601 cls.lpszClassName = (LPCSTR)classW;
602 atom = RegisterClassExW((WNDCLASSEXW *)&cls);
604 ok(atom != 0, "Couldn't register class, i=%d, %d\n", i, GetLastError());
606 hwnd = CreateWindowA(classA, NULL, 0, 0, 0, 100, 100, NULL, NULL, GetModuleHandle(NULL), NULL);
607 ok(hwnd != NULL, "Couldn't create window i=%d\n", i);
609 ok(GetWindowLongPtrA(hwnd, GWLP_WNDPROC) == (LONG_PTR)pDefWindowProcA, "Wrong ANSI wndproc: %p vs %p\n",
610 (void *)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), pDefWindowProcA);
611 ok(GetClassLongPtrA(hwnd, GCLP_WNDPROC) == (ULONG_PTR)pDefWindowProcA, "Wrong ANSI wndproc: %p vs %p\n",
612 (void *)GetClassLongPtrA(hwnd, GCLP_WNDPROC), pDefWindowProcA);
614 ok(GetWindowLongPtrW(hwnd, GWLP_WNDPROC) == (LONG_PTR)pDefWindowProcW, "Wrong Unicode wndproc: %p vs %p\n",
615 (void *)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), pDefWindowProcW);
616 ok(GetClassLongPtrW(hwnd, GCLP_WNDPROC) == (ULONG_PTR)pDefWindowProcW, "Wrong Unicode wndproc: %p vs %p\n",
617 (void *)GetClassLongPtrW(hwnd, GCLP_WNDPROC), pDefWindowProcW);
619 DestroyWindow(hwnd);
620 UnregisterClass((LPSTR)(DWORD_PTR)atom, GetModuleHandle(NULL));
623 /* built-in winproc - window A/W type automatically detected */
624 ZeroMemory(&cls, sizeof(cls));
625 cls.cbSize = sizeof(cls);
626 cls.hInstance = GetModuleHandle(NULL);
627 cls.hbrBackground = GetStockObject (WHITE_BRUSH);
628 cls.lpszClassName = classA;
629 cls.lpfnWndProc = pDefWindowProcW;
630 atom = RegisterClassExA(&cls);
632 hwnd = CreateWindowExW(0, classW, NULL, WS_OVERLAPPEDWINDOW,
633 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
634 ok(IsWindowUnicode(hwnd), "Windows should be Unicode\n");
635 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)pDefWindowProcA);
636 ok(IsWindowUnicode(hwnd), "Windows should have remained Unicode\n");
637 ok(GetWindowLongPtrW(hwnd, GWLP_WNDPROC) == (LONG_PTR)pDefWindowProcW, "Invalid ANSI winproc\n");
638 ok(GetWindowLongPtrA(hwnd, GWLP_WNDPROC) == (LONG_PTR)pDefWindowProcA, "Invalid Unicode winproc\n");
639 SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
640 ok(IsWindowUnicode(hwnd) == FALSE, "SetWindowLongPtrA should have switched window to ANSI\n");
642 DestroyWindow(hwnd);
643 UnregisterClass((LPSTR)(DWORD_PTR)atom, GetModuleHandle(NULL));
645 /* custom winproc - the same function can be used as both A and W*/
646 ZeroMemory(&cls, sizeof(cls));
647 cls.cbSize = sizeof(cls);
648 cls.hInstance = GetModuleHandle(NULL);
649 cls.hbrBackground = GetStockObject (WHITE_BRUSH);
650 cls.lpszClassName = classA;
651 cls.lpfnWndProc = ClassTest_WndProc2;
652 atom = RegisterClassExA(&cls);
654 hwnd = CreateWindowExW(0, classW, NULL, WS_OVERLAPPEDWINDOW,
655 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
656 ok(IsWindowUnicode(hwnd) == FALSE, "Window should be ANSI\n");
657 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
658 ok(IsWindowUnicode(hwnd), "SetWindowLongPtrW should have changed window to Unicode\n");
659 SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
660 ok(IsWindowUnicode(hwnd) == FALSE, "SetWindowLongPtrA should have changed window to ANSI\n");
662 DestroyWindow(hwnd);
663 UnregisterClass((LPSTR)(DWORD_PTR)atom, GetModuleHandle(NULL));
665 /* calling built-in and custom winprocs with CallWindowProc[AW]. Despite
666 * a slightly different nature the end result is the same */
667 hwnd = CreateWindowW(WC_EDITW, unistring, WS_OVERLAPPEDWINDOW,
668 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, NULL, 0);
669 CallWindowProcW((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
670 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
671 CallWindowProcA((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
672 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
673 CallWindowProcW((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
674 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
676 SetWindowTextW(hwnd, classW);
677 CallWindowProcA((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
678 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
680 SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc2);
681 ok(IsWindowUnicode(hwnd) == FALSE, "SetWindowLongPtrA should have changed window to ANSI\n");
682 SetWindowTextA(hwnd, classA); /* Windows resets the title to WideStringToMultiByte(unistring) */
683 memset(buf, 0, sizeof(buf));
684 CallWindowProcA((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
685 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
686 CallWindowProcA((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
687 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
688 CallWindowProcW((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
689 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
691 CallWindowProcW((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
692 ok(memcmp(buf, classW, sizeof(classW)) == 0, "WM_GETTEXT invalid return\n");
694 DestroyWindow(hwnd);
696 hwnd = CreateWindowA(WC_EDITA, classA, WS_OVERLAPPEDWINDOW,
697 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, NULL, 0);
699 CallWindowProcA((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
700 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
701 CallWindowProcA((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
702 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
703 CallWindowProcW((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
704 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
706 CallWindowProcW((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
707 ok(memcmp(buf, classW, sizeof(classW)) == 0, "WM_GETTEXT invalid return\n");
709 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)ClassTest_WndProc);
710 SetWindowTextW(hwnd, unistring);
711 CallWindowProcW((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
712 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
713 CallWindowProcA((WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
714 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
715 CallWindowProcW((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
716 ok(memcmp(buf, unistring, sizeof(unistring)) == 0, "WM_GETTEXT invalid return\n");
718 SetWindowTextW(hwnd, classW);
719 CallWindowProcA((WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC), hwnd, WM_GETTEXT, 120, (LPARAM)buf);
720 ok(memcmp(buf, classA, sizeof(classA)) == 0, "WM_GETTEXT invalid return\n");
722 DestroyWindow(hwnd);
726 static LRESULT WINAPI TestDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
728 return DefWindowProc(hWnd, uMsg, wParam, lParam);
731 static BOOL RegisterTestDialog(HINSTANCE hInstance)
733 WNDCLASSEX wcx;
734 ATOM atom = 0;
736 ZeroMemory(&wcx, sizeof(WNDCLASSEX));
737 wcx.cbSize = sizeof(wcx);
738 wcx.lpfnWndProc = TestDlgProc;
739 wcx.cbClsExtra = 0;
740 wcx.cbWndExtra = DLGWINDOWEXTRA;
741 wcx.hInstance = hInstance;
742 wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
743 wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
744 wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
745 wcx.lpszClassName = "TestDialog";
746 wcx.lpszMenuName = "TestDialog";
747 wcx.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(5),
748 IMAGE_ICON,
749 GetSystemMetrics(SM_CXSMICON),
750 GetSystemMetrics(SM_CYSMICON),
751 LR_DEFAULTCOLOR);
753 atom = RegisterClassEx(&wcx);
754 ok(atom != 0, "RegisterClassEx returned 0\n");
756 return atom;
759 /* test registering a dialog box created by using the CLASS directive in a
760 resource file, then test creating the dialog using CreateDialogParam. */
761 static void WINAPI CreateDialogParamTest(HINSTANCE hInstance)
763 HWND hWndMain;
765 if (RegisterTestDialog(hInstance))
767 hWndMain = CreateDialogParam(hInstance, "CLASS_TEST_DIALOG", NULL, 0, 0);
768 ok(hWndMain != NULL, "CreateDialogParam returned NULL\n");
769 ShowWindow(hWndMain, SW_SHOW);
770 DestroyWindow(hWndMain);
774 START_TEST(class)
776 HANDLE hInstance = GetModuleHandleA( NULL );
778 if (!GetModuleHandleW(0))
780 trace("Class test is incompatible with Win9x implementation, skipping\n");
781 return;
784 ClassTest(hInstance,FALSE);
785 ClassTest(hInstance,TRUE);
786 CreateDialogParamTest(hInstance);
787 test_styles();
788 test_instances();
789 test_defwndproc();