include: Assorted spelling fixes.
[wine] / dlls / wer / main.c
1 /*
2  * Copyright 2010 Louis Lenders
3  * Copyright 2010 Detlef Riekenberg
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 "config.h"
21
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "werapi.h"
28 #include "wine/list.h"
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(wer);
33
34 typedef struct {
35     struct list entry;
36     WER_REPORT_INFORMATION info;
37     WER_REPORT_TYPE reporttype;
38     WCHAR eventtype[1];
39 } report_t;
40
41
42 static CRITICAL_SECTION report_table_cs;
43 static CRITICAL_SECTION_DEBUG report_table_cs_debug =
44 {
45     0, 0, &report_table_cs,
46     { &report_table_cs_debug.ProcessLocksList, &report_table_cs_debug.ProcessLocksList },
47       0, 0, { (DWORD_PTR)(__FILE__ ": report_table_cs") }
48 };
49 static CRITICAL_SECTION report_table_cs = { &report_table_cs_debug, -1, 0, 0, 0, 0 };
50
51 static struct list report_table = LIST_INIT(report_table);
52
53 static WCHAR regpath_exclude[] = {'S','o','f','t','w','a','r','e','\\',
54                                   'M','i','c','r','o','s','o','f','t','\\',
55                                   'W','i','n','d','o','w','s',' ','E','r','r','o','r',' ','R','e','p','o','r','t','i','n','g','\\',
56                                   'E','x','c','l','u','d','e','d','A','p','p','l','i','c','a','t','i','o','n','s',0};
57
58 /***********************************************************************
59  * Memory allocation helper
60  */
61
62 static inline void * __WINE_ALLOC_SIZE(1) heap_alloc_zero(size_t len)
63 {
64     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
65 }
66
67 static inline BOOL heap_free(void *mem)
68 {
69     return HeapFree(GetProcessHeap(), 0, mem);
70 }
71
72 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
73 {
74     TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
75
76     switch (fdwReason)
77     {
78         case DLL_WINE_PREATTACH:
79             return FALSE;    /* prefer native version */
80         case DLL_PROCESS_ATTACH:
81             DisableThreadLibraryCalls(hinstDLL);
82             break;
83         case DLL_PROCESS_DETACH:
84             break;
85     }
86
87     return TRUE;
88 }
89
90 /***********************************************************************
91  * WerAddExcludedApplication (wer.@)
92  *
93  * Add an application to the user specific or the system wide exclusion list
94  *
95  * PARAMS
96  *  exeName  [i] The application name
97  *  allUsers [i] for all users (TRUE) or for the current user (FALSE)
98  *
99  * RETURNS
100  *  Success: S_OK
101  *  Failure: A HRESULT error code
102  *
103  */
104 HRESULT WINAPI WerAddExcludedApplication(PCWSTR exeName, BOOL allUsers)
105 {
106     HKEY hkey;
107     DWORD value = 1;
108     LPWSTR bs;
109
110     TRACE("(%s, %d)\n",debugstr_w(exeName), allUsers);
111     if (!exeName || !exeName[0])
112         return E_INVALIDARG;
113
114     bs = strrchrW(exeName, '\\');
115     if (bs) {
116         bs++;   /* skip the backslash */
117         if (!bs[0]) {
118             return E_INVALIDARG;
119         }
120     } else
121         bs = (LPWSTR) exeName;
122
123     if (!RegCreateKeyW(allUsers ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, regpath_exclude, &hkey)) {
124         RegSetValueExW(hkey, bs, 0, REG_DWORD, (LPBYTE)&value, sizeof(DWORD));
125         RegCloseKey(hkey);
126         return S_OK;
127     }
128     return E_ACCESSDENIED;
129 }
130
131 /***********************************************************************
132  * WerRemoveExcludedApplication (wer.@)
133  *
134  * remove an application from the exclusion list
135  *
136  * PARAMS
137  *  exeName  [i] The application name
138  *  allUsers [i] for all users (TRUE) or for the current user (FALSE)
139  *
140  * RETURNS
141  *  Success: S_OK
142  *  Failure: A HRESULT error code
143  *
144  */
145 HRESULT WINAPI WerRemoveExcludedApplication(PCWSTR exeName, BOOL allUsers)
146 {
147     HKEY hkey;
148     LPWSTR bs;
149     LONG lres;
150
151     TRACE("(%s, %d)\n",debugstr_w(exeName), allUsers);
152     if (!exeName || !exeName[0])
153         return E_INVALIDARG;
154
155     bs = strrchrW(exeName, '\\');
156     if (bs) {
157         bs++;   /* skip the backslash */
158         if (!bs[0]) {
159             return E_INVALIDARG;
160         }
161     } else
162         bs = (LPWSTR) exeName;
163
164     if (!RegCreateKeyW(allUsers ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, regpath_exclude, &hkey)) {
165         lres = RegDeleteValueW(hkey, bs);
166         RegCloseKey(hkey);
167         return lres ? __HRESULT_FROM_WIN32(ERROR_ENVVAR_NOT_FOUND) : S_OK;
168     }
169     return E_ACCESSDENIED;
170 }
171
172 /***********************************************************************
173  * WerReportAddDump (wer.@)
174  *
175  * Add a dump of dumpType to hReportHandle.
176  *
177  * PARAMS
178  *  hReportHandle      [i] error reporting handle to add the dump
179  *  hProcess           [i] handle to the regarding process
180  *  hThread            [o] handle to the regarding thread
181  *  dumpType           [i] type of the dump
182  *  pExceptionParam    [o] pointer to a WER_EXCEPTION_INFORMATION
183  *  pDumpCustomOptions [o] pointer to a WER_DUMP_CUSTOM_OPTIONS
184  *  dwFlags            [i] flag to control the heap dump
185  *
186  * RETURNS
187  *  Success: S_OK
188  *  Failure: A HRESULT error code
189  *
190  */
191 HRESULT WINAPI WerReportAddDump(HREPORT hReportHandle, HANDLE hProcess, HANDLE hThread,
192                                 WER_DUMP_TYPE dumpType, PWER_EXCEPTION_INFORMATION pExceptionParam,
193                                 PWER_DUMP_CUSTOM_OPTIONS pDumpCustomOptions, DWORD dwFlags)
194 {
195     FIXME("(%p, %p, %p, %d, %p, %p, %u) :stub\n", hReportHandle, hProcess, hThread, dumpType,
196           pExceptionParam, pDumpCustomOptions, dwFlags);
197
198     return E_NOTIMPL;
199 }
200
201 /***********************************************************************
202  * WerReportAddFile (wer.@)
203  *
204  * Add File to a error report handle.
205  *
206  * PARAMS
207  *  hreport [i] error reporting handle to add the file
208  *  path    [i] path to the file to add
209  *  type    [i] type of the file to add
210  *  flags   [i] flags for the file
211  *
212  * RETURNS
213  *  Success: S_OK
214  *  Failure: A HRESULT error code
215  *
216  */
217 HRESULT WINAPI WerReportAddFile(HREPORT hreport, PCWSTR path, WER_FILE_TYPE type, DWORD flags)
218 {
219     FIXME("(%p, %s, %d, 0x%x) :stub\n", hreport, debugstr_w(path), type, flags);
220
221     return S_OK;
222 }
223
224 /***********************************************************************
225  * WerReportCloseHandle (wer.@)
226  *
227  * Close an error reporting handle and free associated resources
228  *
229  * PARAMS
230  *  hreport [i] error reporting handle to close
231  *
232  * RETURNS
233  *  Success: S_OK
234  *  Failure: A HRESULT error code
235  *
236  */
237 HRESULT WINAPI WerReportCloseHandle(HREPORT hreport)
238 {
239     report_t * report = (report_t *) hreport;
240     report_t * cursor;
241     BOOL found = FALSE;
242
243     TRACE("(%p)\n", hreport);
244     EnterCriticalSection(&report_table_cs);
245     if (report) {
246         LIST_FOR_EACH_ENTRY(cursor, &report_table, report_t, entry)
247         {
248             if (cursor == report) {
249                 found = TRUE;
250                 list_remove(&report->entry);
251                 break;
252             }
253         }
254     }
255     LeaveCriticalSection(&report_table_cs);
256     if (!found)
257         return E_INVALIDARG;
258
259     heap_free(report);
260
261     return S_OK;
262 }
263
264 /***********************************************************************
265  * WerReportCreate (wer.@)
266  *
267  * Create an error report in memory and return a related HANDLE
268  *
269  * PARAMS
270  *  eventtype  [i] a name for the event type
271  *  reporttype [i] what type of report should be created
272  *  reportinfo [i] NULL or a ptr to a struct with some detailed information
273  *  phandle    [o] ptr, where the resulting handle should be saved
274  *
275  * RETURNS
276  *  Success: S_OK
277  *  Failure: A HRESULT error code
278  *
279  * NOTES
280  *  The event type must be registered at microsoft. Predefined types are
281  *  "APPCRASH" as the default on Windows, "Crash32" and "Crash64"
282  *
283  */
284 HRESULT WINAPI WerReportCreate(PCWSTR eventtype, WER_REPORT_TYPE reporttype, PWER_REPORT_INFORMATION reportinfo, HREPORT *phandle)
285 {
286     report_t *report;
287
288     TRACE("(%s, %d, %p, %p)\n", debugstr_w(eventtype), reporttype, reportinfo, phandle);
289     if (reportinfo) {
290         TRACE(".wzFriendlyEventName: %s\n", debugstr_w(reportinfo->wzFriendlyEventName));
291         TRACE(".wzApplicationName: %s\n", debugstr_w(reportinfo->wzApplicationName));
292     }
293
294     if (phandle)  *phandle = NULL;
295     if (!eventtype || !eventtype[0] || !phandle) {
296         return E_INVALIDARG;
297     }
298
299     report = heap_alloc_zero(FIELD_OFFSET(report_t, eventtype[lstrlenW(eventtype) + 1]));
300     if (!report)
301         return __HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
302
303     lstrcpyW(report->eventtype, eventtype);
304     report->reporttype = reporttype;
305
306     if (reportinfo) {
307         report->info = *reportinfo;
308     } else {
309         FIXME("build report information from scratch for %p\n", report);
310     }
311
312     EnterCriticalSection(&report_table_cs);
313     list_add_head(&report_table, &report->entry);
314     LeaveCriticalSection(&report_table_cs);
315
316     *phandle = report;
317     TRACE("=> %p\n", report);
318     return S_OK;
319 }
320
321 /***********************************************************************
322  * WerReportSetParameter (wer.@)
323  *
324  * Set one of 10 parameter / value pairs for a report handle
325  *
326  * PARAMS
327  *  hreport [i] error reporting handle to add the parameter
328  *  id      [i] parameter to set (WER_P0 up to WER_P9)
329  *  name    [i] optional name of the parameter
330  *  value   [i] value of the parameter
331  *
332  * RETURNS
333  *  Success: S_OK
334  *  Failure: A HRESULT error code
335  *
336  */
337 HRESULT WINAPI WerReportSetParameter(HREPORT hreport, DWORD id, PCWSTR name, PCWSTR value)
338 {
339     FIXME("(%p, %d, %s, %s) :stub\n", hreport, id, debugstr_w(name), debugstr_w(value));
340
341     return S_OK;
342 }
343
344 /***********************************************************************
345  * WerReportSubmit (wer.@)
346  *
347  * Ask the user for permission and send the error report
348  * then kill or restart the application, when requested
349  *
350  * PARAMS
351  *  hreport [i] error reporting handle to send
352  *  consent [i] current transmit permission
353  *  flags   [i] flag to select dialog, transmission snd restart options
354  *  presult [o] ptr, where the transmission result should be saved
355  *
356  * RETURNS
357  *  Success: S_OK
358  *  Failure: A HRESULT error code
359  *
360  */
361 HRESULT WINAPI WerReportSubmit(HREPORT hreport, WER_CONSENT consent, DWORD flags, PWER_SUBMIT_RESULT presult)
362 {
363     FIXME("(%p, %d, 0x%x, %p) :stub\n", hreport, consent, flags, presult);
364
365     if(!presult)
366         return E_INVALIDARG;
367
368     *presult = WerDisabled;
369     return S_OK;
370 }
371
372 /***********************************************************************
373  * WerReportSetUIOption (wer.@)
374  */
375 HRESULT WINAPI WerReportSetUIOption(HREPORT hreport, WER_REPORT_UI uitype, PCWSTR value)
376 {
377     FIXME("(%p, %d, %s) :stub\n", hreport, uitype, debugstr_w(value));
378     return E_NOTIMPL;
379 }