FormatMessage resolved the NT status code to an error message. The cache of resolved messages is kept to speed up status code translation and alleviate the pressure on API call invocations.
(status uint32)
| 41 | // messages is kept to speed up status code translation and alleviate the pressure on |
| 42 | // API call invocations. |
| 43 | func FormatMessage(status uint32) string { |
| 44 | if isSuccess(status) { |
| 45 | return Success |
| 46 | } |
| 47 | mux.Lock() |
| 48 | defer mux.Unlock() |
| 49 | if s, ok := statusCache[status]; ok { |
| 50 | return s |
| 51 | } |
| 52 | var flags uint32 = windows.FORMAT_MESSAGE_FROM_SYSTEM |
| 53 | b := make([]uint16, 300) |
| 54 | msgID := sys.RtlNtStatusToDosError(status) |
| 55 | n, err := windows.FormatMessage(flags, 0, msgID, 0, b, nil) |
| 56 | if err != nil { |
| 57 | return "Unknown" |
| 58 | } |
| 59 | // trim terminating \r and \n |
| 60 | for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- { |
| 61 | } |
| 62 | statusCache[status] = string(utf16.Decode(b[:n-1])) |
| 63 | return statusCache[status] |
| 64 | } |
no test coverage detected