OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cloud_print/virtual_driver/win/virtual_driver_helpers.h" | 5 #include "cloud_print/virtual_driver/win/virtual_driver_helpers.h" |
6 #include <windows.h> | 6 #include <windows.h> |
7 #include <winspool.h> | 7 #include <winspool.h> |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "cloud_print/virtual_driver/win/virtual_driver_consts.h" | 9 #include "base/logging.h" |
| 10 #include "base/win/windows_version.h" |
| 11 #include "cloud_print/virtual_driver/win/virtual_driver_common_resources.h" |
10 | 12 |
11 namespace cloud_print { | 13 namespace cloud_print { |
12 | 14 |
13 const size_t kMaxMessageLen = 100; | 15 const size_t kMaxMessageLen = 100; |
14 | 16 |
15 void DisplayWindowsMessage(HWND hwnd, HRESULT message_id) { | 17 void DisplayWindowsMessage(HWND hwnd, |
| 18 HRESULT message_id, |
| 19 const string16 &caption) { |
16 wchar_t message_text[kMaxMessageLen + 1] = L""; | 20 wchar_t message_text[kMaxMessageLen + 1] = L""; |
17 | 21 |
18 ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, | 22 ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, |
19 NULL, | 23 NULL, |
20 message_id, | 24 message_id, |
21 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | 25 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
22 message_text, | 26 message_text, |
23 kMaxMessageLen, | 27 kMaxMessageLen, |
24 NULL); | 28 NULL); |
25 ::MessageBox(hwnd, message_text, kVirtualDriverName, MB_OK); | 29 ::MessageBox(hwnd, message_text, caption.c_str(), MB_OK); |
26 } | 30 } |
27 | 31 |
28 HRESULT GetLastHResult() { | 32 HRESULT GetLastHResult() { |
29 DWORD error_code = GetLastError(); | 33 DWORD error_code = GetLastError(); |
30 return HRESULT_FROM_WIN32(error_code); | 34 return HRESULT_FROM_WIN32(error_code); |
31 } | 35 } |
32 | 36 |
| 37 void GetPortMonitorDllName(FilePath* path) { |
| 38 wchar_t dll_name[MAX_PATH]; |
| 39 if (IsSystem64Bit()) { |
| 40 LoadLocalString(IDS_PORT_MON_DLL_NAME_64, |
| 41 dll_name, |
| 42 MAX_PATH); |
| 43 } else { |
| 44 LoadLocalString(IDS_PORT_MON_DLL_NAME_32, |
| 45 dll_name, |
| 46 MAX_PATH); |
| 47 } |
| 48 *path = FilePath(dll_name); |
| 49 } |
| 50 |
33 HRESULT GetPrinterDriverDir(FilePath* path) { | 51 HRESULT GetPrinterDriverDir(FilePath* path) { |
34 BYTE driver_dir_buffer[MAX_PATH * sizeof(wchar_t)]; | 52 BYTE driver_dir_buffer[MAX_PATH * sizeof(wchar_t)]; |
35 DWORD needed = 0; | 53 DWORD needed = 0; |
36 if (!GetPrinterDriverDirectory(NULL, | 54 if (!GetPrinterDriverDirectory(NULL, |
37 NULL, | 55 NULL, |
38 1, | 56 1, |
39 driver_dir_buffer, | 57 driver_dir_buffer, |
40 MAX_PATH * sizeof(wchar_t), | 58 MAX_PATH * sizeof(wchar_t), |
41 &needed)) { | 59 &needed)) { |
42 // We could try to allocate a larger buffer if needed > MAX_PATH | 60 // We could try to allocate a larger buffer if needed > MAX_PATH |
43 // but that really shouldn't happen. | 61 // but that really shouldn't happen. |
44 return cloud_print::GetLastHResult(); | 62 return cloud_print::GetLastHResult(); |
45 } | 63 } |
46 *path = FilePath(reinterpret_cast<wchar_t*>(driver_dir_buffer)); | 64 *path = FilePath(reinterpret_cast<wchar_t*>(driver_dir_buffer)); |
47 | 65 |
48 // The XPS driver is a "Level 3" driver | 66 // The XPS driver is a "Level 3" driver |
49 *path = path->Append(L"3"); | 67 *path = path->Append(L"3"); |
50 return S_OK; | 68 return S_OK; |
51 } | 69 } |
| 70 |
| 71 bool IsSystem64Bit() { |
| 72 base::win::OSInfo::WindowsArchitecture arch = |
| 73 base::win::OSInfo::GetInstance()->architecture(); |
| 74 return (arch == base::win::OSInfo::X64_ARCHITECTURE) || |
| 75 (arch == base::win::OSInfo::IA64_ARCHITECTURE); |
52 } | 76 } |
53 | 77 |
| 78 void LoadLocalString(DWORD string_id, wchar_t* target, DWORD target_size) { |
| 79 static wchar_t dummy = L'\0'; |
| 80 HMODULE module = NULL; |
| 81 GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
| 82 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 83 &dummy, |
| 84 &module); |
| 85 int count = LoadString(module, |
| 86 string_id, |
| 87 target, |
| 88 target_size); |
| 89 DCHECK_NE(0, count); |
| 90 } |
| 91 } |
| 92 |
OLD | NEW |