OLD | NEW |
| (Empty) |
1 // Copyright 2004-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #include "omaha/base/debug.h" | |
17 #include "omaha/base/logging.h" | |
18 #include "omaha/base/module_utils.h" | |
19 #include "omaha/base/utils.h" | |
20 | |
21 namespace omaha { | |
22 | |
23 const int kLongPath = (_MAX_PATH * 2); | |
24 const int kReallyLongPath = (kLongPath * 2); | |
25 | |
26 HMODULE ModuleFromStatic(void* pointer_to_static_in_module) { | |
27 ASSERT(pointer_to_static_in_module, (L"")); | |
28 | |
29 MEMORY_BASIC_INFORMATION info = { 0 }; | |
30 VirtualQuery(reinterpret_cast<void*>(pointer_to_static_in_module), | |
31 &info, sizeof(info)); | |
32 // Module handles are just the allocation base address of the module. | |
33 return reinterpret_cast<HMODULE>(info.AllocationBase); | |
34 } | |
35 | |
36 bool GetModuleDirectory(HMODULE module, TCHAR* directory) { | |
37 ASSERT(directory, (L"Invalid arguments")); | |
38 if (!directory) { | |
39 return false; | |
40 } | |
41 | |
42 // PathRemoveFileSpec only supports buffers up to MAX_PATH so we must | |
43 // limit ourselves to this. It will "always" work anyway, given that | |
44 // our installation path is not absurdly deep. | |
45 if (0 == GetModuleFileName(module, directory, MAX_PATH)) { | |
46 ASSERT(false, (L"Path longer than MAX_PATH")); | |
47 return false; | |
48 } | |
49 | |
50 if (!String_PathRemoveFileSpec(directory)) { | |
51 ASSERT(false, (L"PathRemoveFileSpec failed")); | |
52 // Ensure we don't return with an incorrect path in the buffer that was | |
53 // passed in. | |
54 ZeroMemory(directory, MAX_PATH * sizeof(TCHAR)); | |
55 return false; | |
56 } | |
57 | |
58 return true; | |
59 } | |
60 | |
61 HRESULT GetModuleFileName(HMODULE module, CString* path) { | |
62 ASSERT(path, (_T("must be valid"))); | |
63 | |
64 // _MAX_PATH should cover at least 99% of the paths | |
65 int buf_size = _MAX_PATH; | |
66 int chars_copied = 0; | |
67 while ((chars_copied = ::GetModuleFileName(module, | |
68 CStrBuf(*path, buf_size + 1), | |
69 buf_size)) == buf_size) { | |
70 // We'll stop before things get ridiculous | |
71 if (buf_size >= kReallyLongPath) { | |
72 UTIL_LOG(LEVEL_ERROR, | |
73 (_T("[GetModuleFileName - unusually long path '%s']"), path)); | |
74 chars_copied = 0; | |
75 ::SetLastError(ERROR_NOT_ENOUGH_MEMORY); | |
76 break; | |
77 } | |
78 | |
79 buf_size *= 2; | |
80 } | |
81 | |
82 if (!chars_copied) { | |
83 path->Empty(); | |
84 return GetCurError(); | |
85 } | |
86 | |
87 return S_OK; | |
88 } | |
89 | |
90 } // namespace omaha | |
91 | |
OLD | NEW |