ntoskrnl.exe: Implement ExAcquireFastMutex and ExReleaseFastMutex.
[wine.git] / dlls / user32 / resource.c
blob8dad3d53d575c12a2e8276bf59501d43d4bd619d
1 /*
2 * USER resource functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995, 2009 Alexandre Julliard
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"
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "winternl.h"
30 #include "winnls.h"
31 #include "wine/debug.h"
32 #include "user_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(resource);
35 WINE_DECLARE_DEBUG_CHANNEL(accel);
37 /* this is the 8 byte accel struct used in Win32 resources (internal only) */
38 typedef struct
40 WORD fVirt;
41 WORD key;
42 WORD cmd;
43 WORD pad;
44 } PE_ACCEL;
46 /* the accelerator user object */
47 struct accelerator
49 struct user_object obj;
50 unsigned int count;
51 PE_ACCEL table[1];
54 /**********************************************************************
55 * LoadAcceleratorsW (USER32.@)
57 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance, LPCWSTR name)
59 struct accelerator *accel;
60 const PE_ACCEL *table;
61 HRSRC rsrc;
62 HACCEL handle;
63 DWORD count;
65 if (!(rsrc = FindResourceW( instance, name, (LPWSTR)RT_ACCELERATOR ))) return 0;
66 table = LoadResource( instance, rsrc );
67 count = SizeofResource( instance, rsrc ) / sizeof(*table);
68 if (!count) return 0;
69 accel = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( struct accelerator, table[count] ));
70 if (!accel) return 0;
71 accel->count = count;
72 memcpy( accel->table, table, count * sizeof(*table) );
73 if (!(handle = alloc_user_handle( &accel->obj, USER_ACCEL )))
74 HeapFree( GetProcessHeap(), 0, accel );
75 TRACE_(accel)("%p %s returning %p\n", instance, debugstr_w(name), handle );
76 return handle;
79 /***********************************************************************
80 * LoadAcceleratorsA (USER32.@)
82 HACCEL WINAPI LoadAcceleratorsA(HINSTANCE instance,LPCSTR lpTableName)
84 INT len;
85 LPWSTR uni;
86 HACCEL result = 0;
88 if (IS_INTRESOURCE(lpTableName)) return LoadAcceleratorsW( instance, (LPCWSTR)lpTableName );
90 len = MultiByteToWideChar( CP_ACP, 0, lpTableName, -1, NULL, 0 );
91 if ((uni = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
93 MultiByteToWideChar( CP_ACP, 0, lpTableName, -1, uni, len );
94 result = LoadAcceleratorsW(instance,uni);
95 HeapFree( GetProcessHeap(), 0, uni);
97 return result;
100 /**********************************************************************
101 * CopyAcceleratorTableA (USER32.@)
103 INT WINAPI CopyAcceleratorTableA(HACCEL src, LPACCEL dst, INT count)
105 char ch;
106 int i, ret = CopyAcceleratorTableW( src, dst, count );
108 if (ret && dst)
110 for (i = 0; i < ret; i++)
112 if (dst[i].fVirt & FVIRTKEY) continue;
113 WideCharToMultiByte( CP_ACP, 0, &dst[i].key, 1, &ch, 1, NULL, NULL );
114 dst[i].key = ch;
117 return ret;
120 /**********************************************************************
121 * CopyAcceleratorTableW (USER32.@)
123 INT WINAPI CopyAcceleratorTableW(HACCEL src, LPACCEL dst, INT count)
125 struct accelerator *accel;
126 int i;
128 if (!(accel = get_user_handle_ptr( src, USER_ACCEL ))) return 0;
129 if (accel == OBJ_OTHER_PROCESS)
131 FIXME( "other process handle %p?\n", src );
132 return 0;
134 if (dst)
136 if (count > accel->count) count = accel->count;
137 for (i = 0; i < count; i++)
139 dst[i].fVirt = accel->table[i].fVirt & 0x7f;
140 dst[i].key = accel->table[i].key;
141 dst[i].cmd = accel->table[i].cmd;
144 else count = accel->count;
145 release_user_handle_ptr( accel );
146 return count;
149 /*********************************************************************
150 * CreateAcceleratorTableA (USER32.@)
152 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT count)
154 struct accelerator *accel;
155 HACCEL handle;
156 int i;
158 if (count < 1)
160 SetLastError( ERROR_INVALID_PARAMETER );
161 return 0;
163 accel = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( struct accelerator, table[count] ));
164 if (!accel) return 0;
165 accel->count = count;
166 for (i = 0; i < count; i++)
168 accel->table[i].fVirt = lpaccel[i].fVirt;
169 accel->table[i].cmd = lpaccel[i].cmd;
170 if (!(lpaccel[i].fVirt & FVIRTKEY))
172 char ch = lpaccel[i].key;
173 MultiByteToWideChar( CP_ACP, 0, &ch, 1, &accel->table[i].key, 1 );
175 else accel->table[i].key = lpaccel[i].key;
177 if (!(handle = alloc_user_handle( &accel->obj, USER_ACCEL )))
178 HeapFree( GetProcessHeap(), 0, accel );
179 TRACE_(accel)("returning %p\n", handle );
180 return handle;
183 /*********************************************************************
184 * CreateAcceleratorTableW (USER32.@)
186 HACCEL WINAPI CreateAcceleratorTableW(LPACCEL lpaccel, INT count)
188 struct accelerator *accel;
189 HACCEL handle;
190 int i;
192 if (count < 1)
194 SetLastError( ERROR_INVALID_PARAMETER );
195 return 0;
197 accel = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( struct accelerator, table[count] ));
198 if (!accel) return 0;
199 accel->count = count;
200 for (i = 0; i < count; i++)
202 accel->table[i].fVirt = lpaccel[i].fVirt;
203 accel->table[i].key = lpaccel[i].key;
204 accel->table[i].cmd = lpaccel[i].cmd;
206 if (!(handle = alloc_user_handle( &accel->obj, USER_ACCEL )))
207 HeapFree( GetProcessHeap(), 0, accel );
208 TRACE_(accel)("returning %p\n", handle );
209 return handle;
212 /******************************************************************************
213 * DestroyAcceleratorTable [USER32.@]
214 * Destroys an accelerator table
216 * PARAMS
217 * handle [I] Handle to accelerator table
219 * RETURNS
220 * Success: TRUE
221 * Failure: FALSE
223 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
225 struct accelerator *accel;
227 if (!(accel = free_user_handle( handle, USER_ACCEL ))) return FALSE;
228 if (accel == OBJ_OTHER_PROCESS)
230 FIXME( "other process handle %p?\n", accel );
231 return FALSE;
233 return HeapFree( GetProcessHeap(), 0, accel );
236 /**********************************************************************
237 * LoadStringW (USER32.@)
239 INT WINAPI DECLSPEC_HOTPATCH LoadStringW( HINSTANCE instance, UINT resource_id,
240 LPWSTR buffer, INT buflen )
242 HGLOBAL hmem;
243 HRSRC hrsrc;
244 WCHAR *p;
245 int string_num;
246 int i;
248 TRACE("instance = %p, id = %04x, buffer = %p, length = %d\n",
249 instance, resource_id, buffer, buflen);
251 if(buffer == NULL)
252 return 0;
254 /* Use loword (incremented by 1) as resourceid */
255 hrsrc = FindResourceW( instance, MAKEINTRESOURCEW((LOWORD(resource_id) >> 4) + 1),
256 (LPWSTR)RT_STRING );
257 if (!hrsrc) return 0;
258 hmem = LoadResource( instance, hrsrc );
259 if (!hmem) return 0;
261 p = LockResource(hmem);
262 string_num = resource_id & 0x000f;
263 for (i = 0; i < string_num; i++)
264 p += *p + 1;
266 TRACE("strlen = %d\n", (int)*p );
268 /*if buflen == 0, then return a read-only pointer to the resource itself in buffer
269 it is assumed that buffer is actually a (LPWSTR *) */
270 if(buflen == 0)
272 *((LPWSTR *)buffer) = p + 1;
273 return *p;
276 i = min(buflen - 1, *p);
277 if (i > 0) {
278 memcpy(buffer, p + 1, i * sizeof (WCHAR));
279 buffer[i] = 0;
280 } else {
281 if (buflen > 1) {
282 buffer[0] = 0;
283 return 0;
287 TRACE("%s loaded !\n", debugstr_w(buffer));
288 return i;
291 /**********************************************************************
292 * LoadStringA (USER32.@)
294 INT WINAPI DECLSPEC_HOTPATCH LoadStringA( HINSTANCE instance, UINT resource_id, LPSTR buffer, INT buflen )
296 HGLOBAL hmem;
297 HRSRC hrsrc;
298 DWORD retval = 0;
300 TRACE("instance = %p, id = %04x, buffer = %p, length = %d\n",
301 instance, resource_id, buffer, buflen);
303 if (!buflen) return -1;
305 /* Use loword (incremented by 1) as resourceid */
306 if ((hrsrc = FindResourceW( instance, MAKEINTRESOURCEW((LOWORD(resource_id) >> 4) + 1),
307 (LPWSTR)RT_STRING )) &&
308 (hmem = LoadResource( instance, hrsrc )))
310 const WCHAR *p = LockResource(hmem);
311 unsigned int id = resource_id & 0x000f;
313 while (id--) p += *p + 1;
315 if (buflen != 1)
316 RtlUnicodeToMultiByteN( buffer, buflen - 1, &retval, p + 1, *p * sizeof(WCHAR) );
318 buffer[retval] = 0;
319 TRACE("returning %s\n", debugstr_a(buffer));
320 return retval;
323 /**********************************************************************
324 * GetGuiResources (USER32.@)
326 DWORD WINAPI GetGuiResources( HANDLE hProcess, DWORD uiFlags )
328 static BOOL warn = TRUE;
330 if (warn) {
331 FIXME("(%p,%x): stub\n",hProcess,uiFlags);
332 warn = FALSE;
335 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
336 return 0;