# function FormatMessage

**URL:** https://discourse.julialang.org/t/function-formatmessage/8593
**Category:** Internals & Design
**Created:** [January 25, 2018, 12:25pm UTC](https://discourse.julialang.org/t/function-formatmessage/8593 "2018-01-25T12:25:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Yaroslav\_Kavenchuk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yaroslav_kavenchuk/32/7642_2.png) [@Yaroslav\_Kavenchuk](https://discourse.julialang.org/u/Yaroslav_Kavenchuk)
#### Post date: [January 25, 2018, 12:25pm UTC](https://discourse.julialang.org/t/function-formatmessage/8593/1 "2018-01-25T12:25:50Z")

</div>

```julia
        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?

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [January 29, 2018, 5:09am UTC](https://discourse.julialang.org/t/function-formatmessage/8593/2 "2018-01-29T05:09:04Z")

</div>

`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.

---

<div class="post-metadata">

### Author: ![Yaroslav\_Kavenchuk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yaroslav_kavenchuk/32/7642_2.png) [@Yaroslav\_Kavenchuk](https://discourse.julialang.org/u/Yaroslav_Kavenchuk)
#### Post date: [January 29, 2018, 7:19am UTC](https://discourse.julialang.org/t/function-formatmessage/8593/3 "2018-01-29T07:19:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [January 31, 2018, 5:38pm UTC](https://discourse.julialang.org/t/function-formatmessage/8593/4 "2018-01-31T17:38:50Z")

</div>

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.
