sti: Use IRegistrar instead of regsvr.c.
[wine] / dlls / sti / sti.c
1 /*
2  * Copyright (C) 2002 Aric Stewart for CodeWeavers
3  * Copyright (C) 2009 Damjan Jovanovic
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <stdarg.h>
21
22 #define COBJMACROS
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "winerror.h"
28 #include "objbase.h"
29 #include "sti.h"
30
31 #include "sti_private.h"
32
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(sti);
37
38 static const WCHAR registeredAppsLaunchPath[] = {
39     'S','O','F','T','W','A','R','E','\\',
40     'M','i','c','r','o','s','o','f','t','\\',
41     'W','i','n','d','o','w','s','\\',
42     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
43     'S','t','i','l','l','I','m','a','g','e','\\',
44     'R','e','g','i','s','t','e','r','e','d',' ','A','p','p','l','i','c','a','t','i','o','n','s',0
45 };
46
47 static inline stillimage *impl_from_StillImageW(IStillImageW *iface)
48 {
49     return (stillimage *)((char*)iface - FIELD_OFFSET(stillimage, lpVtbl));
50 }
51
52 static HRESULT WINAPI stillimagew_QueryInterface(IStillImageW *iface, REFIID riid, void **ppvObject)
53 {
54     stillimage *This = impl_from_StillImageW(iface);
55     TRACE("(%p %s %p)\n", This, debugstr_guid(riid), ppvObject);
56     return IUnknown_QueryInterface(This->pUnkOuter, riid, ppvObject);
57 }
58
59 static ULONG WINAPI stillimagew_AddRef(IStillImageW *iface)
60 {
61     stillimage *This = impl_from_StillImageW(iface);
62     return IUnknown_AddRef(This->pUnkOuter);
63 }
64
65 static ULONG WINAPI stillimagew_Release(IStillImageW *iface)
66 {
67     stillimage *This = impl_from_StillImageW(iface);
68     return IUnknown_Release(This->pUnkOuter);
69 }
70
71 static HRESULT WINAPI stillimagew_Initialize(IStillImageW *iface, HINSTANCE hinst, DWORD dwVersion)
72 {
73     stillimage *This = impl_from_StillImageW(iface);
74     TRACE("(%p, %p, 0x%X)\n", This, hinst, dwVersion);
75     return S_OK;
76 }
77
78 static HRESULT WINAPI stillimagew_GetDeviceList(IStillImageW *iface, DWORD dwType, DWORD dwFlags,
79                                                 DWORD *pdwItemsReturned, LPVOID *ppBuffer)
80 {
81     stillimage *This = impl_from_StillImageW(iface);
82     FIXME("(%p, %u, 0x%X, %p, %p): stub\n", This, dwType, dwFlags, pdwItemsReturned, ppBuffer);
83     return E_NOTIMPL;
84 }
85
86 static HRESULT WINAPI stillimagew_GetDeviceInfo(IStillImageW *iface, LPWSTR pwszDeviceName,
87                                                 LPVOID *ppBuffer)
88 {
89     stillimage *This = impl_from_StillImageW(iface);
90     FIXME("(%p, %s, %p): stub\n", This, debugstr_w(pwszDeviceName), ppBuffer);
91     return E_NOTIMPL;
92 }
93
94 static HRESULT WINAPI stillimagew_CreateDevice(IStillImageW *iface, LPWSTR pwszDeviceName, DWORD dwMode,
95                                                PSTIDEVICEW *pDevice, LPUNKNOWN pUnkOuter)
96 {
97     stillimage *This = impl_from_StillImageW(iface);
98     FIXME("(%p, %s, %u, %p, %p): stub\n", This, debugstr_w(pwszDeviceName), dwMode, pDevice, pUnkOuter);
99     return E_NOTIMPL;
100 }
101
102 static HRESULT WINAPI stillimagew_GetDeviceValue(IStillImageW *iface, LPWSTR pwszDeviceName, LPWSTR pValueName,
103                                                  LPDWORD pType, LPBYTE pData, LPDWORD cbData)
104 {
105     stillimage *This = impl_from_StillImageW(iface);
106     FIXME("(%p, %s, %s, %p, %p, %p): stub\n", This, debugstr_w(pwszDeviceName), debugstr_w(pValueName),
107         pType, pData, cbData);
108     return E_NOTIMPL;
109 }
110
111 static HRESULT WINAPI stillimagew_SetDeviceValue(IStillImageW *iface, LPWSTR pwszDeviceName, LPWSTR pValueName,
112                                                  DWORD type, LPBYTE pData, DWORD cbData)
113 {
114     stillimage *This = impl_from_StillImageW(iface);
115     FIXME("(%p, %s, %s, %u, %p, %u): stub\n", This, debugstr_w(pwszDeviceName), debugstr_w(pValueName),
116         type, pData, cbData);
117     return E_NOTIMPL;
118 }
119
120 static HRESULT WINAPI stillimagew_GetSTILaunchInformation(IStillImageW *iface, LPWSTR pwszDeviceName,
121                                                           DWORD *pdwEventCode, LPWSTR pwszEventName)
122 {
123     stillimage *This = impl_from_StillImageW(iface);
124     FIXME("(%p, %p, %p, %p): stub\n", This, pwszDeviceName,
125         pdwEventCode, pwszEventName);
126     return E_NOTIMPL;
127 }
128
129 static HRESULT WINAPI stillimagew_RegisterLaunchApplication(IStillImageW *iface, LPWSTR pwszAppName,
130                                                             LPWSTR pwszCommandLine)
131 {
132     static const WCHAR format[] = {'%','s',' ','%','s',0};
133     static const WCHAR commandLineSuffix[] = {
134         '/','S','t','i','D','e','v','i','c','e',':','%','1',' ',
135         '/','S','t','i','E','v','e','n','t',':','%','2',0};
136     HKEY registeredAppsKey = NULL;
137     DWORD ret;
138     HRESULT hr = S_OK;
139     stillimage *This = impl_from_StillImageW(iface);
140
141     TRACE("(%p, %s, %s)\n", This, debugstr_w(pwszAppName), debugstr_w(pwszCommandLine));
142
143     ret = RegCreateKeyW(HKEY_LOCAL_MACHINE, registeredAppsLaunchPath, &registeredAppsKey);
144     if (ret == ERROR_SUCCESS)
145     {
146         WCHAR *value = HeapAlloc(GetProcessHeap(), 0,
147             (lstrlenW(pwszCommandLine) + 1 + lstrlenW(commandLineSuffix) + 1) * sizeof(WCHAR));
148         if (value)
149         {
150             sprintfW(value, format, pwszCommandLine, commandLineSuffix);
151             ret = RegSetValueExW(registeredAppsKey, pwszAppName, 0,
152                 REG_SZ, (BYTE*)value, (lstrlenW(value)+1)*sizeof(WCHAR));
153             if (ret != ERROR_SUCCESS)
154                 hr = HRESULT_FROM_WIN32(ret);
155             HeapFree(GetProcessHeap(), 0, value);
156         }
157         else
158             hr = E_OUTOFMEMORY;
159         RegCloseKey(registeredAppsKey);
160     }
161     else
162         hr = HRESULT_FROM_WIN32(ret);
163     return hr;
164 }
165
166 static HRESULT WINAPI stillimagew_UnregisterLaunchApplication(IStillImageW *iface, LPWSTR pwszAppName)
167 {
168     stillimage *This = impl_from_StillImageW(iface);
169     HKEY registeredAppsKey = NULL;
170     DWORD ret;
171     HRESULT hr = S_OK;
172
173     TRACE("(%p, %s)\n", This, debugstr_w(pwszAppName));
174
175     ret = RegCreateKeyW(HKEY_LOCAL_MACHINE, registeredAppsLaunchPath, &registeredAppsKey);
176     if (ret == ERROR_SUCCESS)
177     {
178         ret = RegDeleteValueW(registeredAppsKey, pwszAppName);
179         if (ret != ERROR_SUCCESS)
180             hr = HRESULT_FROM_WIN32(ret);
181         RegCloseKey(registeredAppsKey);
182     }
183     else
184         hr = HRESULT_FROM_WIN32(ret);
185     return hr;
186 }
187
188 static HRESULT WINAPI stillimagew_EnableHwNotifications(IStillImageW *iface, LPCWSTR pwszDeviceName,
189                                                         BOOL bNewState)
190 {
191     stillimage *This = impl_from_StillImageW(iface);
192     FIXME("(%p, %s, %u): stub\n", This, debugstr_w(pwszDeviceName), bNewState);
193     return E_NOTIMPL;
194 }
195
196 static HRESULT WINAPI stillimagew_GetHwNotificationState(IStillImageW *iface, LPCWSTR pwszDeviceName,
197                                                          BOOL *pbCurrentState)
198 {
199     stillimage *This = impl_from_StillImageW(iface);
200     FIXME("(%p, %s, %p): stub\n", This, debugstr_w(pwszDeviceName), pbCurrentState);
201     return E_NOTIMPL;
202 }
203
204 static HRESULT WINAPI stillimagew_RefreshDeviceBus(IStillImageW *iface, LPCWSTR pwszDeviceName)
205 {
206     stillimage *This = impl_from_StillImageW(iface);
207     FIXME("(%p, %s): stub\n", This, debugstr_w(pwszDeviceName));
208     return E_NOTIMPL;
209 }
210
211 static HRESULT WINAPI stillimagew_LaunchApplicationForDevice(IStillImageW *iface, LPWSTR pwszDeviceName,
212                                                              LPWSTR pwszAppName, LPSTINOTIFY pStiNotify)
213 {
214     stillimage *This = impl_from_StillImageW(iface);
215     FIXME("(%p, %s, %s, %p): stub\n", This, debugstr_w(pwszDeviceName), debugstr_w(pwszAppName),
216         pStiNotify);
217     return E_NOTIMPL;
218 }
219
220 static HRESULT WINAPI stillimagew_SetupDeviceParameters(IStillImageW *iface, PSTI_DEVICE_INFORMATIONW pDevInfo)
221 {
222     stillimage *This = impl_from_StillImageW(iface);
223     FIXME("(%p, %p): stub\n", This, pDevInfo);
224     return E_NOTIMPL;
225 }
226
227 static HRESULT WINAPI stillimagew_WriteToErrorLog(IStillImageW *iface, DWORD dwMessageType, LPCWSTR pszMessage)
228 {
229     stillimage *This = impl_from_StillImageW(iface);
230     FIXME("(%p, %u, %s): stub\n", This, dwMessageType, debugstr_w(pszMessage));
231     return E_NOTIMPL;
232 }
233
234 static const struct IStillImageWVtbl stillimagew_vtbl =
235 {
236     stillimagew_QueryInterface,
237     stillimagew_AddRef,
238     stillimagew_Release,
239     stillimagew_Initialize,
240     stillimagew_GetDeviceList,
241     stillimagew_GetDeviceInfo,
242     stillimagew_CreateDevice,
243     stillimagew_GetDeviceValue,
244     stillimagew_SetDeviceValue,
245     stillimagew_GetSTILaunchInformation,
246     stillimagew_RegisterLaunchApplication,
247     stillimagew_UnregisterLaunchApplication,
248     stillimagew_EnableHwNotifications,
249     stillimagew_GetHwNotificationState,
250     stillimagew_RefreshDeviceBus,
251     stillimagew_LaunchApplicationForDevice,
252     stillimagew_SetupDeviceParameters,
253     stillimagew_WriteToErrorLog
254 };
255
256 static inline stillimage *impl_from_InternalUnknown(IUnknown *iface)
257 {
258     return (stillimage *)((char*)iface - FIELD_OFFSET(stillimage, lpInternalUnkVtbl));
259 }
260
261 static HRESULT WINAPI Internal_QueryInterface(IUnknown *iface, REFIID riid, void **ppvObject)
262 {
263     stillimage *This = impl_from_InternalUnknown(iface);
264
265     TRACE("(%p %s %p)\n", This, debugstr_guid(riid), ppvObject);
266
267     if (IsEqualGUID(riid, &IID_IUnknown))
268         *ppvObject = iface;
269     else if (IsEqualGUID(riid, &IID_IStillImageW))
270         *ppvObject = &This->lpVtbl;
271     else
272     {
273         if (IsEqualGUID(riid, &IID_IStillImageA))
274             FIXME("interface IStillImageA is unsupported on Windows Vista too, please report if it's needed\n");
275         else
276             FIXME("interface %s not implemented\n", debugstr_guid(riid));
277         *ppvObject = NULL;
278         return E_NOINTERFACE;
279     }
280
281     IUnknown_AddRef((IUnknown*) *ppvObject);
282     return S_OK;
283 }
284
285 static ULONG WINAPI Internal_AddRef(IUnknown *iface)
286 {
287     stillimage *This = impl_from_InternalUnknown(iface);
288     return InterlockedIncrement(&This->ref);
289 }
290
291 static ULONG WINAPI Internal_Release(IUnknown *iface)
292 {
293     ULONG ref;
294     stillimage *This = impl_from_InternalUnknown(iface);
295
296     ref = InterlockedDecrement(&This->ref);
297     if (ref == 0)
298         HeapFree(GetProcessHeap(), 0, This);
299     return ref;
300 }
301
302 static const struct IUnknownVtbl internal_unk_vtbl =
303 {
304     Internal_QueryInterface,
305     Internal_AddRef,
306     Internal_Release
307 };
308
309 /******************************************************************************
310  *           StiCreateInstanceA   (STI.@)
311  */
312 HRESULT WINAPI StiCreateInstanceA(HINSTANCE hinst, DWORD dwVer, PSTIA *ppSti, LPUNKNOWN pUnkOuter)
313 {
314     FIXME("(%p, %u, %p, %p): stub, unimplemented on Windows Vista too, please report if it's needed\n", hinst, dwVer, ppSti, pUnkOuter);
315     return STG_E_UNIMPLEMENTEDFUNCTION;
316 }
317
318 /******************************************************************************
319  *           StiCreateInstanceW   (STI.@)
320  */
321 HRESULT WINAPI StiCreateInstanceW(HINSTANCE hinst, DWORD dwVer, PSTIW *ppSti, LPUNKNOWN pUnkOuter)
322 {
323     stillimage *This;
324     HRESULT hr;
325
326     TRACE("(%p, %u, %p, %p)\n", hinst, dwVer, ppSti, pUnkOuter);
327
328     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(stillimage));
329     if (This)
330     {
331         This->lpVtbl = &stillimagew_vtbl;
332         This->lpInternalUnkVtbl = &internal_unk_vtbl;
333         if (pUnkOuter)
334             This->pUnkOuter = pUnkOuter;
335         else
336             This->pUnkOuter = (IUnknown*) &This->lpInternalUnkVtbl;
337         This->ref = 1;
338
339         hr = IStillImage_Initialize((IStillImageW*) &This->lpVtbl, hinst, dwVer);
340         if (SUCCEEDED(hr))
341         {
342             if (pUnkOuter)
343                 *ppSti = (IStillImageW*) &This->lpInternalUnkVtbl;
344             else
345                 *ppSti = (IStillImageW*) &This->lpVtbl;
346         }
347     }
348     else
349         hr = E_OUTOFMEMORY;
350
351     return hr;
352 }