Copy msimg32
[reactos.git] / reactos / lib / setupapi / misc.c
1 /*
2 * Setupapi miscellaneous functions
3 *
4 * Copyright 2005 Eric Kohl
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "setupapi.h"
29
30 #include "wine/unicode.h"
31
32
33 /**************************************************************************
34 * MyFree [SETUPAPI.@]
35 *
36 * Frees an allocated memory block from the process heap.
37 *
38 * PARAMS
39 * lpMem [I] pointer to memory block which will be freed
40 *
41 * RETURNS
42 * None
43 */
44
45 VOID WINAPI MyFree(LPVOID lpMem)
46 {
47 HeapFree(GetProcessHeap(), 0, lpMem);
48 }
49
50
51 /**************************************************************************
52 * MyMalloc [SETUPAPI.@]
53 *
54 * Allocates memory block from the process heap.
55 *
56 * PARAMS
57 * dwSize [I] size of the allocated memory block
58 *
59 * RETURNS
60 * Success: pointer to allocated memory block
61 * Failure: NULL
62 */
63
64 LPVOID WINAPI MyMalloc(DWORD dwSize)
65 {
66 return HeapAlloc(GetProcessHeap(), 0, dwSize);
67 }
68
69
70 /**************************************************************************
71 * MyRealloc [SETUPAPI.@]
72 *
73 * Changes the size of an allocated memory block or allocates a memory
74 * block from the process heap.
75 *
76 * PARAMS
77 * lpSrc [I] pointer to memory block which will be resized
78 * dwSize [I] new size of the memory block
79 *
80 * RETURNS
81 * Success: pointer to the resized memory block
82 * Failure: NULL
83 *
84 * NOTES
85 * If lpSrc is a NULL-pointer, then MyRealloc allocates a memory
86 * block like MyMalloc.
87 */
88
89 LPVOID WINAPI MyRealloc(LPVOID lpSrc, DWORD dwSize)
90 {
91 if (lpSrc == NULL)
92 return HeapAlloc(GetProcessHeap(), 0, dwSize);
93
94 return HeapReAlloc(GetProcessHeap(), 0, lpSrc, dwSize);
95 }
96
97
98 /**************************************************************************
99 * DuplicateString [SETUPAPI.@]
100 *
101 * Duplicates a unicode string.
102 *
103 * PARAMS
104 * lpSrc [I] pointer to the unicode string that will be duplicated
105 *
106 * RETURNS
107 * Success: pointer to the duplicated unicode string
108 * Failure: NULL
109 *
110 * NOTES
111 * Call MyFree() to release the duplicated string.
112 */
113
114 LPWSTR WINAPI DuplicateString(LPCWSTR lpSrc)
115 {
116 LPWSTR lpDst;
117
118 lpDst = MyMalloc((lstrlenW(lpSrc) + 1) * sizeof(WCHAR));
119 if (lpDst == NULL)
120 return NULL;
121
122 strcpyW(lpDst, lpSrc);
123
124 return lpDst;
125 }
126
127
128 /**************************************************************************
129 * QueryRegistryValue [SETUPAPI.@]
130 *
131 * Retrieves value data from the registry and allocates memory for the
132 * value data.
133 *
134 * PARAMS
135 * hKey [I] Handle of the key to query
136 * lpValueName [I] Name of value under hkey to query
137 * lpData [O] Destination for the values contents,
138 * lpType [O] Destination for the value type
139 * lpcbData [O] Destination for the size of data
140 *
141 * RETURNS
142 * Success: ERROR_SUCCESS
143 * Failure: Otherwise
144 *
145 * NOTES
146 * Use MyFree to release the lpData buffer.
147 */
148
149 LONG WINAPI QueryRegistryValue(HKEY hKey,
150 LPCWSTR lpValueName,
151 LPBYTE *lpData,
152 LPDWORD lpType,
153 LPDWORD lpcbData)
154 {
155 LONG lError;
156
157 /* Get required buffer size */
158 *lpcbData = 0;
159 lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, NULL, lpcbData);
160 if (lError != ERROR_SUCCESS)
161 return lError;
162
163 /* Allocate buffer */
164 *lpData = MyMalloc(*lpcbData);
165 if (*lpData == NULL)
166 return ERROR_NOT_ENOUGH_MEMORY;
167
168 /* Query registry value */
169 lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, *lpData, lpcbData);
170 if (lError != ERROR_SUCCESS)
171 MyFree(*lpData);
172
173 return lError;
174 }