main.c 9.01 KB
Newer Older
1 2
/*
 * Copyright 2010 Louis Lenders
3
 * Copyright 2010 Detlef Riekenberg
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"

#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
26
#include "winreg.h"
27
#include "werapi.h"
28
#include "wine/list.h"
29
#include "wine/unicode.h"
30 31 32 33
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(wer);

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
typedef struct {
    struct list entry;
    WER_REPORT_INFORMATION info;
    WER_REPORT_TYPE reporttype;
    WCHAR eventtype[1];
} report_t;


static CRITICAL_SECTION report_table_cs;
static CRITICAL_SECTION_DEBUG report_table_cs_debug =
{
    0, 0, &report_table_cs,
    { &report_table_cs_debug.ProcessLocksList, &report_table_cs_debug.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": report_table_cs") }
};
static CRITICAL_SECTION report_table_cs = { &report_table_cs_debug, -1, 0, 0, 0, 0 };

static struct list report_table = LIST_INIT(report_table);

53 54 55 56 57
static WCHAR regpath_exclude[] = {'S','o','f','t','w','a','r','e','\\',
                                  'M','i','c','r','o','s','o','f','t','\\',
                                  'W','i','n','d','o','w','s',' ','E','r','r','o','r',' ','R','e','p','o','r','t','i','n','g','\\',
                                  'E','x','c','l','u','d','e','d','A','p','p','l','i','c','a','t','i','o','n','s',0};

58 59 60 61 62 63 64 65 66 67 68 69 70 71
/***********************************************************************
 * Memory alloccation helper
 */

static inline void * __WINE_ALLOC_SIZE(1) heap_alloc_zero(size_t len)
{
    return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
}

static inline BOOL heap_free(void *mem)
{
    return HeapFree(GetProcessHeap(), 0, mem);
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);

    switch (fdwReason)
    {
        case DLL_WINE_PREATTACH:
            return FALSE;    /* prefer native version */
        case DLL_PROCESS_ATTACH:
            DisableThreadLibraryCalls(hinstDLL);
            break;
        case DLL_PROCESS_DETACH:
            break;
    }

    return TRUE;
}
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/***********************************************************************
 * WerAddExcludedApplication (wer.@)
 *
 * Add an application to the user specific or the system wide exclusion list
 *
 * PARAMS
 *  exeName  [i] The application name
 *  allUsers [i] for all users (TRUE) or for the current user (FALSE)
 *
 * RETURNS
 *  Success: S_OK
 *  Faulure: A HRESULT error code
 *
 */
HRESULT WINAPI WerAddExcludedApplication(PCWSTR exeName, BOOL allUsers)
{
    HKEY hkey;
    DWORD value = 1;
    LPWSTR bs;

    TRACE("(%s, %d)\n",debugstr_w(exeName), allUsers);
    if (!exeName || !exeName[0])
        return E_INVALIDARG;

    bs = strrchrW(exeName, '\\');
    if (bs) {
        bs++;   /* skip the backslash */
        if (!bs[0]) {
            return E_INVALIDARG;
        }
    } else
        bs = (LPWSTR) exeName;

    if (!RegCreateKeyW(allUsers ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, regpath_exclude, &hkey)) {
        RegSetValueExW(hkey, bs, 0, REG_DWORD, (LPBYTE)&value, sizeof(DWORD));
        RegCloseKey(hkey);
        return S_OK;
    }
    return E_ACCESSDENIED;
}

131 132 133 134 135 136 137 138 139
/***********************************************************************
 * WerRemoveExcludedApplication (wer.@)
 *
 * remove an application from the exclusion list
 *
 * PARAMS
 *  exeName  [i] The application name
 *  allUsers [i] for all users (TRUE) or for the current user (FALSE)
 *
140 141 142
 * RETURNS
 *  Success: S_OK
 *  Faulure: A HRESULT error code
143 144 145 146
 *
 */
HRESULT WINAPI WerRemoveExcludedApplication(PCWSTR exeName, BOOL allUsers)
{
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    HKEY hkey;
    LPWSTR bs;
    LONG lres;

    TRACE("(%s, %d)\n",debugstr_w(exeName), allUsers);
    if (!exeName || !exeName[0])
        return E_INVALIDARG;

    bs = strrchrW(exeName, '\\');
    if (bs) {
        bs++;   /* skip the backslash */
        if (!bs[0]) {
            return E_INVALIDARG;
        }
    } else
        bs = (LPWSTR) exeName;

    if (!RegCreateKeyW(allUsers ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, regpath_exclude, &hkey)) {
        lres = RegDeleteValueW(hkey, bs);
        RegCloseKey(hkey);
        return lres ? __HRESULT_FROM_WIN32(ERROR_ENVVAR_NOT_FOUND) : S_OK;
    }
    return E_ACCESSDENIED;
170
}
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

