summaryrefslogtreecommitdiffstats
path: root/ceee/common/com_utils.h
blob: 30df2ae282c1068edbd1061ea258c080cc2cafe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Utilities for COM objects, error codes etc.

#ifndef CEEE_COMMON_COM_UTILS_H_
#define CEEE_COMMON_COM_UTILS_H_

#include <windows.h>
#include <wtypes.h>

#include <ostream>

namespace com {

// Returns the provided hr if it is an error, otherwise default_error.
inline HRESULT AlwaysError(HRESULT hr, HRESULT default_error) {
  if (FAILED(hr)) {
    return hr;
  } else {
    return default_error;
  }
}

// Returns the provided hr if it is an error, otherwise E_FAIL.
inline HRESULT AlwaysError(HRESULT hr) {
  return AlwaysError(hr, E_FAIL);
}

// Converts a Win32 result code to a HRESULT. If the 'win32_code'
// does not indicate an error, it returns 'default_error'.
inline HRESULT AlwaysErrorFromWin32(DWORD win32_code,
                                    HRESULT default_error) {
  HRESULT hr = HRESULT_FROM_WIN32(win32_code);
  return AlwaysError(hr, default_error);
}

// Converts a Win32 result code to a HRESULT. If the 'win32_code'
// does not indicate an error, it returns E_FAIL
inline HRESULT AlwaysErrorFromWin32(DWORD win32_code) {
  return AlwaysErrorFromWin32(win32_code, E_FAIL);
}

// Returns the HRESULT equivalent of GetLastError(), unless it does not
// represent an error in which case it returns 'default_error'.
inline HRESULT AlwaysErrorFromLastError(HRESULT default_error) {
  return AlwaysErrorFromWin32(::GetLastError(), default_error);
}

// Returns the HRESULT equivalent of GetLastError(), unless it does not
// represent an error in which case it returns E_FAIL.
inline HRESULT AlwaysErrorFromLastError() {
  return AlwaysErrorFromLastError(E_FAIL);
}

// Return the specified string, or the empty string if it is NULL.
inline const wchar_t* ToString(BSTR str) {
  return str ? str : L"";
}

// Logs HRESULTs verbosely, with the error code and human-readable error
// text if available.
class LogHr {
 public:
  explicit LogHr(HRESULT hr) : hr_(hr) {}
 private:
  HRESULT hr_;
  friend std::ostream& operator<<(std::ostream&, const LogHr&);
};

std::ostream& operator<<(std::ostream& os, const LogHr& hr);

// Logs Windows errors verbosely, with the error code and human-readable error
// text if available.
class LogWe {
 public:
  LogWe() : we_(::GetLastError()) {}
  explicit LogWe(DWORD we) : we_(we) {}
 private:
  DWORD we_;
  friend std::ostream& operator<<(std::ostream&, const LogWe&);
};

std::ostream& operator<<(std::ostream& os, const LogWe& we);

// Use to register a .reg file that needs %MODULE_PATH% and %MODULE_BASENAME%
// replaceables, but no %APPID%.
HRESULT ModuleRegistrationWithoutAppid(int reg_id, BOOL should_register);

// Converts GUID into text and places in the provided object. Returns true if
// succeeded. Logs possible error codes.
bool GuidToString(const GUID& id, std::wstring* guid_as_text);

}  // namespace com

#endif  // CEEE_COMMON_COM_UTILS_H_