summaryrefslogtreecommitdiffstats
path: root/ceee/common/com_utils.cc
blob: b251ca17592419632db08a542aa7c8af7ddb4e9d (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
// 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.

#include "ceee/common/com_utils.h"

#include <atlbase.h>
#include <shlwapi.h>

#include "base/string_util.h"

namespace com {

std::ostream& operator<<(std::ostream& os, const LogHr& hr) {
  // Looks up the human-readable system message for the HRESULT code
  // and since we're not passing any params to FormatMessage, we don't
  // want inserts expanded.
  const DWORD kFlags = FORMAT_MESSAGE_FROM_SYSTEM |
                       FORMAT_MESSAGE_IGNORE_INSERTS;
  char error_text[4096] = { '\0' };
  DWORD message_length = ::FormatMessageA(kFlags, 0, hr.hr_, 0, error_text,
                                          arraysize(error_text), NULL);
  std::string error(error_text);
  TrimString(error, kWhitespaceASCII, &error);

  return os << "[hr=0x" << std::hex << hr.hr_ << ", msg=" << error << "]";
}

std::ostream& operator<<(std::ostream& os, const LogWe& we) {
  // Looks up the human-readable system message for the Windows error code
  // and since we're not passing any params to FormatMessage, we don't
  // want inserts expanded.
  const DWORD kFlags = FORMAT_MESSAGE_FROM_SYSTEM |
                       FORMAT_MESSAGE_IGNORE_INSERTS;
  char error_text[4096] = { '\0' };
  DWORD message_length = ::FormatMessageA(kFlags, 0, we.we_, 0, error_text,
                                          arraysize(error_text), NULL);
  std::string error(error_text);
  TrimString(error, kWhitespaceASCII, &error);

  return os << "[we=" << we.we_ << ", msg=" << error << "]";
}

// Seam so unit tests can mock out the side effects of this function.
HRESULT UpdateRegistryFromResourceImpl(int reg_id, BOOL should_register,
                                       _ATL_REGMAP_ENTRY* entries) {
  return _pAtlModule->UpdateRegistryFromResource(
      reg_id, should_register, entries);
}

HRESULT ModuleRegistrationWithoutAppid(int reg_id, BOOL should_register) {
  // Get our module path.
  wchar_t module_path[MAX_PATH] = { 0 };
  if (0 == ::GetModuleFileName(_AtlBaseModule.GetModuleInstance(),
                               module_path, arraysize(module_path))) {
    NOTREACHED();
    return AlwaysErrorFromLastError();
  }

  // Copy the filename.
  std::wstring module_basename(::PathFindFileName(module_path));
  // And strip the filename.
  if (!::PathRemoveFileSpec(module_path)) {
    NOTREACHED();
    return AlwaysErrorFromLastError();
  }

  _ATL_REGMAP_ENTRY entries[] = {
      { L"MODULE_PATH", module_path },
      { L"MODULE_BASENAME", module_basename.c_str() },
      { NULL, NULL}
    };

  return UpdateRegistryFromResourceImpl(reg_id, should_register, entries);
}

}  // namespace com