function FormatMessage


#1
        lpMsgBuf = Ref{Ptr{UInt16}}()
        lpMsgBuf[] = 0
        len = ccall(:FormatMessageW, stdcall, UInt32, (Cint, Ptr{Cvoid}, Cint, Cint, Ptr{Ptr{UInt16}}, Cint, Ptr{Cvoid}),
                    FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
                    C_NULL, e, 0, lpMsgBuf, 0, C_NULL)
        p = lpMsgBuf[]
        len == 0 && return ""
        buf = Vector{UInt16}(uninitialized, len)
        GC.@preserve buf unsafe_copyto!(pointer(buf), p, len)
        ccall(:LocalFree, stdcall, Ptr{Cvoid}, (Ptr{Cvoid},), p)
        return transcode(String, buf)

Why is the p copied to buf before encoding it?
Why not decode the p at once?


#2

transcode wants an array, so we need to first wrap the data. This function isn’t performance-sensative, so that’s just done in a simple manner of copying the data into a gc-managed array first, then converting to utf8 second.


#3

Many thanks!
Can an example of a similar code, but critical to the speed of execution?


#4

An example in Base? I can’t think of any, and don’t expect there are any. unsafe_copyto! is very fast relative to either FormatMessageW or transcode, so it’s pretty hard to construct a situation where this would end up being performance-sensative.