See the question and my original answer on StackOverflow

Here some sample code that works:

Sub Main()

    Dim h As IntPtr = LoadLibrary("wininet.dll") ' or GetModuleHandle ...
    Dim sb = New StringBuilder(1024)
    FormatMessage(Format_Message.FORMAT_MESSAGE_FROM_HMODULE Or Format_Message.FORMAT_MESSAGE_IGNORE_INSERTS,
        h,
        12002,
        0,
        sb,
        sb.Capacity,
        Nothing)

    Console.WriteLine(sb) ' prints "The operation timed out"
   ' FreeLibrary, etc.
End Sub

Enum Format_Message
    FORMAT_MESSAGE_IGNORE_INSERTS = &H200
    FORMAT_MESSAGE_FROM_SYSTEM = &H1000
    FORMAT_MESSAGE_FROM_HMODULE = &H800
End Enum

<DllImport("Kernel32", SetLastError:=True, CharSet:=CharSet.Unicode)>
Public Function FormatMessage(ByVal dwFlags As Format_Message, ByVal lpSource As IntPtr, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, lpBuffer As StringBuilder, ByVal nSize As Integer, ByVal Arguments As IntPtr) As Integer
End Function

<DllImport("kernel32", SetLastError:=True, CharSet:=CharSet.Unicode)>
Public Function LoadLibrary(ByVal lpFileName As String) As IntPtr
End Function