| 121 | } |
| 122 | |
| 123 | wstring GetWindowsErrorString(DWORD dwLastErr) |
| 124 | { |
| 125 | wstring mes; |
| 126 | |
| 127 | if (dwLastErr == 0) { |
| 128 | mes += L"unknown windows error 0"; |
| 129 | return mes; |
| 130 | } |
| 131 | |
| 132 | LPTSTR errorText = NULL; |
| 133 | |
| 134 | if (!::FormatMessageW( |
| 135 | // use system message tables to retrieve error text |
| 136 | FORMAT_MESSAGE_FROM_SYSTEM |
| 137 | // allocate buffer on local heap for error text |
| 138 | | FORMAT_MESSAGE_ALLOCATE_BUFFER |
| 139 | // Important! will fail otherwise, since we're not |
| 140 | // (and CANNOT) pass insertion parameters |
| 141 | | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 142 | NULL, // unused with FORMAT_MESSAGE_FROM_SYSTEM |
| 143 | dwLastErr, |
| 144 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 145 | (LPTSTR)&errorText, // output |
| 146 | 0, // minimum size for output buffer |
| 147 | NULL)) { // arguments - see note |
| 148 | |
| 149 | mes += L"unable to get message for error " + to_wstring(dwLastErr); |
| 150 | |
| 151 | if (errorText) { |
| 152 | LocalFree(errorText); |
| 153 | } |
| 154 | |
| 155 | return mes; |
| 156 | } |
| 157 | |
| 158 | if (errorText) { |
| 159 | // ... do something with the string `errorText` - log it, display it to the user, etc. |
| 160 | mes += errorText; |
| 161 | // release memory allocated by FormatMessage() |
| 162 | LocalFree(errorText); |
| 163 | errorText = NULL; |
| 164 | } else { |
| 165 | mes += L"got null message for error " + to_wstring(dwLastErr); |
| 166 | } |
| 167 | |
| 168 | return mes; |
| 169 | } |
| 170 | |
| 171 | static bool IsProcessElevated() |
| 172 | { |
no outgoing calls
no test coverage detected