/***********************************************************************
 * WerReportCloseHandle (wer.@)
 *
 * Close an error reporting handle and free associated resources
 *
 * PARAMS
 *  hreport [i] error reporting handle to close
 *
 * RETURNS
 *  Success: S_OK
 *  Failure: A HRESULT error code
 *
 */
HRESULT WINAPI WerReportCloseHandle(HREPORT hreport)
{
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    report_t * report = (report_t *) hreport;
    report_t * cursor;
    BOOL found = FALSE;

    TRACE("(%p)\n", hreport);
    EnterCriticalSection(&report_table_cs);
    if (report) {
        LIST_FOR_EACH_ENTRY(cursor, &report_table, report_t, entry)
        {
            if (cursor == report) {
                found = TRUE;
                list_remove(&report->entry);
                break;
            }
        }
    }
    LeaveCriticalSection(&report_table_cs);
    if (!found)
        return E_INVALIDARG;
206

207 208 209
    heap_free(report);

    return S_OK;
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
}

/***********************************************************************
 * WerReportCreate (wer.@)
 *
 * Create an error report in memory and return a related HANDLE
 *
 * PARAMS
 *  eventtype  [i] a name for the event type
 *  reporttype [i] what type of report should be created
 *  reportinfo [i] NULL or a ptr to a struct with some detailed information
 *  phandle    [o] ptr, where the resulting handle should be saved
 *
 * RETURNS
 *  Success: S_OK
 *  Failure: A HRESULT error code
 *
 * NOTES
 *  The event type must be registered at microsoft. Predefined types are
 *  "APPCRASH" as the default on Windows, "Crash32" and "Crash64"
 *
 */
HRESULT WINAPI WerReportCreate(PCWSTR eventtype, WER_REPORT_TYPE reporttype, PWER_REPORT_INFORMATION reportinfo, HREPORT *phandle)
{
234 235
    report_t *report;
    DWORD len;
236

237
    TRACE("(%s, %d, %p, %p)\n", debugstr_w(eventtype), reporttype, reportinfo, phandle);
238 239 240 241 242 243 244 245 246 247
    if (reportinfo) {
        TRACE(".wzFriendlyEventName: %s\n", debugstr_w(reportinfo->wzFriendlyEventName));
        TRACE(".wzApplicationName: %s\n", debugstr_w(reportinfo->wzApplicationName));
    }

    if (phandle)  *phandle = NULL;
    if (!eventtype || !eventtype[0] || !phandle) {
        return E_INVALIDARG;
    }

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    len = lstrlenW(eventtype) + 1;

    report = heap_alloc_zero(len * sizeof(WCHAR) + sizeof(report_t));
    if (!report)
        return __HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);

    lstrcpyW(report->eventtype, eventtype);
    report->reporttype = reporttype;

    if (reportinfo) {
        report->info = *reportinfo;
    } else {
        FIXME("build report information from scratch for %p\n", report);
    }

    EnterCriticalSection(&report_table_cs);
    list_add_head(&report_table, &report->entry);
    LeaveCriticalSection(&report_table_cs);

    *phandle = report;
    TRACE("=> %p\n", report);
    return S_OK;
270
}
271 272 273 274 275 276 277 278

/***********************************************************************
 * WerReportSetParameter (wer.@)
 *
 * Set one of 10 parameter / value pairs for a report handle
 *
 * PARAMS
 *  hreport [i] error reporting handle to add the parameter
279
 *  id      [i] parameter to set (WER_P0 up to WER_P9)
280 281 282 283 284 285 286 287 288 289 290 291 292 293
 *  name    [i] optional name of the parameter
 *  value   [i] value of the parameter
 *
 * RETURNS
 *  Success: S_OK
 *  Failure: A HRESULT error code
 *
 */
HRESULT WINAPI WerReportSetParameter(HREPORT hreport, DWORD id, PCWSTR name, PCWSTR value)
{
    FIXME("(%p, %d, %s, %s) :stub\n", hreport, id, debugstr_w(name), debugstr_w(value));

    return E_NOTIMPL;
}
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

/***********************************************************************
 * WerReportSubmit (wer.@)
 *
 * Ask the user for permission and send the error report
 * then kill or restart the application, when requested
 *
 * PARAMS
 *  hreport [i] error reporting handle to send
 *  consent [i] current transmit permission
 *  flags   [i] flag to select dialog, transmission snd restart options
 *  presult [o] ptr, where the transmission result should be saved
 *
 * RETURNS
 *  Success: S_OK
 *  Failure: A HRESULT error code
 *
 */
HRESULT WINAPI WerReportSubmit(HREPORT hreport, WER_CONSENT consent, DWORD flags, PWER_SUBMIT_RESULT presult)
{
    FIXME("(%p, %d, 0x%x, %p) :stub\n", hreport, consent, flags, presult);

    if(!presult)
        return E_INVALIDARG;

    *presult = WerDisabled;
    return E_NOTIMPL;
}