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