The documentation doesn't specify the value of the last error upon success, so I think it's better to check len first.
Also, the documentation mentions that the last error doesn't become ERROR_INSUFFICIENT_BUFFER on Windows XP, so it's probably even better to check only len.

tl;dr replace line with:

    if (len == buffer.length)