1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cloud_print/common/win/cloud_print_utils.h"
9 #include "base/win/registry.h"
11 namespace cloud_print {
15 // Google Update related constants.
16 const wchar_t kClientStateKey[] = L"SOFTWARE\\Google\\Update\\ClientState\\";
17 const wchar_t* kUsageKey = L"dr";
21 HRESULT GetLastHResult() {
22 DWORD error_code = GetLastError();
23 return error_code ? HRESULT_FROM_WIN32(error_code) : E_FAIL;
26 string16 LoadLocalString(DWORD id) {
27 static wchar_t dummy = L'\0';
28 HMODULE module = NULL;
29 ::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
30 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, &dummy, &module);
31 LPCWSTR buffer = NULL;
32 // If the last parameter is 0, LoadString assume that 3rd parameter type is
33 // LPCWSTR* and assign pointer to read-only memory with resource.
34 int count = ::LoadString(module, id, reinterpret_cast<LPWSTR>(&buffer), 0);
37 return string16(buffer, buffer + count);
40 string16 GetErrorMessage(HRESULT hr) {
42 ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
43 FORMAT_MESSAGE_ALLOCATE_BUFFER,
44 0, hr, 0, reinterpret_cast<LPWSTR>(&buffer), 0, NULL);
45 string16 result(buffer);
50 void SetGoogleUpdateUsage(const string16& product_id) {
51 // Set appropriate key to 1 to let Omaha record usage.
52 base::win::RegKey key;
53 if (key.Create(HKEY_CURRENT_USER,
54 (kClientStateKey + product_id).c_str(),
55 KEY_SET_VALUE) != ERROR_SUCCESS ||
56 key.WriteValue(kUsageKey, L"1") != ERROR_SUCCESS) {
57 LOG(ERROR) << "Unable to set usage key";
61 } // namespace cloud_print