user32: Properly handle integer atoms specified as strings in class names.
[wine/multimedia.git] / dlls / user32 / class.c
blobe3a29515576107c3f3595d3c35394f9b3f11a388
1 /*
2 * Window classes functions
4 * Copyright 1993, 1996, 2003 Alexandre Julliard
5 * Copyright 1998 Juergen Schmied (jsch)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "winerror.h"
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "wine/unicode.h"
35 #include "win.h"
36 #include "user_private.h"
37 #include "controls.h"
38 #include "wine/server.h"
39 #include "wine/list.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(class);
44 typedef struct tagCLASS
46 struct list entry; /* Entry in class list */
47 UINT style; /* Class style */
48 BOOL local; /* Local class? */
49 WNDPROC winproc; /* Window procedure */
50 INT cbClsExtra; /* Class extra bytes */
51 INT cbWndExtra; /* Window extra bytes */
52 LPWSTR menuName; /* Default menu name (Unicode followed by ASCII) */
53 HINSTANCE hInstance; /* Module that created the task */
54 HICON hIcon; /* Default icon */
55 HICON hIconSm; /* Default small icon */
56 HCURSOR hCursor; /* Default cursor */
57 HBRUSH hbrBackground; /* Default background */
58 ATOM atomName; /* Name of the class */
59 } CLASS;
61 static struct list class_list = LIST_INIT( class_list );
63 #define CLASS_OTHER_PROCESS ((CLASS *)1)
64 #define MAX_ATOM_LEN 255 /* from dlls/kernel32/atom.c */
66 /***********************************************************************
67 * get_class_ptr
69 static CLASS *get_class_ptr( HWND hwnd, BOOL write_access )
71 WND *ptr = WIN_GetPtr( hwnd );
73 if (ptr)
75 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP) return ptr->class;
76 if (!write_access) return CLASS_OTHER_PROCESS;
78 /* modifying classes in other processes is not allowed */
79 if (ptr == WND_DESKTOP || IsWindow( hwnd ))
81 SetLastError( ERROR_ACCESS_DENIED );
82 return NULL;
85 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
86 return NULL;
90 /***********************************************************************
91 * release_class_ptr
93 static inline void release_class_ptr( CLASS *ptr )
95 USER_Unlock();
99 /***********************************************************************
100 * get_int_atom_value
102 static ATOM get_int_atom_value( LPCWSTR name )
104 UINT ret = 0;
106 if (IS_INTRESOURCE(name)) return LOWORD(name);
107 if (*name++ != '#') return 0;
108 while (*name)
110 if (*name < '0' || *name > '9') return 0;
111 ret = ret * 10 + *name++ - '0';
112 if (ret > 0xffff) return 0;
114 return ret;
118 /***********************************************************************
119 * set_server_info
121 * Set class info with the wine server.
123 static BOOL set_server_info( HWND hwnd, INT offset, LONG_PTR newval, UINT size )
125 BOOL ret;
127 SERVER_START_REQ( set_class_info )
129 req->window = hwnd;
130 req->extra_offset = -1;
131 switch(offset)
133 case GCW_ATOM:
134 req->flags = SET_CLASS_ATOM;
135 req->atom = newval;
136 case GCL_STYLE:
137 req->flags = SET_CLASS_STYLE;
138 req->style = newval;
139 break;
140 case GCL_CBWNDEXTRA:
141 req->flags = SET_CLASS_WINEXTRA;
142 req->win_extra = newval;
143 break;
144 case GCLP_HMODULE:
145 req->flags = SET_CLASS_INSTANCE;
146 req->instance = (void *)newval;
147 break;
148 default:
149 assert( offset >= 0 );
150 req->flags = SET_CLASS_EXTRA;
151 req->extra_offset = offset;
152 req->extra_size = size;
153 if ( size == sizeof(LONG) )
155 LONG newlong = newval;
156 memcpy( &req->extra_value, &newlong, sizeof(LONG) );
158 else
159 memcpy( &req->extra_value, &newval, sizeof(LONG_PTR) );
160 break;
162 ret = !wine_server_call_err( req );
164 SERVER_END_REQ;
165 return ret;
169 /***********************************************************************
170 * CLASS_GetMenuNameA
172 * Get the menu name as a ASCII string.
174 static inline LPSTR CLASS_GetMenuNameA( CLASS *classPtr )
176 if (!HIWORD(classPtr->menuName)) return (LPSTR)classPtr->menuName;
177 return (LPSTR)(classPtr->menuName + strlenW(classPtr->menuName) + 1);
181 /***********************************************************************
182 * CLASS_GetMenuNameW
184 * Get the menu name as a Unicode string.
186 static inline LPWSTR CLASS_GetMenuNameW( CLASS *classPtr )
188 return classPtr->menuName;
192 /***********************************************************************
193 * CLASS_SetMenuNameA
195 * Set the menu name in a class structure by copying the string.
197 static void CLASS_SetMenuNameA( CLASS *classPtr, LPCSTR name )
199 if (HIWORD(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
200 if (HIWORD(name))
202 DWORD lenA = strlen(name) + 1;
203 DWORD lenW = MultiByteToWideChar( CP_ACP, 0, name, lenA, NULL, 0 );
204 classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
205 MultiByteToWideChar( CP_ACP, 0, name, lenA, classPtr->menuName, lenW );
206 memcpy( classPtr->menuName + lenW, name, lenA );
208 else classPtr->menuName = (LPWSTR)name;
212 /***********************************************************************
213 * CLASS_SetMenuNameW
215 * Set the menu name in a class structure by copying the string.
217 static void CLASS_SetMenuNameW( CLASS *classPtr, LPCWSTR name )
219 if (HIWORD(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
220 if (HIWORD(name))
222 DWORD lenW = strlenW(name) + 1;
223 DWORD lenA = WideCharToMultiByte( CP_ACP, 0, name, lenW, NULL, 0, NULL, NULL );
224 classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
225 memcpy( classPtr->menuName, name, lenW*sizeof(WCHAR) );
226 WideCharToMultiByte( CP_ACP, 0, name, lenW,
227 (char *)(classPtr->menuName + lenW), lenA, NULL, NULL );
229 else classPtr->menuName = (LPWSTR)name;
233 /***********************************************************************
234 * CLASS_FreeClass
236 * Free a class structure.
238 static void CLASS_FreeClass( CLASS *classPtr )
240 TRACE("%p\n", classPtr);
242 USER_Lock();
244 list_remove( &classPtr->entry );
245 if (classPtr->hbrBackground > (HBRUSH)(COLOR_GRADIENTINACTIVECAPTION + 1))
246 DeleteObject( classPtr->hbrBackground );
247 HeapFree( GetProcessHeap(), 0, classPtr->menuName );
248 HeapFree( GetProcessHeap(), 0, classPtr );
249 USER_Unlock();
253 /***********************************************************************
254 * CLASS_FreeModuleClasses
256 void CLASS_FreeModuleClasses( HMODULE16 hModule )
258 struct list *ptr, *next;
260 TRACE("0x%08x\n", hModule);
262 USER_Lock();
263 for (ptr = list_head( &class_list ); ptr; ptr = next)
265 CLASS *class = LIST_ENTRY( ptr, CLASS, entry );
266 next = list_next( &class_list, ptr );
267 if (class->hInstance == HINSTANCE_32(hModule))
269 BOOL ret;
271 SERVER_START_REQ( destroy_class )
273 req->atom = class->atomName;
274 req->instance = class->hInstance;
275 ret = !wine_server_call_err( req );
277 SERVER_END_REQ;
278 if (ret) CLASS_FreeClass( class );
281 USER_Unlock();
285 /***********************************************************************
286 * CLASS_FindClassByAtom
288 * Return a pointer to the class.
289 * hinstance has been normalized by the caller.
291 static CLASS *CLASS_FindClassByAtom( ATOM atom, HINSTANCE hinstance )
293 struct list *ptr;
295 USER_Lock();
297 LIST_FOR_EACH( ptr, &class_list )
299 CLASS *class = LIST_ENTRY( ptr, CLASS, entry );
300 if (class->atomName != atom) continue;
301 if (!hinstance || !class->local || class->hInstance == hinstance)
303 TRACE("0x%04x %p -> %p\n", atom, hinstance, class);
304 return class;
307 USER_Unlock();
308 TRACE("0x%04x %p -> not found\n", atom, hinstance);
309 return NULL;
313 /***********************************************************************
314 * CLASS_RegisterClass
316 * The real RegisterClass() functionality.
318 static CLASS *CLASS_RegisterClass( LPCWSTR name, HINSTANCE hInstance, BOOL local,
319 DWORD style, INT classExtra, INT winExtra )
321 CLASS *classPtr;
322 BOOL ret;
324 TRACE("name=%s hinst=%p style=0x%x clExtr=0x%x winExtr=0x%x\n",
325 debugstr_w(name), hInstance, style, classExtra, winExtra );
327 /* Fix the extra bytes value */
329 if (classExtra < 0 || winExtra < 0)
331 SetLastError( ERROR_INVALID_PARAMETER );
332 return NULL;
334 if (classExtra > 40) /* Extra bytes are limited to 40 in Win32 */
335 WARN("Class extra bytes %d is > 40\n", classExtra);
336 if (winExtra > 40) /* Extra bytes are limited to 40 in Win32 */
337 WARN("Win extra bytes %d is > 40\n", winExtra );
339 classPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLASS) + classExtra );
340 if (!classPtr) return NULL;
342 SERVER_START_REQ( create_class )
344 req->local = local;
345 req->style = style;
346 req->instance = hInstance;
347 req->extra = classExtra;
348 req->win_extra = winExtra;
349 req->client_ptr = classPtr;
350 if (!(req->atom = get_int_atom_value(name)) && name)
351 wine_server_add_data( req, name, strlenW(name) * sizeof(WCHAR) );
352 ret = !wine_server_call_err( req );
353 classPtr->atomName = reply->atom;
355 SERVER_END_REQ;
356 if (!ret)
358 HeapFree( GetProcessHeap(), 0, classPtr );
359 return NULL;
362 classPtr->style = style;
363 classPtr->local = local;
364 classPtr->cbWndExtra = winExtra;
365 classPtr->cbClsExtra = classExtra;
366 classPtr->hInstance = hInstance;
368 /* Other non-null values must be set by caller */
370 USER_Lock();
371 if (local) list_add_head( &class_list, &classPtr->entry );
372 else list_add_tail( &class_list, &classPtr->entry );
373 return classPtr;
377 /***********************************************************************
378 * register_builtin
380 * Register a builtin control class.
381 * This allows having both ASCII and Unicode winprocs for the same class.
383 static WNDPROC register_builtin( const struct builtin_class_descr *descr )
385 CLASS *classPtr;
387 if (!(classPtr = CLASS_RegisterClass( descr->name, user32_module, FALSE,
388 descr->style, 0, descr->extra ))) return 0;
390 classPtr->hCursor = LoadCursorA( 0, (LPSTR)descr->cursor );
391 classPtr->hbrBackground = descr->brush;
392 classPtr->winproc = WINPROC_AllocProc( descr->procA, descr->procW );
393 release_class_ptr( classPtr );
394 return classPtr->winproc;
398 /***********************************************************************
399 * CLASS_RegisterBuiltinClasses
401 void CLASS_RegisterBuiltinClasses(void)
403 extern const struct builtin_class_descr BUTTON_builtin_class;
404 extern const struct builtin_class_descr COMBO_builtin_class;
405 extern const struct builtin_class_descr COMBOLBOX_builtin_class;
406 extern const struct builtin_class_descr DIALOG_builtin_class;
407 extern const struct builtin_class_descr DESKTOP_builtin_class;
408 extern const struct builtin_class_descr EDIT_builtin_class;
409 extern const struct builtin_class_descr ICONTITLE_builtin_class;
410 extern const struct builtin_class_descr LISTBOX_builtin_class;
411 extern const struct builtin_class_descr MDICLIENT_builtin_class;
412 extern const struct builtin_class_descr MENU_builtin_class;
413 extern const struct builtin_class_descr SCROLL_builtin_class;
414 extern const struct builtin_class_descr STATIC_builtin_class;
416 register_builtin( &DESKTOP_builtin_class );
417 register_builtin( &BUTTON_builtin_class );
418 register_builtin( &COMBO_builtin_class );
419 register_builtin( &COMBOLBOX_builtin_class );
420 register_builtin( &DIALOG_builtin_class );
421 EDIT_winproc_handle = register_builtin( &EDIT_builtin_class );
422 register_builtin( &ICONTITLE_builtin_class );
423 register_builtin( &LISTBOX_builtin_class );
424 register_builtin( &MDICLIENT_builtin_class );
425 register_builtin( &MENU_builtin_class );
426 register_builtin( &SCROLL_builtin_class );
427 register_builtin( &STATIC_builtin_class );
429 /* the DefWindowProc winprocs are magic too */
430 WINPROC_AllocProc( DefWindowProcA, DefWindowProcW );
434 /***********************************************************************
435 * CLASS_AddWindow
437 * Add a new window using this class, and set the necessary
438 * information inside the window structure.
440 void CLASS_AddWindow( CLASS *class, WND *win, BOOL unicode )
442 win->class = class;
443 win->clsStyle = class->style;
444 win->winproc = class->winproc;
445 if (WINPROC_IsUnicode( win->winproc, unicode )) win->flags |= WIN_ISUNICODE;
449 /***********************************************************************
450 * RegisterClassA (USER32.@)
452 * Register a window class.
454 * RETURNS
455 * >0: Unique identifier
456 * 0: Failure
458 ATOM WINAPI RegisterClassA( const WNDCLASSA* wc ) /* [in] Address of structure with class data */
460 WNDCLASSEXA wcex;
462 wcex.cbSize = sizeof(wcex);
463 wcex.style = wc->style;
464 wcex.lpfnWndProc = wc->lpfnWndProc;
465 wcex.cbClsExtra = wc->cbClsExtra;
466 wcex.cbWndExtra = wc->cbWndExtra;
467 wcex.hInstance = wc->hInstance;
468 wcex.hIcon = wc->hIcon;
469 wcex.hCursor = wc->hCursor;
470 wcex.hbrBackground = wc->hbrBackground;
471 wcex.lpszMenuName = wc->lpszMenuName;
472 wcex.lpszClassName = wc->lpszClassName;
473 wcex.hIconSm = 0;
474 return RegisterClassExA( &wcex );
478 /***********************************************************************
479 * RegisterClassW (USER32.@)
481 * See RegisterClassA.
483 ATOM WINAPI RegisterClassW( const WNDCLASSW* wc )
485 WNDCLASSEXW wcex;
487 wcex.cbSize = sizeof(wcex);
488 wcex.style = wc->style;
489 wcex.lpfnWndProc = wc->lpfnWndProc;
490 wcex.cbClsExtra = wc->cbClsExtra;
491 wcex.cbWndExtra = wc->cbWndExtra;
492 wcex.hInstance = wc->hInstance;
493 wcex.hIcon = wc->hIcon;
494 wcex.hCursor = wc->hCursor;
495 wcex.hbrBackground = wc->hbrBackground;
496 wcex.lpszMenuName = wc->lpszMenuName;
497 wcex.lpszClassName = wc->lpszClassName;
498 wcex.hIconSm = 0;
499 return RegisterClassExW( &wcex );
503 /***********************************************************************
504 * RegisterClassExA (USER32.@)
506 ATOM WINAPI RegisterClassExA( const WNDCLASSEXA* wc )
508 ATOM atom;
509 CLASS *classPtr;
510 HINSTANCE instance;
512 if (wc->hInstance == user32_module)
514 /* we can't register a class for user32 */
515 SetLastError( ERROR_INVALID_PARAMETER );
516 return 0;
518 if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
520 if (!IS_INTRESOURCE(wc->lpszClassName))
522 WCHAR name[MAX_ATOM_LEN + 1];
524 if (!MultiByteToWideChar( CP_ACP, 0, wc->lpszClassName, -1, name, MAX_ATOM_LEN + 1 )) return 0;
525 classPtr = CLASS_RegisterClass( name, instance, !(wc->style & CS_GLOBALCLASS),
526 wc->style, wc->cbClsExtra, wc->cbWndExtra );
528 else
530 classPtr = CLASS_RegisterClass( (LPCWSTR)wc->lpszClassName, instance,
531 !(wc->style & CS_GLOBALCLASS), wc->style,
532 wc->cbClsExtra, wc->cbWndExtra );
534 if (!classPtr) return 0;
535 atom = classPtr->atomName;
537 TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
538 debugstr_a(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
539 wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
541 classPtr->hIcon = wc->hIcon;
542 classPtr->hIconSm = wc->hIconSm;
543 classPtr->hCursor = wc->hCursor;
544 classPtr->hbrBackground = wc->hbrBackground;
545 classPtr->winproc = WINPROC_AllocProc( wc->lpfnWndProc, NULL );
546 CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
547 release_class_ptr( classPtr );
548 return atom;
552 /***********************************************************************
553 * RegisterClassExW (USER32.@)
555 ATOM WINAPI RegisterClassExW( const WNDCLASSEXW* wc )
557 ATOM atom;
558 CLASS *classPtr;
559 HINSTANCE instance;
561 if (wc->hInstance == user32_module)
563 /* we can't register a class for user32 */
564 SetLastError( ERROR_INVALID_PARAMETER );
565 return 0;
567 if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
569 if (!(classPtr = CLASS_RegisterClass( wc->lpszClassName, instance, !(wc->style & CS_GLOBALCLASS),
570 wc->style, wc->cbClsExtra, wc->cbWndExtra )))
571 return 0;
573 atom = classPtr->atomName;
575 TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
576 debugstr_w(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
577 wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
579 classPtr->hIcon = wc->hIcon;
580 classPtr->hIconSm = wc->hIconSm;
581 classPtr->hCursor = wc->hCursor;
582 classPtr->hbrBackground = wc->hbrBackground;
583 classPtr->winproc = WINPROC_AllocProc( NULL, wc->lpfnWndProc );
584 CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
585 release_class_ptr( classPtr );
586 return atom;
590 /***********************************************************************
591 * UnregisterClassA (USER32.@)
593 BOOL WINAPI UnregisterClassA( LPCSTR className, HINSTANCE hInstance )
595 if (!IS_INTRESOURCE(className))
597 WCHAR name[MAX_ATOM_LEN + 1];
599 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, name, MAX_ATOM_LEN + 1 ))
600 return FALSE;
601 return UnregisterClassW( name, hInstance );
603 return UnregisterClassW( (LPCWSTR)className, hInstance );
606 /***********************************************************************
607 * UnregisterClassW (USER32.@)
609 BOOL WINAPI UnregisterClassW( LPCWSTR className, HINSTANCE hInstance )
611 CLASS *classPtr = NULL;
613 TRACE("%s %p\n",debugstr_w(className), hInstance);
615 SERVER_START_REQ( destroy_class )
617 req->instance = hInstance;
618 if (!(req->atom = get_int_atom_value(className)) && className)
619 wine_server_add_data( req, className, strlenW(className) * sizeof(WCHAR) );
620 if (!wine_server_call_err( req )) classPtr = reply->client_ptr;
622 SERVER_END_REQ;
624 if (classPtr) CLASS_FreeClass( classPtr );
625 return (classPtr != NULL);
629 /***********************************************************************
630 * GetClassWord (USER32.@)
632 WORD WINAPI GetClassWord( HWND hwnd, INT offset )
634 CLASS *class;
635 WORD retvalue = 0;
637 if (offset < 0) return GetClassLongA( hwnd, offset );
639 TRACE("%p %x\n",hwnd, offset);
641 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
643 if (class == CLASS_OTHER_PROCESS)
645 SERVER_START_REQ( set_class_info )
647 req->window = hwnd;
648 req->flags = 0;
649 req->extra_offset = offset;
650 req->extra_size = sizeof(retvalue);
651 if (!wine_server_call_err( req ))
652 memcpy( &retvalue, &reply->old_extra_value, sizeof(retvalue) );
654 SERVER_END_REQ;
655 return retvalue;
658 if (offset <= class->cbClsExtra - sizeof(WORD))
659 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(retvalue) );
660 else
661 SetLastError( ERROR_INVALID_INDEX );
662 release_class_ptr( class );
663 return retvalue;
667 /***********************************************************************
668 * CLASS_GetClassLong
670 * Implementation of GetClassLong(Ptr)A/W
672 static ULONG_PTR CLASS_GetClassLong( HWND hwnd, INT offset, UINT size,
673 BOOL unicode )
675 CLASS *class;
676 ULONG_PTR retvalue = 0;
678 TRACE("%p %d\n", hwnd, offset);
680 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
682 if (class == CLASS_OTHER_PROCESS)
684 SERVER_START_REQ( set_class_info )
686 req->window = hwnd;
687 req->flags = 0;
688 req->extra_offset = (offset >= 0) ? offset : -1;
689 req->extra_size = (offset >= 0) ? size : 0;
690 if (!wine_server_call_err( req ))
692 switch(offset)
694 case GCLP_HBRBACKGROUND:
695 case GCLP_HCURSOR:
696 case GCLP_HICON:
697 case GCLP_HICONSM:
698 case GCLP_WNDPROC:
699 case GCLP_MENUNAME:
700 FIXME( "offset %d (%s) not supported on other process window %p\n",
701 offset, SPY_GetClassLongOffsetName(offset), hwnd );
702 SetLastError( ERROR_INVALID_HANDLE );
703 break;
704 case GCL_STYLE:
705 retvalue = reply->old_style;
706 break;
707 case GCL_CBWNDEXTRA:
708 retvalue = reply->old_win_extra;
709 break;
710 case GCL_CBCLSEXTRA:
711 retvalue = reply->old_extra;
712 break;
713 case GCLP_HMODULE:
714 retvalue = (ULONG_PTR)reply->old_instance;
715 break;
716 case GCW_ATOM:
717 retvalue = reply->old_atom;
718 break;
719 default:
720 if (offset >= 0)
722 if (size == sizeof(DWORD))
724 DWORD retdword;
725 memcpy( &retdword, &reply->old_extra_value, sizeof(DWORD) );
726 retvalue = retdword;
728 else
729 memcpy( &retvalue, &reply->old_extra_value,
730 sizeof(ULONG_PTR) );
732 else SetLastError( ERROR_INVALID_INDEX );
733 break;
737 SERVER_END_REQ;
738 return retvalue;
741 if (offset >= 0)
743 if (offset <= class->cbClsExtra - size)
745 if (size == sizeof(DWORD))
747 DWORD retdword;
748 memcpy( &retdword, (char *)(class + 1) + offset, sizeof(DWORD) );
749 retvalue = retdword;
751 else
752 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(ULONG_PTR) );
754 else
755 SetLastError( ERROR_INVALID_INDEX );
756 release_class_ptr( class );
757 return retvalue;
760 switch(offset)
762 case GCLP_HBRBACKGROUND:
763 retvalue = (ULONG_PTR)class->hbrBackground;
764 break;
765 case GCLP_HCURSOR:
766 retvalue = (ULONG_PTR)class->hCursor;
767 break;
768 case GCLP_HICON:
769 retvalue = (ULONG_PTR)class->hIcon;
770 break;
771 case GCLP_HICONSM:
772 retvalue = (ULONG_PTR)class->hIconSm;
773 break;
774 case GCL_STYLE:
775 retvalue = class->style;
776 break;
777 case GCL_CBWNDEXTRA:
778 retvalue = class->cbWndExtra;
779 break;
780 case GCL_CBCLSEXTRA:
781 retvalue = class->cbClsExtra;
782 break;
783 case GCLP_HMODULE:
784 retvalue = (ULONG_PTR)class->hInstance;
785 break;
786 case GCLP_WNDPROC:
787 retvalue = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
788 break;
789 case GCLP_MENUNAME:
790 retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
791 if (unicode)
792 retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
793 else
794 retvalue = (ULONG_PTR)CLASS_GetMenuNameA( class );
795 break;
796 case GCW_ATOM:
797 retvalue = class->atomName;
798 break;
799 default:
800 SetLastError( ERROR_INVALID_INDEX );
801 break;
803 release_class_ptr( class );
804 return retvalue;
808 /***********************************************************************
809 * GetClassLongW (USER32.@)
811 DWORD WINAPI GetClassLongW( HWND hwnd, INT offset )
813 return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), TRUE );
818 /***********************************************************************
819 * GetClassLongA (USER32.@)
821 DWORD WINAPI GetClassLongA( HWND hwnd, INT offset )
823 return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), FALSE );
827 /***********************************************************************
828 * SetClassWord (USER32.@)
830 WORD WINAPI SetClassWord( HWND hwnd, INT offset, WORD newval )
832 CLASS *class;
833 WORD retval = 0;
835 if (offset < 0) return SetClassLongA( hwnd, offset, (DWORD)newval );
837 TRACE("%p %d %x\n", hwnd, offset, newval);
839 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
841 SERVER_START_REQ( set_class_info )
843 req->window = hwnd;
844 req->flags = SET_CLASS_EXTRA;
845 req->extra_offset = offset;
846 req->extra_size = sizeof(newval);
847 memcpy( &req->extra_value, &newval, sizeof(newval) );
848 if (!wine_server_call_err( req ))
850 void *ptr = (char *)(class + 1) + offset;
851 memcpy( &retval, ptr, sizeof(retval) );
852 memcpy( ptr, &newval, sizeof(newval) );
855 SERVER_END_REQ;
856 release_class_ptr( class );
857 return retval;
861 /***********************************************************************
862 * CLASS_SetClassLong
864 * Implementation of SetClassLong(Ptr)A/W
866 static ULONG_PTR CLASS_SetClassLong( HWND hwnd, INT offset, LONG_PTR newval,
867 UINT size, BOOL unicode )
869 CLASS *class;
870 ULONG_PTR retval = 0;
872 TRACE("%p %d %lx\n", hwnd, offset, newval);
874 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
876 if (offset >= 0)
878 if (set_server_info( hwnd, offset, newval, size ))
880 void *ptr = (char *)(class + 1) + offset;
881 if ( size == sizeof(LONG) )
883 DWORD retdword;
884 LONG newlong = newval;
885 memcpy( &retdword, ptr, sizeof(DWORD) );
886 memcpy( ptr, &newlong, sizeof(LONG) );
887 retval = retdword;
889 else
891 memcpy( &retval, ptr, sizeof(ULONG_PTR) );
892 memcpy( ptr, &newval, sizeof(LONG_PTR) );
896 else switch(offset)
898 case GCLP_MENUNAME:
899 if ( unicode )
900 CLASS_SetMenuNameW( class, (LPCWSTR)newval );
901 else
902 CLASS_SetMenuNameA( class, (LPCSTR)newval );
903 retval = 0; /* Old value is now meaningless anyway */
904 break;
905 case GCLP_WNDPROC:
906 retval = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
907 class->winproc = WINPROC_AllocProc( unicode ? NULL : (WNDPROC)newval,
908 unicode ? (WNDPROC)newval : NULL );
909 break;
910 case GCLP_HBRBACKGROUND:
911 retval = (ULONG_PTR)class->hbrBackground;
912 class->hbrBackground = (HBRUSH)newval;
913 break;
914 case GCLP_HCURSOR:
915 retval = (ULONG_PTR)class->hCursor;
916 class->hCursor = (HCURSOR)newval;
917 break;
918 case GCLP_HICON:
919 retval = (ULONG_PTR)class->hIcon;
920 class->hIcon = (HICON)newval;
921 break;
922 case GCLP_HICONSM:
923 retval = (ULONG_PTR)class->hIconSm;
924 class->hIconSm = (HICON)newval;
925 break;
926 case GCL_STYLE:
927 if (!set_server_info( hwnd, offset, newval, size )) break;
928 retval = class->style;
929 class->style = newval;
930 break;
931 case GCL_CBWNDEXTRA:
932 if (!set_server_info( hwnd, offset, newval, size )) break;
933 retval = class->cbWndExtra;
934 class->cbWndExtra = newval;
935 break;
936 case GCLP_HMODULE:
937 if (!set_server_info( hwnd, offset, newval, size )) break;
938 retval = (ULONG_PTR)class->hInstance;
939 class->hInstance = (HINSTANCE)newval;
940 break;
941 case GCW_ATOM:
942 if (!set_server_info( hwnd, offset, newval, size )) break;
943 retval = class->atomName;
944 class->atomName = newval;
945 break;
946 case GCL_CBCLSEXTRA: /* cannot change this one */
947 SetLastError( ERROR_INVALID_PARAMETER );
948 break;
949 default:
950 SetLastError( ERROR_INVALID_INDEX );
951 break;
953 release_class_ptr( class );
954 return retval;
958 /***********************************************************************
959 * SetClassLongW (USER32.@)
961 DWORD WINAPI SetClassLongW( HWND hwnd, INT offset, LONG newval )
963 TRACE("%p %d %x\n", hwnd, offset, newval);
965 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), TRUE );
969 /***********************************************************************
970 * SetClassLongA (USER32.@)
972 DWORD WINAPI SetClassLongA( HWND hwnd, INT offset, LONG newval )
974 TRACE("%p %d %x\n", hwnd, offset, newval);
976 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), FALSE );
980 /***********************************************************************
981 * GetClassNameA (USER32.@)
983 INT WINAPI GetClassNameA( HWND hwnd, LPSTR buffer, INT count )
985 char tmpbuf[MAX_ATOM_LEN + 1];
986 INT ret;
988 TRACE("%p %p %d\n", hwnd, buffer, count);
990 if (count <= 0) return 0;
992 ret = GlobalGetAtomNameA( GetClassLongW( hwnd, GCW_ATOM ), tmpbuf, MAX_ATOM_LEN + 1 );
993 if (ret)
995 ret = min(count - 1, ret);
996 memcpy(buffer, tmpbuf, ret);
997 buffer[ret] = 0;
999 return ret;
1003 /***********************************************************************
1004 * GetClassNameW (USER32.@)
1006 INT WINAPI GetClassNameW( HWND hwnd, LPWSTR buffer, INT count )
1008 WCHAR tmpbuf[MAX_ATOM_LEN + 1];
1009 INT ret;
1011 TRACE("%p %p %d\n", hwnd, buffer, count);
1013 if (count <= 0) return 0;
1015 ret = GlobalGetAtomNameW( GetClassLongW( hwnd, GCW_ATOM ), tmpbuf, MAX_ATOM_LEN + 1 );
1016 if (ret)
1018 ret = min(count - 1, ret);
1019 memcpy(buffer, tmpbuf, ret * sizeof(WCHAR));
1020 buffer[ret] = 0;
1022 return ret;
1026 /***********************************************************************
1027 * RealGetWindowClassA (USER32.@)
1029 UINT WINAPI RealGetWindowClassA( HWND hwnd, LPSTR buffer, UINT count )
1031 return GetClassNameA( hwnd, buffer, count );
1035 /***********************************************************************
1036 * RealGetWindowClassW (USER32.@)
1038 UINT WINAPI RealGetWindowClassW( HWND hwnd, LPWSTR buffer, UINT count )
1040 return GetClassNameW( hwnd, buffer, count );
1044 /***********************************************************************
1045 * GetClassInfoA (USER32.@)
1047 BOOL WINAPI GetClassInfoA( HINSTANCE hInstance, LPCSTR name, WNDCLASSA *wc )
1049 WNDCLASSEXA wcex;
1050 UINT ret = GetClassInfoExA( hInstance, name, &wcex );
1052 if (ret)
1054 wc->style = wcex.style;
1055 wc->lpfnWndProc = wcex.lpfnWndProc;
1056 wc->cbClsExtra = wcex.cbClsExtra;
1057 wc->cbWndExtra = wcex.cbWndExtra;
1058 wc->hInstance = wcex.hInstance;
1059 wc->hIcon = wcex.hIcon;
1060 wc->hCursor = wcex.hCursor;
1061 wc->hbrBackground = wcex.hbrBackground;
1062 wc->lpszMenuName = wcex.lpszMenuName;
1063 wc->lpszClassName = wcex.lpszClassName;
1065 return ret;
1069 /***********************************************************************
1070 * GetClassInfoW (USER32.@)
1072 BOOL WINAPI GetClassInfoW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSW *wc )
1074 WNDCLASSEXW wcex;
1075 UINT ret = GetClassInfoExW( hInstance, name, &wcex );
1077 if (ret)
1079 wc->style = wcex.style;
1080 wc->lpfnWndProc = wcex.lpfnWndProc;
1081 wc->cbClsExtra = wcex.cbClsExtra;
1082 wc->cbWndExtra = wcex.cbWndExtra;
1083 wc->hInstance = wcex.hInstance;
1084 wc->hIcon = wcex.hIcon;
1085 wc->hCursor = wcex.hCursor;
1086 wc->hbrBackground = wcex.hbrBackground;
1087 wc->lpszMenuName = wcex.lpszMenuName;
1088 wc->lpszClassName = wcex.lpszClassName;
1090 return ret;
1094 /***********************************************************************
1095 * GetClassInfoExA (USER32.@)
1097 BOOL WINAPI GetClassInfoExA( HINSTANCE hInstance, LPCSTR name, WNDCLASSEXA *wc )
1099 ATOM atom = HIWORD(name) ? GlobalFindAtomA( name ) : LOWORD(name);
1100 CLASS *classPtr;
1102 TRACE("%p %s %x %p\n", hInstance, debugstr_a(name), atom, wc);
1104 if (!hInstance) hInstance = user32_module;
1106 if (!atom || !(classPtr = CLASS_FindClassByAtom( atom, hInstance )))
1108 SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1109 return FALSE;
1111 wc->style = classPtr->style;
1112 wc->lpfnWndProc = WINPROC_GetProc( classPtr->winproc, FALSE );
1113 wc->cbClsExtra = classPtr->cbClsExtra;
1114 wc->cbWndExtra = classPtr->cbWndExtra;
1115 wc->hInstance = (hInstance == user32_module) ? 0 : hInstance;
1116 wc->hIcon = (HICON)classPtr->hIcon;
1117 wc->hIconSm = (HICON)classPtr->hIconSm;
1118 wc->hCursor = (HCURSOR)classPtr->hCursor;
1119 wc->hbrBackground = (HBRUSH)classPtr->hbrBackground;
1120 wc->lpszMenuName = CLASS_GetMenuNameA( classPtr );
1121 wc->lpszClassName = name;
1122 release_class_ptr( classPtr );
1124 /* We must return the atom of the class here instead of just TRUE. */
1125 return atom;
1129 /***********************************************************************
1130 * GetClassInfoExW (USER32.@)
1132 BOOL WINAPI GetClassInfoExW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSEXW *wc )
1134 ATOM atom = HIWORD(name) ? GlobalFindAtomW( name ) : LOWORD(name);
1135 CLASS *classPtr;
1137 TRACE("%p %s %x %p\n", hInstance, debugstr_w(name), atom, wc);
1139 if (!hInstance) hInstance = user32_module;
1141 if (!atom || !(classPtr = CLASS_FindClassByAtom( atom, hInstance )))
1143 SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1144 return FALSE;
1146 wc->style = classPtr->style;
1147 wc->lpfnWndProc = WINPROC_GetProc( classPtr->winproc, TRUE );
1148 wc->cbClsExtra = classPtr->cbClsExtra;
1149 wc->cbWndExtra = classPtr->cbWndExtra;
1150 wc->hInstance = (hInstance == user32_module) ? 0 : hInstance;
1151 wc->hIcon = (HICON)classPtr->hIcon;
1152 wc->hIconSm = (HICON)classPtr->hIconSm;
1153 wc->hCursor = (HCURSOR)classPtr->hCursor;
1154 wc->hbrBackground = (HBRUSH)classPtr->hbrBackground;
1155 wc->lpszMenuName = CLASS_GetMenuNameW( classPtr );
1156 wc->lpszClassName = name;
1157 release_class_ptr( classPtr );
1159 /* We must return the atom of the class here instead of just TRUE. */
1160 return atom;
1164 #if 0 /* toolhelp is in kernel, so this cannot work */
1166 /***********************************************************************
1167 * ClassFirst (TOOLHELP.69)
1169 BOOL16 WINAPI ClassFirst16( CLASSENTRY *pClassEntry )
1171 TRACE("%p\n",pClassEntry);
1172 pClassEntry->wNext = 1;
1173 return ClassNext16( pClassEntry );
1177 /***********************************************************************
1178 * ClassNext (TOOLHELP.70)
1180 BOOL16 WINAPI ClassNext16( CLASSENTRY *pClassEntry )
1182 int i;
1183 CLASS *class = firstClass;
1185 TRACE("%p\n",pClassEntry);
1187 if (!pClassEntry->wNext) return FALSE;
1188 for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
1189 if (!class)
1191 pClassEntry->wNext = 0;
1192 return FALSE;
1194 pClassEntry->hInst = class->hInstance;
1195 pClassEntry->wNext++;
1196 GlobalGetAtomNameA( class->atomName, pClassEntry->szClassName,
1197 sizeof(pClassEntry->szClassName) );
1198 return TRUE;
1200 #endif
1202 /* 64bit versions */
1204 #ifdef GetClassLongPtrA
1205 #undef GetClassLongPtrA
1206 #endif
1208 #ifdef GetClassLongPtrW
1209 #undef GetClassLongPtrW
1210 #endif
1212 #ifdef SetClassLongPtrA
1213 #undef SetClassLongPtrA
1214 #endif
1216 #ifdef SetClassLongPtrW
1217 #undef SetClassLongPtrW
1218 #endif
1220 /***********************************************************************
1221 * GetClassLongPtrA (USER32.@)
1223 ULONG_PTR WINAPI GetClassLongPtrA( HWND hwnd, INT offset )
1225 return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), FALSE );
1228 /***********************************************************************
1229 * GetClassLongPtrW (USER32.@)
1231 ULONG_PTR WINAPI GetClassLongPtrW( HWND hwnd, INT offset )
1233 return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), TRUE );
1236 /***********************************************************************
1237 * SetClassLongPtrW (USER32.@)
1239 ULONG_PTR WINAPI SetClassLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
1241 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), TRUE );
1244 /***********************************************************************
1245 * SetClassLongPtrA (USER32.@)
1247 ULONG_PTR WINAPI SetClassLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
1249 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), FALSE );