ntdll/tests: Add some tests for opening objects through symlinks.
[wine.git] / dlls / user32 / class.c
blob0b3582e9a8246e4c7780566b8942e2c9f5a0094e
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 #define MAX_ATOM_LEN 255 /* from dlls/kernel32/atom.c */
46 typedef struct tagCLASS
48 struct list entry; /* Entry in class list */
49 UINT style; /* Class style */
50 BOOL local; /* Local class? */
51 WNDPROC winproc; /* Window procedure */
52 INT cbClsExtra; /* Class extra bytes */
53 INT cbWndExtra; /* Window extra bytes */
54 LPWSTR menuName; /* Default menu name (Unicode followed by ASCII) */
55 struct dce *dce; /* Opaque pointer to class DCE */
56 HINSTANCE hInstance; /* Module that created the task */
57 HICON hIcon; /* Default icon */
58 HICON hIconSm; /* Default small icon */
59 HICON hIconSmIntern; /* Internal small icon, derived from hIcon */
60 HCURSOR hCursor; /* Default cursor */
61 HBRUSH hbrBackground; /* Default background */
62 ATOM atomName; /* Name of the class */
63 WCHAR name[MAX_ATOM_LEN + 1];
64 } CLASS;
66 static struct list class_list = LIST_INIT( class_list );
67 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
69 #define CLASS_OTHER_PROCESS ((CLASS *)1)
71 /***********************************************************************
72 * get_class_ptr
74 static CLASS *get_class_ptr( HWND hwnd, BOOL write_access )
76 WND *ptr = WIN_GetPtr( hwnd );
78 if (ptr)
80 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP) return ptr->class;
81 if (!write_access) return CLASS_OTHER_PROCESS;
83 /* modifying classes in other processes is not allowed */
84 if (ptr == WND_DESKTOP || IsWindow( hwnd ))
86 SetLastError( ERROR_ACCESS_DENIED );
87 return NULL;
90 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
91 return NULL;
95 /***********************************************************************
96 * release_class_ptr
98 static inline void release_class_ptr( CLASS *ptr )
100 USER_Unlock();
104 /***********************************************************************
105 * get_int_atom_value
107 ATOM get_int_atom_value( LPCWSTR name )
109 UINT ret = 0;
111 if (IS_INTRESOURCE(name)) return LOWORD(name);
112 if (*name++ != '#') return 0;
113 while (*name)
115 if (*name < '0' || *name > '9') return 0;
116 ret = ret * 10 + *name++ - '0';
117 if (ret > 0xffff) return 0;
119 return ret;
123 /***********************************************************************
124 * is_comctl32_class
126 static BOOL is_comctl32_class( const WCHAR *name )
128 static const WCHAR classesW[][20] =
130 {'C','o','m','b','o','B','o','x','E','x','3','2',0},
131 {'m','s','c','t','l','s','_','h','o','t','k','e','y','3','2',0},
132 {'m','s','c','t','l','s','_','p','r','o','g','r','e','s','s','3','2',0},
133 {'m','s','c','t','l','s','_','s','t','a','t','u','s','b','a','r','3','2',0},
134 {'m','s','c','t','l','s','_','t','r','a','c','k','b','a','r','3','2',0},
135 {'m','s','c','t','l','s','_','u','p','d','o','w','n','3','2',0},
136 {'N','a','t','i','v','e','F','o','n','t','C','t','l',0},
137 {'R','e','B','a','r','W','i','n','d','o','w','3','2',0},
138 {'S','y','s','A','n','i','m','a','t','e','3','2',0},
139 {'S','y','s','D','a','t','e','T','i','m','e','P','i','c','k','3','2',0},
140 {'S','y','s','H','e','a','d','e','r','3','2',0},
141 {'S','y','s','I','P','A','d','d','r','e','s','s','3','2',0},
142 {'S','y','s','L','i','s','t','V','i','e','w','3','2',0},
143 {'S','y','s','M','o','n','t','h','C','a','l','3','2',0},
144 {'S','y','s','P','a','g','e','r',0},
145 {'S','y','s','T','a','b','C','o','n','t','r','o','l','3','2',0},
146 {'S','y','s','T','r','e','e','V','i','e','w','3','2',0},
147 {'T','o','o','l','b','a','r','W','i','n','d','o','w','3','2',0},
148 {'t','o','o','l','t','i','p','s','_','c','l','a','s','s','3','2',0},
151 int min = 0, max = (sizeof(classesW) / sizeof(classesW[0])) - 1;
153 while (min <= max)
155 int res, pos = (min + max) / 2;
156 if (!(res = strcmpiW( name, classesW[pos] ))) return TRUE;
157 if (res < 0) max = pos - 1;
158 else min = pos + 1;
160 return FALSE;
164 /***********************************************************************
165 * set_server_info
167 * Set class info with the wine server.
169 static BOOL set_server_info( HWND hwnd, INT offset, LONG_PTR newval, UINT size )
171 BOOL ret;
173 SERVER_START_REQ( set_class_info )
175 req->window = wine_server_user_handle( hwnd );
176 req->extra_offset = -1;
177 switch(offset)
179 case GCW_ATOM:
180 req->flags = SET_CLASS_ATOM;
181 req->atom = LOWORD(newval);
182 break;
183 case GCL_STYLE:
184 req->flags = SET_CLASS_STYLE;
185 req->style = newval;
186 break;
187 case GCL_CBWNDEXTRA:
188 req->flags = SET_CLASS_WINEXTRA;
189 req->win_extra = newval;
190 break;
191 case GCLP_HMODULE:
192 req->flags = SET_CLASS_INSTANCE;
193 req->instance = wine_server_client_ptr( (void *)newval );
194 break;
195 default:
196 assert( offset >= 0 );
197 req->flags = SET_CLASS_EXTRA;
198 req->extra_offset = offset;
199 req->extra_size = size;
200 if ( size == sizeof(LONG) )
202 LONG newlong = newval;
203 memcpy( &req->extra_value, &newlong, sizeof(LONG) );
205 else
206 memcpy( &req->extra_value, &newval, sizeof(LONG_PTR) );
207 break;
209 ret = !wine_server_call_err( req );
211 SERVER_END_REQ;
212 return ret;
216 /***********************************************************************
217 * CLASS_GetMenuNameA
219 * Get the menu name as a ASCII string.
221 static inline LPSTR CLASS_GetMenuNameA( CLASS *classPtr )
223 if (IS_INTRESOURCE(classPtr->menuName)) return (LPSTR)classPtr->menuName;
224 return (LPSTR)(classPtr->menuName + strlenW(classPtr->menuName) + 1);
228 /***********************************************************************
229 * CLASS_GetMenuNameW
231 * Get the menu name as a Unicode string.
233 static inline LPWSTR CLASS_GetMenuNameW( CLASS *classPtr )
235 return classPtr->menuName;
239 /***********************************************************************
240 * CLASS_SetMenuNameA
242 * Set the menu name in a class structure by copying the string.
244 static void CLASS_SetMenuNameA( CLASS *classPtr, LPCSTR name )
246 if (!IS_INTRESOURCE(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
247 if (!IS_INTRESOURCE(name))
249 DWORD lenA = strlen(name) + 1;
250 DWORD lenW = MultiByteToWideChar( CP_ACP, 0, name, lenA, NULL, 0 );
251 classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
252 MultiByteToWideChar( CP_ACP, 0, name, lenA, classPtr->menuName, lenW );
253 memcpy( classPtr->menuName + lenW, name, lenA );
255 else classPtr->menuName = (LPWSTR)name;
259 /***********************************************************************
260 * CLASS_SetMenuNameW
262 * Set the menu name in a class structure by copying the string.
264 static void CLASS_SetMenuNameW( CLASS *classPtr, LPCWSTR name )
266 if (!IS_INTRESOURCE(classPtr->menuName)) HeapFree( GetProcessHeap(), 0, classPtr->menuName );
267 if (!IS_INTRESOURCE(name))
269 DWORD lenW = strlenW(name) + 1;
270 DWORD lenA = WideCharToMultiByte( CP_ACP, 0, name, lenW, NULL, 0, NULL, NULL );
271 classPtr->menuName = HeapAlloc( GetProcessHeap(), 0, lenA + lenW*sizeof(WCHAR) );
272 memcpy( classPtr->menuName, name, lenW*sizeof(WCHAR) );
273 WideCharToMultiByte( CP_ACP, 0, name, lenW,
274 (char *)(classPtr->menuName + lenW), lenA, NULL, NULL );
276 else classPtr->menuName = (LPWSTR)name;
280 /***********************************************************************
281 * CLASS_FreeClass
283 * Free a class structure.
285 static void CLASS_FreeClass( CLASS *classPtr )
287 TRACE("%p\n", classPtr);
289 USER_Lock();
291 if (classPtr->dce) free_dce( classPtr->dce, 0 );
292 list_remove( &classPtr->entry );
293 if (classPtr->hbrBackground > (HBRUSH)(COLOR_GRADIENTINACTIVECAPTION + 1))
294 DeleteObject( classPtr->hbrBackground );
295 DestroyIcon( classPtr->hIconSmIntern );
296 HeapFree( GetProcessHeap(), 0, classPtr->menuName );
297 HeapFree( GetProcessHeap(), 0, classPtr );
298 USER_Unlock();
302 /***********************************************************************
303 * CLASS_FindClass
305 * Return a pointer to the class.
307 static CLASS *CLASS_FindClass( LPCWSTR name, HINSTANCE hinstance )
309 static const WCHAR comctl32W[] = {'c','o','m','c','t','l','3','2','.','d','l','l',0};
310 struct list *ptr;
311 ATOM atom = get_int_atom_value( name );
313 GetDesktopWindow(); /* create the desktop window to trigger builtin class registration */
315 if (!name) return NULL;
317 for (;;)
319 USER_Lock();
321 LIST_FOR_EACH( ptr, &class_list )
323 CLASS *class = LIST_ENTRY( ptr, CLASS, entry );
324 if (atom)
326 if (class->atomName != atom) continue;
328 else
330 if (strcmpiW( class->name, name )) continue;
332 if (!class->local || class->hInstance == hinstance)
334 TRACE("%s %p -> %p\n", debugstr_w(name), hinstance, class);
335 return class;
338 USER_Unlock();
340 if (atom) break;
341 if (!is_comctl32_class( name )) break;
342 if (GetModuleHandleW( comctl32W )) break;
343 if (!LoadLibraryW( comctl32W )) break;
344 TRACE( "%s retrying after loading comctl32\n", debugstr_w(name) );
347 TRACE("%s %p -> not found\n", debugstr_w(name), hinstance);
348 return NULL;
352 /***********************************************************************
353 * CLASS_RegisterClass
355 * The real RegisterClass() functionality.
357 static CLASS *CLASS_RegisterClass( LPCWSTR name, HINSTANCE hInstance, BOOL local,
358 DWORD style, INT classExtra, INT winExtra )
360 CLASS *classPtr;
361 BOOL ret;
363 TRACE("name=%s hinst=%p style=0x%x clExtr=0x%x winExtr=0x%x\n",
364 debugstr_w(name), hInstance, style, classExtra, winExtra );
366 /* Fix the extra bytes value */
368 if (classExtra > 40) /* Extra bytes are limited to 40 in Win32 */
369 WARN("Class extra bytes %d is > 40\n", classExtra);
370 if (winExtra > 40) /* Extra bytes are limited to 40 in Win32 */
371 WARN("Win extra bytes %d is > 40\n", winExtra );
373 classPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLASS) + classExtra );
374 if (!classPtr) return NULL;
376 classPtr->atomName = get_int_atom_value( name );
377 if (!classPtr->atomName && name) strcpyW( classPtr->name, name );
378 else GlobalGetAtomNameW( classPtr->atomName, classPtr->name, sizeof(classPtr->name)/sizeof(WCHAR) );
380 SERVER_START_REQ( create_class )
382 req->local = local;
383 req->style = style;
384 req->instance = wine_server_client_ptr( hInstance );
385 req->extra = classExtra;
386 req->win_extra = winExtra;
387 req->client_ptr = wine_server_client_ptr( classPtr );
388 req->atom = classPtr->atomName;
389 if (!req->atom && name) wine_server_add_data( req, name, strlenW(name) * sizeof(WCHAR) );
390 ret = !wine_server_call_err( req );
391 classPtr->atomName = reply->atom;
393 SERVER_END_REQ;
394 if (!ret)
396 HeapFree( GetProcessHeap(), 0, classPtr );
397 return NULL;
400 classPtr->style = style;
401 classPtr->local = local;
402 classPtr->cbWndExtra = winExtra;
403 classPtr->cbClsExtra = classExtra;
404 classPtr->hInstance = hInstance;
406 /* Other non-null values must be set by caller */
408 USER_Lock();
409 if (local) list_add_head( &class_list, &classPtr->entry );
410 else list_add_tail( &class_list, &classPtr->entry );
411 return classPtr;
415 /***********************************************************************
416 * register_builtin
418 * Register a builtin control class.
419 * This allows having both ASCII and Unicode winprocs for the same class.
421 static void register_builtin( const struct builtin_class_descr *descr )
423 CLASS *classPtr;
425 if (!(classPtr = CLASS_RegisterClass( descr->name, user32_module, FALSE,
426 descr->style, 0, descr->extra ))) return;
428 if (descr->cursor) classPtr->hCursor = LoadCursorA( 0, (LPSTR)descr->cursor );
429 classPtr->hbrBackground = descr->brush;
430 classPtr->winproc = BUILTIN_WINPROC( descr->proc );
431 release_class_ptr( classPtr );
435 /***********************************************************************
436 * register_builtins
438 static BOOL WINAPI register_builtins( INIT_ONCE *once, void *param, void **context )
440 register_builtin( &BUTTON_builtin_class );
441 register_builtin( &COMBO_builtin_class );
442 register_builtin( &COMBOLBOX_builtin_class );
443 register_builtin( &DIALOG_builtin_class );
444 register_builtin( &EDIT_builtin_class );
445 register_builtin( &ICONTITLE_builtin_class );
446 register_builtin( &LISTBOX_builtin_class );
447 register_builtin( &MDICLIENT_builtin_class );
448 register_builtin( &MENU_builtin_class );
449 register_builtin( &SCROLL_builtin_class );
450 register_builtin( &STATIC_builtin_class );
451 register_builtin( &IME_builtin_class );
452 return TRUE;
456 /***********************************************************************
457 * register_builtin_classes
459 void register_builtin_classes(void)
461 InitOnceExecuteOnce( &init_once, register_builtins, NULL, NULL );
465 /***********************************************************************
466 * register_desktop_class
468 void register_desktop_class(void)
470 register_builtin( &DESKTOP_builtin_class );
471 register_builtin( &MESSAGE_builtin_class );
475 /***********************************************************************
476 * get_class_winproc
478 WNDPROC get_class_winproc( CLASS *class )
480 return class->winproc;
484 /***********************************************************************
485 * get_class_dce
487 struct dce *get_class_dce( CLASS *class )
489 return class->dce;
493 /***********************************************************************
494 * set_class_dce
496 struct dce *set_class_dce( CLASS *class, struct dce *dce )
498 if (class->dce) return class->dce; /* already set, don't change it */
499 class->dce = dce;
500 return dce;
504 /***********************************************************************
505 * RegisterClassA (USER32.@)
507 * Register a window class.
509 * RETURNS
510 * >0: Unique identifier
511 * 0: Failure
513 ATOM WINAPI RegisterClassA( const WNDCLASSA* wc ) /* [in] Address of structure with class data */
515 WNDCLASSEXA wcex;
517 wcex.cbSize = sizeof(wcex);
518 wcex.style = wc->style;
519 wcex.lpfnWndProc = wc->lpfnWndProc;
520 wcex.cbClsExtra = wc->cbClsExtra;
521 wcex.cbWndExtra = wc->cbWndExtra;
522 wcex.hInstance = wc->hInstance;
523 wcex.hIcon = wc->hIcon;
524 wcex.hCursor = wc->hCursor;
525 wcex.hbrBackground = wc->hbrBackground;
526 wcex.lpszMenuName = wc->lpszMenuName;
527 wcex.lpszClassName = wc->lpszClassName;
528 wcex.hIconSm = 0;
529 return RegisterClassExA( &wcex );
533 /***********************************************************************
534 * RegisterClassW (USER32.@)
536 * See RegisterClassA.
538 ATOM WINAPI RegisterClassW( const WNDCLASSW* wc )
540 WNDCLASSEXW wcex;
542 wcex.cbSize = sizeof(wcex);
543 wcex.style = wc->style;
544 wcex.lpfnWndProc = wc->lpfnWndProc;
545 wcex.cbClsExtra = wc->cbClsExtra;
546 wcex.cbWndExtra = wc->cbWndExtra;
547 wcex.hInstance = wc->hInstance;
548 wcex.hIcon = wc->hIcon;
549 wcex.hCursor = wc->hCursor;
550 wcex.hbrBackground = wc->hbrBackground;
551 wcex.lpszMenuName = wc->lpszMenuName;
552 wcex.lpszClassName = wc->lpszClassName;
553 wcex.hIconSm = 0;
554 return RegisterClassExW( &wcex );
558 /***********************************************************************
559 * RegisterClassExA (USER32.@)
561 ATOM WINAPI RegisterClassExA( const WNDCLASSEXA* wc )
563 ATOM atom;
564 CLASS *classPtr;
565 HINSTANCE instance;
567 GetDesktopWindow(); /* create the desktop window to trigger builtin class registration */
569 if (wc->cbSize != sizeof(*wc) || wc->cbClsExtra < 0 || wc->cbWndExtra < 0 ||
570 wc->hInstance == user32_module) /* we can't register a class for user32 */
572 SetLastError( ERROR_INVALID_PARAMETER );
573 return 0;
575 if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
577 if (!IS_INTRESOURCE(wc->lpszClassName))
579 WCHAR name[MAX_ATOM_LEN + 1];
581 if (!MultiByteToWideChar( CP_ACP, 0, wc->lpszClassName, -1, name, MAX_ATOM_LEN + 1 )) return 0;
582 classPtr = CLASS_RegisterClass( name, instance, !(wc->style & CS_GLOBALCLASS),
583 wc->style, wc->cbClsExtra, wc->cbWndExtra );
585 else
587 classPtr = CLASS_RegisterClass( (LPCWSTR)wc->lpszClassName, instance,
588 !(wc->style & CS_GLOBALCLASS), wc->style,
589 wc->cbClsExtra, wc->cbWndExtra );
591 if (!classPtr) return 0;
592 atom = classPtr->atomName;
594 TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
595 debugstr_a(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
596 wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
598 classPtr->hIcon = wc->hIcon;
599 classPtr->hIconSm = wc->hIconSm;
600 classPtr->hIconSmIntern = wc->hIcon && !wc->hIconSm ?
601 CopyImage( wc->hIcon, IMAGE_ICON,
602 GetSystemMetrics( SM_CXSMICON ),
603 GetSystemMetrics( SM_CYSMICON ),
604 LR_COPYFROMRESOURCE ) : NULL;
605 classPtr->hCursor = wc->hCursor;
606 classPtr->hbrBackground = wc->hbrBackground;
607 classPtr->winproc = WINPROC_AllocProc( wc->lpfnWndProc, FALSE );
608 CLASS_SetMenuNameA( classPtr, wc->lpszMenuName );
609 release_class_ptr( classPtr );
610 return atom;
614 /***********************************************************************
615 * RegisterClassExW (USER32.@)
617 ATOM WINAPI RegisterClassExW( const WNDCLASSEXW* wc )
619 ATOM atom;
620 CLASS *classPtr;
621 HINSTANCE instance;
623 GetDesktopWindow(); /* create the desktop window to trigger builtin class registration */
625 if (wc->cbSize != sizeof(*wc) || wc->cbClsExtra < 0 || wc->cbWndExtra < 0 ||
626 wc->hInstance == user32_module) /* we can't register a class for user32 */
628 SetLastError( ERROR_INVALID_PARAMETER );
629 return 0;
631 if (!(instance = wc->hInstance)) instance = GetModuleHandleW( NULL );
633 if (!(classPtr = CLASS_RegisterClass( wc->lpszClassName, instance, !(wc->style & CS_GLOBALCLASS),
634 wc->style, wc->cbClsExtra, wc->cbWndExtra )))
635 return 0;
637 atom = classPtr->atomName;
639 TRACE("name=%s atom=%04x wndproc=%p hinst=%p bg=%p style=%08x clsExt=%d winExt=%d class=%p\n",
640 debugstr_w(wc->lpszClassName), atom, wc->lpfnWndProc, instance, wc->hbrBackground,
641 wc->style, wc->cbClsExtra, wc->cbWndExtra, classPtr );
643 classPtr->hIcon = wc->hIcon;
644 classPtr->hIconSm = wc->hIconSm;
645 classPtr->hIconSmIntern = wc->hIcon && !wc->hIconSm ?
646 CopyImage( wc->hIcon, IMAGE_ICON,
647 GetSystemMetrics( SM_CXSMICON ),
648 GetSystemMetrics( SM_CYSMICON ),
649 LR_COPYFROMRESOURCE ) : NULL;
650 classPtr->hCursor = wc->hCursor;
651 classPtr->hbrBackground = wc->hbrBackground;
652 classPtr->winproc = WINPROC_AllocProc( wc->lpfnWndProc, TRUE );
653 CLASS_SetMenuNameW( classPtr, wc->lpszMenuName );
654 release_class_ptr( classPtr );
655 return atom;
659 /***********************************************************************
660 * UnregisterClassA (USER32.@)
662 BOOL WINAPI UnregisterClassA( LPCSTR className, HINSTANCE hInstance )
664 if (!IS_INTRESOURCE(className))
666 WCHAR name[MAX_ATOM_LEN + 1];
668 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, name, MAX_ATOM_LEN + 1 ))
669 return FALSE;
670 return UnregisterClassW( name, hInstance );
672 return UnregisterClassW( (LPCWSTR)className, hInstance );
675 /***********************************************************************
676 * UnregisterClassW (USER32.@)
678 BOOL WINAPI UnregisterClassW( LPCWSTR className, HINSTANCE hInstance )
680 CLASS *classPtr = NULL;
682 GetDesktopWindow(); /* create the desktop window to trigger builtin class registration */
684 SERVER_START_REQ( destroy_class )
686 req->instance = wine_server_client_ptr( hInstance );
687 if (!(req->atom = get_int_atom_value(className)) && className)
688 wine_server_add_data( req, className, strlenW(className) * sizeof(WCHAR) );
689 if (!wine_server_call_err( req )) classPtr = wine_server_get_ptr( reply->client_ptr );
691 SERVER_END_REQ;
693 if (classPtr) CLASS_FreeClass( classPtr );
694 return (classPtr != NULL);
698 /***********************************************************************
699 * GetClassWord (USER32.@)
701 WORD WINAPI GetClassWord( HWND hwnd, INT offset )
703 CLASS *class;
704 WORD retvalue = 0;
706 if (offset < 0) return GetClassLongA( hwnd, offset );
708 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
710 if (class == CLASS_OTHER_PROCESS)
712 SERVER_START_REQ( set_class_info )
714 req->window = wine_server_user_handle( hwnd );
715 req->flags = 0;
716 req->extra_offset = offset;
717 req->extra_size = sizeof(retvalue);
718 if (!wine_server_call_err( req ))
719 memcpy( &retvalue, &reply->old_extra_value, sizeof(retvalue) );
721 SERVER_END_REQ;
722 return retvalue;
725 if (offset <= class->cbClsExtra - sizeof(WORD))
726 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(retvalue) );
727 else
728 SetLastError( ERROR_INVALID_INDEX );
729 release_class_ptr( class );
730 return retvalue;
734 /***********************************************************************
735 * CLASS_GetClassLong
737 * Implementation of GetClassLong(Ptr)A/W
739 static ULONG_PTR CLASS_GetClassLong( HWND hwnd, INT offset, UINT size,
740 BOOL unicode )
742 CLASS *class;
743 ULONG_PTR retvalue = 0;
745 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
747 if (class == CLASS_OTHER_PROCESS)
749 SERVER_START_REQ( set_class_info )
751 req->window = wine_server_user_handle( hwnd );
752 req->flags = 0;
753 req->extra_offset = (offset >= 0) ? offset : -1;
754 req->extra_size = (offset >= 0) ? size : 0;
755 if (!wine_server_call_err( req ))
757 switch(offset)
759 case GCLP_HBRBACKGROUND:
760 case GCLP_HCURSOR:
761 case GCLP_HICON:
762 case GCLP_HICONSM:
763 case GCLP_WNDPROC:
764 case GCLP_MENUNAME:
765 FIXME( "offset %d (%s) not supported on other process window %p\n",
766 offset, SPY_GetClassLongOffsetName(offset), hwnd );
767 SetLastError( ERROR_INVALID_HANDLE );
768 break;
769 case GCL_STYLE:
770 retvalue = reply->old_style;
771 break;
772 case GCL_CBWNDEXTRA:
773 retvalue = reply->old_win_extra;
774 break;
775 case GCL_CBCLSEXTRA:
776 retvalue = reply->old_extra;
777 break;
778 case GCLP_HMODULE:
779 retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
780 break;
781 case GCW_ATOM:
782 retvalue = reply->old_atom;
783 break;
784 default:
785 if (offset >= 0)
787 if (size == sizeof(DWORD))
789 DWORD retdword;
790 memcpy( &retdword, &reply->old_extra_value, sizeof(DWORD) );
791 retvalue = retdword;
793 else
794 memcpy( &retvalue, &reply->old_extra_value,
795 sizeof(ULONG_PTR) );
797 else SetLastError( ERROR_INVALID_INDEX );
798 break;
802 SERVER_END_REQ;
803 return retvalue;
806 if (offset >= 0)
808 if (offset <= class->cbClsExtra - size)
810 if (size == sizeof(DWORD))
812 DWORD retdword;
813 memcpy( &retdword, (char *)(class + 1) + offset, sizeof(DWORD) );
814 retvalue = retdword;
816 else
817 memcpy( &retvalue, (char *)(class + 1) + offset, sizeof(ULONG_PTR) );
819 else
820 SetLastError( ERROR_INVALID_INDEX );
821 release_class_ptr( class );
822 return retvalue;
825 switch(offset)
827 case GCLP_HBRBACKGROUND:
828 retvalue = (ULONG_PTR)class->hbrBackground;
829 break;
830 case GCLP_HCURSOR:
831 retvalue = (ULONG_PTR)class->hCursor;
832 break;
833 case GCLP_HICON:
834 retvalue = (ULONG_PTR)class->hIcon;
835 break;
836 case GCLP_HICONSM:
837 retvalue = (ULONG_PTR)(class->hIconSm ? class->hIconSm : class->hIconSmIntern);
838 break;
839 case GCL_STYLE:
840 retvalue = class->style;
841 break;
842 case GCL_CBWNDEXTRA:
843 retvalue = class->cbWndExtra;
844 break;
845 case GCL_CBCLSEXTRA:
846 retvalue = class->cbClsExtra;
847 break;
848 case GCLP_HMODULE:
849 retvalue = (ULONG_PTR)class->hInstance;
850 break;
851 case GCLP_WNDPROC:
852 retvalue = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
853 break;
854 case GCLP_MENUNAME:
855 retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
856 if (unicode)
857 retvalue = (ULONG_PTR)CLASS_GetMenuNameW( class );
858 else
859 retvalue = (ULONG_PTR)CLASS_GetMenuNameA( class );
860 break;
861 case GCW_ATOM:
862 retvalue = class->atomName;
863 break;
864 default:
865 SetLastError( ERROR_INVALID_INDEX );
866 break;
868 release_class_ptr( class );
869 return retvalue;
873 /***********************************************************************
874 * GetClassLongW (USER32.@)
876 DWORD WINAPI GetClassLongW( HWND hwnd, INT offset )
878 return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), TRUE );
883 /***********************************************************************
884 * GetClassLongA (USER32.@)
886 DWORD WINAPI GetClassLongA( HWND hwnd, INT offset )
888 return CLASS_GetClassLong( hwnd, offset, sizeof(DWORD), FALSE );
892 /***********************************************************************
893 * SetClassWord (USER32.@)
895 WORD WINAPI SetClassWord( HWND hwnd, INT offset, WORD newval )
897 CLASS *class;
898 WORD retval = 0;
900 if (offset < 0) return SetClassLongA( hwnd, offset, (DWORD)newval );
902 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
904 SERVER_START_REQ( set_class_info )
906 req->window = wine_server_user_handle( hwnd );
907 req->flags = SET_CLASS_EXTRA;
908 req->extra_offset = offset;
909 req->extra_size = sizeof(newval);
910 memcpy( &req->extra_value, &newval, sizeof(newval) );
911 if (!wine_server_call_err( req ))
913 void *ptr = (char *)(class + 1) + offset;
914 memcpy( &retval, ptr, sizeof(retval) );
915 memcpy( ptr, &newval, sizeof(newval) );
918 SERVER_END_REQ;
919 release_class_ptr( class );
920 return retval;
924 /***********************************************************************
925 * CLASS_SetClassLong
927 * Implementation of SetClassLong(Ptr)A/W
929 static ULONG_PTR CLASS_SetClassLong( HWND hwnd, INT offset, LONG_PTR newval,
930 UINT size, BOOL unicode )
932 CLASS *class;
933 ULONG_PTR retval = 0;
935 if (!(class = get_class_ptr( hwnd, TRUE ))) return 0;
937 if (offset >= 0)
939 if (set_server_info( hwnd, offset, newval, size ))
941 void *ptr = (char *)(class + 1) + offset;
942 if ( size == sizeof(LONG) )
944 DWORD retdword;
945 LONG newlong = newval;
946 memcpy( &retdword, ptr, sizeof(DWORD) );
947 memcpy( ptr, &newlong, sizeof(LONG) );
948 retval = retdword;
950 else
952 memcpy( &retval, ptr, sizeof(ULONG_PTR) );
953 memcpy( ptr, &newval, sizeof(LONG_PTR) );
957 else switch(offset)
959 case GCLP_MENUNAME:
960 if ( unicode )
961 CLASS_SetMenuNameW( class, (LPCWSTR)newval );
962 else
963 CLASS_SetMenuNameA( class, (LPCSTR)newval );
964 retval = 0; /* Old value is now meaningless anyway */
965 break;
966 case GCLP_WNDPROC:
967 retval = (ULONG_PTR)WINPROC_GetProc( class->winproc, unicode );
968 class->winproc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
969 break;
970 case GCLP_HBRBACKGROUND:
971 retval = (ULONG_PTR)class->hbrBackground;
972 class->hbrBackground = (HBRUSH)newval;
973 break;
974 case GCLP_HCURSOR:
975 retval = (ULONG_PTR)class->hCursor;
976 class->hCursor = (HCURSOR)newval;
977 break;
978 case GCLP_HICON:
979 retval = (ULONG_PTR)class->hIcon;
980 if (class->hIconSmIntern)
982 DestroyIcon(class->hIconSmIntern);
983 class->hIconSmIntern = NULL;
985 if (newval && !class->hIconSm)
986 class->hIconSmIntern = CopyImage( (HICON)newval, IMAGE_ICON,
987 GetSystemMetrics( SM_CXSMICON ), GetSystemMetrics( SM_CYSMICON ),
988 LR_COPYFROMRESOURCE );
989 class->hIcon = (HICON)newval;
990 break;
991 case GCLP_HICONSM:
992 retval = (ULONG_PTR)class->hIconSm;
993 if (retval && !newval && class->hIcon)
994 class->hIconSmIntern = CopyImage( class->hIcon, IMAGE_ICON,
995 GetSystemMetrics( SM_CXSMICON ), GetSystemMetrics( SM_CYSMICON ),
996 LR_COPYFROMRESOURCE );
997 else if (newval && class->hIconSmIntern)
999 DestroyIcon(class->hIconSmIntern);
1000 class->hIconSmIntern = NULL;
1002 class->hIconSm = (HICON)newval;
1003 break;
1004 case GCL_STYLE:
1005 if (!set_server_info( hwnd, offset, newval, size )) break;
1006 retval = class->style;
1007 class->style = newval;
1008 break;
1009 case GCL_CBWNDEXTRA:
1010 if (!set_server_info( hwnd, offset, newval, size )) break;
1011 retval = class->cbWndExtra;
1012 class->cbWndExtra = newval;
1013 break;
1014 case GCLP_HMODULE:
1015 if (!set_server_info( hwnd, offset, newval, size )) break;
1016 retval = (ULONG_PTR)class->hInstance;
1017 class->hInstance = (HINSTANCE)newval;
1018 break;
1019 case GCW_ATOM:
1020 if (!set_server_info( hwnd, offset, newval, size )) break;
1021 retval = class->atomName;
1022 class->atomName = newval;
1023 GlobalGetAtomNameW( newval, class->name, sizeof(class->name)/sizeof(WCHAR) );
1024 break;
1025 case GCL_CBCLSEXTRA: /* cannot change this one */
1026 SetLastError( ERROR_INVALID_PARAMETER );
1027 break;
1028 default:
1029 SetLastError( ERROR_INVALID_INDEX );
1030 break;
1032 release_class_ptr( class );
1033 return retval;
1037 /***********************************************************************
1038 * SetClassLongW (USER32.@)
1040 DWORD WINAPI SetClassLongW( HWND hwnd, INT offset, LONG newval )
1042 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), TRUE );
1046 /***********************************************************************
1047 * SetClassLongA (USER32.@)
1049 DWORD WINAPI SetClassLongA( HWND hwnd, INT offset, LONG newval )
1051 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG), FALSE );
1055 /***********************************************************************
1056 * GetClassNameA (USER32.@)
1058 INT WINAPI GetClassNameA( HWND hwnd, LPSTR buffer, INT count )
1060 WCHAR tmpbuf[MAX_ATOM_LEN + 1];
1061 DWORD len;
1063 if (count <= 0) return 0;
1064 if (!GetClassNameW( hwnd, tmpbuf, sizeof(tmpbuf)/sizeof(WCHAR) )) return 0;
1065 RtlUnicodeToMultiByteN( buffer, count - 1, &len, tmpbuf, strlenW(tmpbuf) * sizeof(WCHAR) );
1066 buffer[len] = 0;
1067 return len;
1071 /***********************************************************************
1072 * GetClassNameW (USER32.@)
1074 INT WINAPI GetClassNameW( HWND hwnd, LPWSTR buffer, INT count )
1076 CLASS *class;
1077 INT ret;
1079 TRACE("%p %p %d\n", hwnd, buffer, count);
1081 if (count <= 0) return 0;
1083 if (!(class = get_class_ptr( hwnd, FALSE ))) return 0;
1085 if (class == CLASS_OTHER_PROCESS)
1087 WCHAR tmpbuf[MAX_ATOM_LEN + 1];
1089 ret = GlobalGetAtomNameW( GetClassLongW( hwnd, GCW_ATOM ), tmpbuf, MAX_ATOM_LEN + 1 );
1090 if (ret)
1092 ret = min(count - 1, ret);
1093 memcpy(buffer, tmpbuf, ret * sizeof(WCHAR));
1094 buffer[ret] = 0;
1097 else
1099 lstrcpynW( buffer, class->name, count );
1100 release_class_ptr( class );
1101 ret = strlenW( buffer );
1103 return ret;
1107 /***********************************************************************
1108 * RealGetWindowClassA (USER32.@)
1110 UINT WINAPI RealGetWindowClassA( HWND hwnd, LPSTR buffer, UINT count )
1112 return GetClassNameA( hwnd, buffer, count );
1116 /***********************************************************************
1117 * RealGetWindowClassW (USER32.@)
1119 UINT WINAPI RealGetWindowClassW( HWND hwnd, LPWSTR buffer, UINT count )
1121 return GetClassNameW( hwnd, buffer, count );
1125 /***********************************************************************
1126 * GetClassInfoA (USER32.@)
1128 BOOL WINAPI GetClassInfoA( HINSTANCE hInstance, LPCSTR name, WNDCLASSA *wc )
1130 WNDCLASSEXA wcex;
1131 UINT ret = GetClassInfoExA( hInstance, name, &wcex );
1133 if (ret)
1135 wc->style = wcex.style;
1136 wc->lpfnWndProc = wcex.lpfnWndProc;
1137 wc->cbClsExtra = wcex.cbClsExtra;
1138 wc->cbWndExtra = wcex.cbWndExtra;
1139 wc->hInstance = wcex.hInstance;
1140 wc->hIcon = wcex.hIcon;
1141 wc->hCursor = wcex.hCursor;
1142 wc->hbrBackground = wcex.hbrBackground;
1143 wc->lpszMenuName = wcex.lpszMenuName;
1144 wc->lpszClassName = wcex.lpszClassName;
1146 return ret;
1150 /***********************************************************************
1151 * GetClassInfoW (USER32.@)
1153 BOOL WINAPI GetClassInfoW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSW *wc )
1155 WNDCLASSEXW wcex;
1156 UINT ret = GetClassInfoExW( hInstance, name, &wcex );
1158 if (ret)
1160 wc->style = wcex.style;
1161 wc->lpfnWndProc = wcex.lpfnWndProc;
1162 wc->cbClsExtra = wcex.cbClsExtra;
1163 wc->cbWndExtra = wcex.cbWndExtra;
1164 wc->hInstance = wcex.hInstance;
1165 wc->hIcon = wcex.hIcon;
1166 wc->hCursor = wcex.hCursor;
1167 wc->hbrBackground = wcex.hbrBackground;
1168 wc->lpszMenuName = wcex.lpszMenuName;
1169 wc->lpszClassName = wcex.lpszClassName;
1171 return ret;
1175 /***********************************************************************
1176 * GetClassInfoExA (USER32.@)
1178 BOOL WINAPI GetClassInfoExA( HINSTANCE hInstance, LPCSTR name, WNDCLASSEXA *wc )
1180 ATOM atom;
1181 CLASS *classPtr;
1183 TRACE("%p %s %p\n", hInstance, debugstr_a(name), wc);
1185 if (!wc)
1187 SetLastError( ERROR_NOACCESS );
1188 return FALSE;
1191 if (!hInstance) hInstance = user32_module;
1193 if (!IS_INTRESOURCE(name))
1195 WCHAR nameW[MAX_ATOM_LEN + 1];
1196 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, sizeof(nameW)/sizeof(WCHAR) ))
1197 return FALSE;
1198 classPtr = CLASS_FindClass( nameW, hInstance );
1200 else classPtr = CLASS_FindClass( (LPCWSTR)name, hInstance );
1202 if (!classPtr)
1204 SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1205 return FALSE;
1207 wc->style = classPtr->style;
1208 wc->lpfnWndProc = WINPROC_GetProc( classPtr->winproc, FALSE );
1209 wc->cbClsExtra = classPtr->cbClsExtra;
1210 wc->cbWndExtra = classPtr->cbWndExtra;
1211 wc->hInstance = (hInstance == user32_module) ? 0 : hInstance;
1212 wc->hIcon = classPtr->hIcon;
1213 wc->hIconSm = classPtr->hIconSm ? classPtr->hIconSm : classPtr->hIconSmIntern;
1214 wc->hCursor = classPtr->hCursor;
1215 wc->hbrBackground = classPtr->hbrBackground;
1216 wc->lpszMenuName = CLASS_GetMenuNameA( classPtr );
1217 wc->lpszClassName = name;
1218 atom = classPtr->atomName;
1219 release_class_ptr( classPtr );
1221 /* We must return the atom of the class here instead of just TRUE. */
1222 return atom;
1226 /***********************************************************************
1227 * GetClassInfoExW (USER32.@)
1229 BOOL WINAPI GetClassInfoExW( HINSTANCE hInstance, LPCWSTR name, WNDCLASSEXW *wc )
1231 ATOM atom;
1232 CLASS *classPtr;
1234 TRACE("%p %s %p\n", hInstance, debugstr_w(name), wc);
1236 if (!wc)
1238 SetLastError( ERROR_NOACCESS );
1239 return FALSE;
1242 if (!hInstance) hInstance = user32_module;
1244 if (!(classPtr = CLASS_FindClass( name, hInstance )))
1246 SetLastError( ERROR_CLASS_DOES_NOT_EXIST );
1247 return FALSE;
1249 wc->style = classPtr->style;
1250 wc->lpfnWndProc = WINPROC_GetProc( classPtr->winproc, TRUE );
1251 wc->cbClsExtra = classPtr->cbClsExtra;
1252 wc->cbWndExtra = classPtr->cbWndExtra;
1253 wc->hInstance = (hInstance == user32_module) ? 0 : hInstance;
1254 wc->hIcon = classPtr->hIcon;
1255 wc->hIconSm = classPtr->hIconSm ? classPtr->hIconSm : classPtr->hIconSmIntern;
1256 wc->hCursor = classPtr->hCursor;
1257 wc->hbrBackground = classPtr->hbrBackground;
1258 wc->lpszMenuName = CLASS_GetMenuNameW( classPtr );
1259 wc->lpszClassName = name;
1260 atom = classPtr->atomName;
1261 release_class_ptr( classPtr );
1263 /* We must return the atom of the class here instead of just TRUE. */
1264 return atom;
1268 #if 0 /* toolhelp is in kernel, so this cannot work */
1270 /***********************************************************************
1271 * ClassFirst (TOOLHELP.69)
1273 BOOL16 WINAPI ClassFirst16( CLASSENTRY *pClassEntry )
1275 TRACE("%p\n",pClassEntry);
1276 pClassEntry->wNext = 1;
1277 return ClassNext16( pClassEntry );
1281 /***********************************************************************
1282 * ClassNext (TOOLHELP.70)
1284 BOOL16 WINAPI ClassNext16( CLASSENTRY *pClassEntry )
1286 int i;
1287 CLASS *class = firstClass;
1289 TRACE("%p\n",pClassEntry);
1291 if (!pClassEntry->wNext) return FALSE;
1292 for (i = 1; (i < pClassEntry->wNext) && class; i++) class = class->next;
1293 if (!class)
1295 pClassEntry->wNext = 0;
1296 return FALSE;
1298 pClassEntry->hInst = class->hInstance;
1299 pClassEntry->wNext++;
1300 GlobalGetAtomNameA( class->atomName, pClassEntry->szClassName,
1301 sizeof(pClassEntry->szClassName) );
1302 return TRUE;
1304 #endif
1306 /* 64bit versions */
1308 #ifdef GetClassLongPtrA
1309 #undef GetClassLongPtrA
1310 #endif
1312 #ifdef GetClassLongPtrW
1313 #undef GetClassLongPtrW
1314 #endif
1316 #ifdef SetClassLongPtrA
1317 #undef SetClassLongPtrA
1318 #endif
1320 #ifdef SetClassLongPtrW
1321 #undef SetClassLongPtrW
1322 #endif
1324 /***********************************************************************
1325 * GetClassLongPtrA (USER32.@)
1327 ULONG_PTR WINAPI GetClassLongPtrA( HWND hwnd, INT offset )
1329 return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), FALSE );
1332 /***********************************************************************
1333 * GetClassLongPtrW (USER32.@)
1335 ULONG_PTR WINAPI GetClassLongPtrW( HWND hwnd, INT offset )
1337 return CLASS_GetClassLong( hwnd, offset, sizeof(ULONG_PTR), TRUE );
1340 /***********************************************************************
1341 * SetClassLongPtrW (USER32.@)
1343 ULONG_PTR WINAPI SetClassLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
1345 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), TRUE );
1348 /***********************************************************************
1349 * SetClassLongPtrA (USER32.@)
1351 ULONG_PTR WINAPI SetClassLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
1353 return CLASS_SetClassLong( hwnd, offset, newval, sizeof(LONG_PTR), FALSE );