Reverse the order for deleting the items in resetcontent to correctly
[wine] / dlls / user / tests / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /* To get CS_DROPSHADOW with the MSVC headers */
22 #define _WIN32_WINNT 0x0501
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28
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"
35
36 #define NUMCLASSWORDS 4
37
38 static LRESULT WINAPI ClassTest_WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
39 {
40     return DefWindowProcW (hWnd, msg, wParam, lParam);
41 }
42
43 /***********************************************************************
44  */
45 static void ClassTest(HINSTANCE hInstance, BOOL global)
46 {
47     WNDCLASSW cls, wc;
48     static const WCHAR className[] = {'T','e','s','t','C','l','a','s','s',0};
49     static const WCHAR winName[]   = {'W','i','n','C','l','a','s','s','T','e','s','t',0};
50     ATOM test_atom;
51     HWND hTestWnd;
52     LONG i;
53     WCHAR str[20];
54     ATOM classatom;
55
56     cls.style         = CS_HREDRAW | CS_VREDRAW | (global?CS_GLOBALCLASS:0);
57     cls.lpfnWndProc   = ClassTest_WndProc;
58     cls.cbClsExtra    = NUMCLASSWORDS*sizeof(DWORD);
59     cls.cbWndExtra    = 12;
60     cls.hInstance     = hInstance;
61     cls.hIcon         = LoadIconW (0, (LPWSTR)IDI_APPLICATION);
62     cls.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
63     cls.hbrBackground = GetStockObject (WHITE_BRUSH);
64     cls.lpszMenuName  = 0;
65     cls.lpszClassName = className;
66
67     classatom=RegisterClassW(&cls);
68     if (!classatom && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
69         return;
70     ok(classatom, "failed to register class\n");
71
72     ok(!RegisterClassW (&cls),
73         "RegisterClass of the same class should fail for the second time\n");
74
75     /* Setup windows */
76     hTestWnd = CreateWindowW (className, winName,
77        WS_OVERLAPPEDWINDOW + WS_HSCROLL + WS_VSCROLL,
78        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0,
79        0, hInstance, 0);
80
81     ok(hTestWnd!=0, "Failed to create window\n");
82
83     /* test initial values of valid classwords */
84     for(i=0; i<NUMCLASSWORDS; i++)
85     {
86         SetLastError(0);
87         ok(!GetClassLongW(hTestWnd,i*sizeof (DWORD)),
88             "GetClassLongW initial value nonzero!\n");
89         ok(!GetLastError(),
90             "GetClassLongW failed!\n");
91     }
92
93 #if 0
94     /*
95      *  GetClassLongW(hTestWnd, NUMCLASSWORDS*sizeof(DWORD))
96      *  does not fail on Win 98, though MSDN says it should
97      */
98     SetLastError(0);
99     GetClassLongW(hTestWnd, NUMCLASSWORDS*sizeof(DWORD));
100     ok(GetLastError(),
101         "GetClassLongW() with invalid offset did not fail\n");
102 #endif
103
104     /* set values of valid class words */
105     for(i=0; i<NUMCLASSWORDS; i++)
106     {
107         SetLastError(0);
108         ok(!SetClassLongW(hTestWnd,i*sizeof(DWORD),i+1),
109             "GetClassLongW(%ld) initial value nonzero!\n",i*sizeof(DWORD));
110         ok(!GetLastError(),
111             "SetClassLongW(%ld) failed!\n",i*sizeof(DWORD));
112     }
113
114     /* test values of valid classwords that we set */
115     for(i=0; i<NUMCLASSWORDS; i++)
116     {
117         SetLastError(0);
118         ok( (i+1) == GetClassLongW(hTestWnd,i*sizeof (DWORD)),
119             "GetClassLongW value doesn't match what was set!\n");
120         ok(!GetLastError(),
121             "GetClassLongW failed!\n");
122     }
123
124     /* check GetClassName */
125     i = GetClassNameW(hTestWnd, str, sizeof(str));
126     ok(i == lstrlenW(className),
127         "GetClassName returned incorrect length\n");
128     ok(!lstrcmpW(className,str),
129         "GetClassName returned incorrect name for this window's class\n");
130
131     /* check GetClassInfo with our hInstance */
132     if((test_atom = GetClassInfoW(hInstance, str, &wc)))
133     {
134         ok(test_atom == classatom,
135             "class atom did not match\n");
136         ok(wc.cbClsExtra == cls.cbClsExtra,
137             "cbClsExtra did not match\n");
138         ok(wc.cbWndExtra == cls.cbWndExtra,
139             "cbWndExtra did not match\n");
140         ok(wc.hbrBackground == cls.hbrBackground,
141             "hbrBackground did not match\n");
142         ok(wc.hCursor== cls.hCursor,
143             "hCursor did not match\n");
144         ok(wc.hInstance== cls.hInstance,
145             "hInstance did not match\n");
146     }
147     else
148         ok(FALSE,"GetClassInfo (hinstance) failed!\n");
149
150     /* check GetClassInfo with zero hInstance */
151     if(global)
152     {
153         if((test_atom = GetClassInfoW(0, str, &wc)))
154         {
155             ok(test_atom == classatom,
156                 "class atom did not match %x != %x\n", test_atom, classatom);
157             ok(wc.cbClsExtra == cls.cbClsExtra,
158                 "cbClsExtra did not match %x!=%x\n",wc.cbClsExtra,cls.cbClsExtra);
159             ok(wc.cbWndExtra == cls.cbWndExtra,
160                 "cbWndExtra did not match %x!=%x\n",wc.cbWndExtra,cls.cbWndExtra);
161             ok(wc.hbrBackground == cls.hbrBackground,
162                 "hbrBackground did not match %p!=%p\n",wc.hbrBackground,cls.hbrBackground);
163             ok(wc.hCursor== cls.hCursor,
164                 "hCursor did not match %p!=%p\n",wc.hCursor,cls.hCursor);
165             ok(!wc.hInstance,
166                 "hInstance not zero for global class %p\n",wc.hInstance);
167         }
168         else
169             ok(FALSE,"GetClassInfo (0) failed for global class!\n");
170     }
171     else
172     {
173         ok(!GetClassInfoW(0, str, &wc),
174             "GetClassInfo (0) succeeded for local class!\n");
175     }
176
177     ok(!UnregisterClassW(className, hInstance),
178         "Unregister class succeeded with window existing\n");
179
180     ok(DestroyWindow(hTestWnd),
181         "DestroyWindow() failed!\n");
182
183     ok(UnregisterClassW(className, hInstance),
184         "UnregisterClass() failed\n");
185
186     return;
187 }
188
189 static void check_style( const char *name, int must_exist, UINT style, UINT ignore )
190 {
191     WNDCLASS wc;
192
193     if (GetClassInfo( 0, name, &wc ))
194     {
195         ok( !(~wc.style & style & ~ignore), "System class %s is missing bits %x (%08x/%08x)\n",
196             name, ~wc.style & style, wc.style, style );
197         ok( !(wc.style & ~style), "System class %s has extra bits %x (%08x/%08x)\n",
198             name, wc.style & ~style, wc.style, style );
199     }
200     else
201         ok( !must_exist, "System class %s does not exist\n", name );
202 }
203
204 /* test styles of system classes */
205 static void test_styles(void)
206 {
207     /* check style bits */
208     check_style( "Button",     1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, 0 );
209     check_style( "ComboBox",   1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, 0 );
210     check_style( "Edit",       1, CS_PARENTDC | CS_DBLCLKS, 0 );
211     check_style( "ListBox",    1, CS_PARENTDC | CS_DBLCLKS, CS_PARENTDC /*FIXME*/ );
212     check_style( "MDIClient",  1, 0, 0 );
213     check_style( "ScrollBar",  1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, 0 );
214     check_style( "Static",     1, CS_PARENTDC | CS_DBLCLKS, 0 );
215     check_style( "ComboLBox",  1, CS_SAVEBITS | CS_DBLCLKS, 0 );
216     check_style( "DDEMLEvent", 0, 0, 0 );
217     check_style( "Message",    0, 0, 0 );
218     check_style( "#32768",     1, CS_DROPSHADOW | CS_SAVEBITS | CS_DBLCLKS, CS_DROPSHADOW );  /* menu */
219     check_style( "#32769",     1, CS_DBLCLKS, 0 );  /* desktop */
220     check_style( "#32770",     1, CS_SAVEBITS | CS_DBLCLKS, 0 );  /* dialog */
221     todo_wine { check_style( "#32771",     1, CS_SAVEBITS | CS_HREDRAW | CS_VREDRAW, 0 ); } /* task switch */
222     check_style( "#32772",     1, 0, 0 );  /* icon title */
223 }
224
225 static void check_class(HINSTANCE inst, const char *name, const char *menu_name)
226 {
227     WNDCLASS wc;
228     UINT atom = GetClassInfo(inst,name,&wc);
229     ok( atom, "Class %s %p not found\n", name, inst );
230     if (atom)
231     {
232         if (wc.lpszMenuName && menu_name)
233             ok( !strcmp( menu_name, wc.lpszMenuName ), "Wrong name %s/%s for class %s %p\n",
234                 wc.lpszMenuName, menu_name, name, inst );
235         else
236             ok( !menu_name == !wc.lpszMenuName, "Wrong name %p/%p for class %s %p\n",
237                 wc.lpszMenuName, menu_name, name, inst );
238     }
239 }
240
241 static void check_instance( const char *name, HINSTANCE inst, HINSTANCE info_inst, HINSTANCE gcl_inst )
242 {
243     WNDCLASSA wc;
244     HWND hwnd;
245
246     ok( GetClassInfo( inst, name, &wc ), "Couldn't find class %s inst %p\n", name, inst );
247     ok( wc.hInstance == info_inst, "Wrong info instance %p/%p for class %s\n",
248         wc.hInstance, info_inst, name );
249     hwnd = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, inst, 0 );
250     ok( hwnd != NULL, "Couldn't create window for class %s inst %p\n", name, inst );
251     ok( (HINSTANCE)GetClassLongA( hwnd, GCL_HMODULE ) == gcl_inst,
252         "Wrong GCL instance %p/%p for class %s\n",
253         (HINSTANCE)GetClassLongA( hwnd, GCL_HMODULE ), gcl_inst, name );
254     ok( (HINSTANCE)GetWindowLongA( hwnd, GWL_HINSTANCE ) == inst,
255         "Wrong GWL instance %p/%p for window %s\n",
256         (HINSTANCE)GetWindowLongA( hwnd, GWL_HINSTANCE ), inst, name );
257     ok(!UnregisterClassA(name, inst), "UnregisterClassA should fail while exists a class window\n");
258     ok(GetLastError() == ERROR_CLASS_HAS_WINDOWS, "GetLastError() should be set to ERROR_CLASS_HAS_WINDOWS not %ld\n", GetLastError());
259     DestroyWindow(hwnd);
260 }
261
262 struct class_info
263 {
264     const char *name;
265     HINSTANCE inst, info_inst, gcl_inst;
266 };
267
268 static DWORD WINAPI thread_proc(void *param)
269 {
270     struct class_info *class_info = (struct class_info *)param;
271
272     check_instance(class_info->name, class_info->inst, class_info->info_inst, class_info->gcl_inst);
273
274     return 0;
275 }
276
277 static void check_thread_instance( const char *name, HINSTANCE inst, HINSTANCE info_inst, HINSTANCE gcl_inst )
278 {
279     HANDLE hThread;
280     DWORD tid;
281     struct class_info class_info;
282
283     class_info.name = name;
284     class_info.inst = inst;
285     class_info.info_inst = info_inst;
286     class_info.gcl_inst = gcl_inst;
287
288     hThread = CreateThread(NULL, 0, thread_proc, &class_info, 0, &tid);
289     ok(hThread != NULL, "CreateThread failed, error %ld\n", GetLastError());
290     ok(WaitForSingleObject(hThread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
291     CloseHandle(hThread);
292 }
293
294 /* test various instance parameters */
295 static void test_instances(void)
296 {
297     WNDCLASSA cls, wc;
298     HWND hwnd, hwnd2;
299     const char *name = "__test__";
300     HINSTANCE kernel32 = GetModuleHandleA("kernel32");
301     HINSTANCE user32 = GetModuleHandleA("user32");
302     HINSTANCE main_module = GetModuleHandleA(NULL);
303
304     memset( &cls, 0, sizeof(cls) );
305     cls.style         = CS_HREDRAW | CS_VREDRAW;
306     cls.lpfnWndProc   = ClassTest_WndProc;
307     cls.cbClsExtra    = 0;
308     cls.cbWndExtra    = 0;
309     cls.lpszClassName = name;
310
311     cls.lpszMenuName  = "main_module";
312     cls.hInstance = main_module;
313     ok( RegisterClassA( &cls ), "Failed to register local class for main module\n" );
314     check_class( main_module, name, "main_module" );
315     check_instance( name, main_module, main_module, main_module );
316     check_thread_instance( name, main_module, main_module, main_module );
317
318     cls.lpszMenuName  = "kernel32";
319     cls.hInstance = kernel32;
320     ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
321     check_class( kernel32, name, "kernel32" );
322     check_class( main_module, name, "main_module" );
323     check_instance( name, kernel32, kernel32, kernel32 );
324     check_thread_instance( name, kernel32, kernel32, kernel32 );
325     ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
326
327     /* setting global flag doesn't change status of class */
328     hwnd = CreateWindowExA( 0, name, "test", 0, 0, 0, 0, 0, 0, 0, main_module, 0 );
329     SetClassLongA( hwnd, GCL_STYLE, CS_GLOBALCLASS );
330     cls.lpszMenuName  = "kernel32";
331     cls.hInstance = kernel32;
332     ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
333     check_class( kernel32, name, "kernel32" );
334     check_class( main_module, name, "main_module" );
335     check_instance( name, kernel32, kernel32, kernel32 );
336     check_instance( name, main_module, main_module, main_module );
337     check_thread_instance( name, kernel32, kernel32, kernel32 );
338     check_thread_instance( name, main_module, main_module, main_module );
339     ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
340
341     /* changing the instance doesn't make it global */
342     SetClassLongA( hwnd, GCL_HMODULE, 0 );
343     ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
344     check_class( kernel32, name, "kernel32" );
345     check_instance( name, kernel32, kernel32, kernel32 );
346     check_thread_instance( name, kernel32, kernel32, kernel32 );
347     ok( !GetClassInfo( 0, name, &wc ), "Class found with null instance\n" );
348     ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
349
350     /* GetClassInfo with instance 0 finds user32 instance */
351     SetClassLongA( hwnd, GCL_HMODULE, (LONG)user32 );
352     ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
353     check_class( kernel32, name, "kernel32" );
354     check_class( user32, name, "main_module" );
355     check_class( 0, name, "main_module" );
356     check_instance( name, kernel32, kernel32, kernel32 );
357     check_instance( name, user32, 0, user32 );
358     check_instance( name, 0, 0, kernel32 );
359     check_thread_instance( name, kernel32, kernel32, kernel32 );
360     check_thread_instance( name, user32, 0, user32 );
361     check_thread_instance( name, 0, 0, kernel32 );
362     ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
363
364     SetClassLongA( hwnd, GCL_HMODULE, 0x12345678 );
365     ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
366     check_class( kernel32, name, "kernel32" );
367     check_class( (HINSTANCE)0x12345678, name, "main_module" );
368     check_instance( name, kernel32, kernel32, kernel32 );
369     check_instance( name, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678 );
370     check_thread_instance( name, kernel32, kernel32, kernel32 );
371     check_thread_instance( name, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678 );
372     ok( !GetClassInfo( 0, name, &wc ), "Class found with null instance\n" );
373
374     /* creating a window with instance 0 uses the first class found */
375     cls.hInstance = (HINSTANCE)0xdeadbeef;
376     cls.lpszMenuName = "deadbeef";
377     cls.style = 3;
378     ok( RegisterClassA( &cls ), "Failed to register local class for deadbeef\n" );
379     hwnd2 = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, NULL, 0 );
380     ok( (HINSTANCE)GetClassLong( hwnd2, GCL_HMODULE ) == (HINSTANCE)0xdeadbeef,
381         "Didn't get deadbeef class for null instance\n" );
382     DestroyWindow( hwnd2 );
383     ok( UnregisterClassA( name, (HINSTANCE)0xdeadbeef ), "Unregister failed for deadbeef\n" );
384
385     hwnd2 = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, NULL, 0 );
386     ok( (HINSTANCE)GetClassLong( hwnd2, GCL_HMODULE ) == kernel32,
387         "Didn't get kernel32 class for null instance\n" );
388     DestroyWindow( hwnd2 );
389
390     ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
391
392     hwnd2 = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, NULL, 0 );
393     ok( GetClassLong( hwnd2, GCL_HMODULE ) == 0x12345678,
394         "Didn't get 12345678 class for null instance\n" );
395     DestroyWindow( hwnd2 );
396
397     SetClassLongA( hwnd, GCL_HMODULE, (LONG)main_module );
398     DestroyWindow( hwnd );
399
400     /* null handle means the same thing as main module */
401     cls.lpszMenuName  = "null";
402     cls.hInstance = 0;
403     ok( !RegisterClassA( &cls ), "Succeeded registering local class for null instance\n" );
404     ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %ld\n", GetLastError() );
405     ok( UnregisterClassA( name, main_module ), "Unregister failed for main module\n" );
406
407     ok( RegisterClassA( &cls ), "Failed to register local class for null instance\n" );
408     /* must be found with main module handle */
409     check_class( main_module, name, "null" );
410     check_instance( name, main_module, main_module, main_module );
411     check_thread_instance( name, main_module, main_module, main_module );
412     ok( !GetClassInfo( 0, name, &wc ), "Class found with null instance\n" );
413     ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %ld\n", GetLastError() );
414     ok( UnregisterClassA( name, 0 ), "Unregister failed for null instance\n" );
415
416     /* registering for user32 always fails */
417     cls.lpszMenuName = "user32";
418     cls.hInstance = user32;
419     ok( !RegisterClassA( &cls ), "Succeeded registering local class for user32\n" );
420     ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error code %ld\n", GetLastError() );
421     cls.style |= CS_GLOBALCLASS;
422     ok( !RegisterClassA( &cls ), "Succeeded registering global class for user32\n" );
423     ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error code %ld\n", GetLastError() );
424
425     /* unregister is OK though */
426     cls.hInstance = main_module;
427     ok( RegisterClassA( &cls ), "Failed to register global class for main module\n" );
428     ok( UnregisterClassA( name, user32 ), "Unregister failed for user32\n" );
429
430     /* instance doesn't matter for global class */
431     cls.style |= CS_GLOBALCLASS;
432     cls.lpszMenuName  = "main_module";
433     cls.hInstance = main_module;
434     ok( RegisterClassA( &cls ), "Failed to register global class for main module\n" );
435     cls.lpszMenuName  = "kernel32";
436     cls.hInstance = kernel32;
437     ok( !RegisterClassA( &cls ), "Succeeded registering local class for kernel32\n" );
438     ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %ld\n", GetLastError() );
439     /* even if global flag is cleared */
440     hwnd = CreateWindowExA( 0, name, "test", 0, 0, 0, 0, 0, 0, 0, main_module, 0 );
441     SetClassLongA( hwnd, GCL_STYLE, 0 );
442     ok( !RegisterClassA( &cls ), "Succeeded registering local class for kernel32\n" );
443     ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %ld\n", GetLastError() );
444
445     check_class( main_module, name, "main_module" );
446     check_class( kernel32, name, "main_module" );
447     check_class( 0, name, "main_module" );
448     check_class( (HINSTANCE)0x12345678, name, "main_module" );
449     check_instance( name, main_module, main_module, main_module );
450     check_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, main_module );
451     check_thread_instance( name, main_module, main_module, main_module );
452     check_thread_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, main_module );
453
454     /* changing the instance for global class doesn't make much difference */
455     SetClassLongA( hwnd, GCL_HMODULE, 0xdeadbeef );
456     check_instance( name, main_module, main_module, (HINSTANCE)0xdeadbeef );
457     check_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef );
458     check_thread_instance( name, main_module, main_module, (HINSTANCE)0xdeadbeef );
459     check_thread_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef );
460
461     DestroyWindow( hwnd );
462     ok( UnregisterClassA( name, (HINSTANCE)0x87654321 ), "Unregister failed for main module global\n" );
463     ok( !UnregisterClassA( name, (HINSTANCE)0x87654321 ), "Unregister succeeded the second time\n" );
464     ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %ld\n", GetLastError() );
465
466     cls.hInstance = (HINSTANCE)0x12345678;
467     ok( RegisterClassA( &cls ), "Failed to register global class for dummy instance\n" );
468     check_instance( name, main_module, main_module, (HINSTANCE)0x12345678 );
469     check_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0x12345678 );
470     check_thread_instance( name, main_module, main_module, (HINSTANCE)0x12345678 );
471     check_thread_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0x12345678 );
472     ok( UnregisterClassA( name, (HINSTANCE)0x87654321 ), "Unregister failed for main module global\n" );
473
474     /* check system classes */
475
476     /* we cannot register a global class with the name of a system class */
477     cls.style |= CS_GLOBALCLASS;
478     cls.lpszMenuName  = "button_main_module";
479     cls.lpszClassName = "BUTTON";
480     cls.hInstance = main_module;
481     ok( !RegisterClassA( &cls ), "Succeeded registering global button class for main module\n" );
482     ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %ld\n", GetLastError() );
483     cls.hInstance = kernel32;
484     ok( !RegisterClassA( &cls ), "Succeeded registering global button class for kernel32\n" );
485     ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %ld\n", GetLastError() );
486
487     /* local class is OK however */
488     cls.style &= ~CS_GLOBALCLASS;
489     cls.lpszMenuName  = "button_main_module";
490     cls.hInstance = main_module;
491     ok( RegisterClassA( &cls ), "Failed to register local button class for main module\n" );
492     check_class( main_module, "BUTTON", "button_main_module" );
493     cls.lpszMenuName  = "button_kernel32";
494     cls.hInstance = kernel32;
495     ok( RegisterClassA( &cls ), "Failed to register local button class for kernel32\n" );
496     check_class( kernel32, "BUTTON", "button_kernel32" );
497     check_class( main_module, "BUTTON", "button_main_module" );
498     ok( UnregisterClassA( "BUTTON", kernel32 ), "Unregister failed for kernel32 button\n" );
499     ok( UnregisterClassA( "BUTTON", main_module ), "Unregister failed for main module button\n" );
500     /* GetClassInfo sets instance to passed value for global classes */
501     check_instance( "BUTTON", 0, 0, user32 );
502     check_instance( "BUTTON", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
503     check_instance( "BUTTON", user32, 0, user32 );
504     check_thread_instance( "BUTTON", 0, 0, user32 );
505     check_thread_instance( "BUTTON", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
506     check_thread_instance( "BUTTON", user32, 0, user32 );
507
508     /* we can unregister system classes */
509     ok( GetClassInfo( 0, "BUTTON", &wc ), "Button class not found with null instance\n" );
510     ok( GetClassInfo( kernel32, "BUTTON", &wc ), "Button class not found with kernel32\n" );
511     ok( UnregisterClass( "BUTTON", (HINSTANCE)0x12345678 ), "Failed to unregister button\n" );
512     ok( !UnregisterClass( "BUTTON", (HINSTANCE)0x87654321 ), "Unregistered button a second time\n" );
513     ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %ld\n", GetLastError() );
514     ok( !GetClassInfo( 0, "BUTTON", &wc ), "Button still exists\n" );
515     ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %ld\n", GetLastError() );
516
517     /* we can change the instance of a system class */
518     check_instance( "EDIT", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
519     check_thread_instance( "EDIT", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
520     hwnd = CreateWindowExA( 0, "EDIT", "test", 0, 0, 0, 0, 0, 0, 0, main_module, 0 );
521     SetClassLongA( hwnd, GCL_HMODULE, 0xdeadbeef );
522     check_instance( "EDIT", (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0xdeadbeef );
523     check_thread_instance( "EDIT", (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0xdeadbeef );
524 }
525
526 LRESULT WINAPI TestDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
527 {
528     return DefWindowProc(hWnd, uMsg, wParam, lParam);
529 }
530
531 BOOL RegisterTestDialog(HINSTANCE hInstance)
532 {
533     WNDCLASSEX wcx;
534     ATOM atom = 0;
535
536     ZeroMemory(&wcx, sizeof(WNDCLASSEX));
537     wcx.cbSize = sizeof(wcx);
538     wcx.lpfnWndProc = TestDlgProc;
539     wcx.cbClsExtra = 0;
540     wcx.cbWndExtra = DLGWINDOWEXTRA;
541     wcx.hInstance = hInstance;
542     wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
543     wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
544     wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
545     wcx.lpszClassName = "TestDialog";
546     wcx.lpszMenuName =  "TestDialog";
547     wcx.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(5),
548         IMAGE_ICON,
549         GetSystemMetrics(SM_CXSMICON),
550         GetSystemMetrics(SM_CYSMICON),
551         LR_DEFAULTCOLOR);
552
553     atom = RegisterClassEx(&wcx);
554     ok(atom != 0, "RegisterClassEx returned 0\n");
555
556     return atom;
557 }
558
559 /* test registering a dialog box created by using the CLASS directive in a
560    resource file, then test creating the dialog using CreateDialogParam. */
561 void WINAPI CreateDialogParamTest(HINSTANCE hInstance)
562 {
563     HWND hWndMain;
564
565     if (RegisterTestDialog(hInstance))
566     {
567         hWndMain = CreateDialogParam(hInstance, "CLASS_TEST_DIALOG", NULL, 0, 0);
568         ok(hWndMain != NULL, "CreateDialogParam returned NULL\n");
569         ShowWindow(hWndMain, SW_SHOW);
570         DestroyWindow(hWndMain);
571     }
572 }
573
574 START_TEST(class)
575 {
576     HANDLE hInstance = GetModuleHandleA( NULL );
577
578     if (!GetModuleHandleW(0))
579     {
580         trace("Class test is incompatible with Win9x implementation, skipping\n");
581         return;
582     }
583
584     ClassTest(hInstance,FALSE);
585     ClassTest(hInstance,TRUE);
586     CreateDialogParamTest(hInstance);
587     test_styles();
588     test_instances();
589 }