1

How to get the result of the "net helpmsg code" cmd ? Should I start a process (like here) and parse the return message ? Or I can use an existing api ?

Hyro
  • 23
  • 4
  • This depends a little on what error numbers you're intending to format; `net helpmsg` doesn't cover everything itself. For starters, try `new System.ComponentModel.Win32Exception(code).Message`, which is a simple wrapper around `FormatMessage`. This will not translate HRESULTs; `Marshal.GetExceptionForHR` will. – Jeroen Mostert Nov 23 '17 at 17:54

1 Answers1

0

This is VB for that feature. Load the dll with the messages then call FormatMessage. This also gets the Win32 code for a NTStatus error.

Public Declare Function RtlNtStatusToDosError Lib "ntdll.dll" (ByVal status As Long) As Long
Public Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, ByVal lpSource As Long, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Public Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
Public Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long

Sub Main()
    Dim x  As Long
    Dim hNtdll As Long
    Dim WinError As Long
    Dim NTSTatus As Long
    Dim Ret As Long
    Dim RetStr As String
    RetStr = Space(1020)
    Dim RetStr1 As String
    RetStr1 = Space(1020)
            NTSTatus = Val(InputBox("Enter NTStatus code", "", "&hc0000005"))
            WinError = RtlNtStatusToDosError(NTSTatus)
            Ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM + FORMAT_MESSAGE_IGNORE_INSERTS, vbNull, WinError, 0, RetStr, 1020, 0)
            RetStr = Left(RetStr, Ret)
            hNtdll = LoadLibrary("C:\Windows\System32\en-US\ntdll.dll.mui")
            Ret = FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS + FORMAT_MESSAGE_FROM_HMODULE, hNtdll, NTSTatus, 0, RetStr1, 1020, 0)
            RetStr = "NT Status Message:" & vbCrLf & Left(RetStr1, Ret) & vbCrLf & vbCrLf & "Windows Error Message:" & vbCrLf & RetStr
            MsgBox "NT Status 0x" & Hex(NTSTatus) & " (" & NTSTatus & ")" & vbCrLf & "Windows Error 0x" & Hex(WinError) & " (" & WinError & ")" & vbCrLf & vbCrLf & RetStr
End Sub
ACatInLove
  • 530
  • 2
  • 5