github.com/hspan/go-ole@v0.0.0/error_windows.go (about)

     1  // +build windows
     2  
     3  package ole
     4  
     5  import (
     6  	"fmt"
     7  	"syscall"
     8  	"unicode/utf16"
     9  )
    10  
    11  // errstr converts error code to string.
    12  func errstr(errno int) string {
    13  	// ask windows for the remaining errors
    14  	var flags uint32 = syscall.FORMAT_MESSAGE_FROM_SYSTEM | syscall.FORMAT_MESSAGE_ARGUMENT_ARRAY | syscall.FORMAT_MESSAGE_IGNORE_INSERTS
    15  	b := make([]uint16, 300)
    16  	n, err := syscall.FormatMessage(flags, 0, uint32(errno), 0, b, nil)
    17  	if err != nil {
    18  		return fmt.Sprintf("error %d (FormatMessage failed with: %v)", errno, err)
    19  	}
    20  	// trim terminating \r and \n
    21  	for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- {
    22  	}
    23  	return string(utf16.Decode(b[:n]))
    24  }