Skip to content

Commit

Permalink
[MSGTRANS]
Browse files Browse the repository at this point in the history
- Add support for NTSTATUS to Win32 error code conversion
- Allow codes to be looked up via #define value as well as numeric
  • Loading branch information
gedmurphy committed Jan 26, 2018
1 parent 8a9417b commit 6fc7f87
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 34 deletions.
8 changes: 4 additions & 4 deletions MsgTrans.Library/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public Command(MessageTranslator msgTrans)
}

protected void AddMessage(MessageType msgType,
long dec,
string hex,
string code,
string msg)
long dec,
string hex,
string code,
string msg)
{
MsgType = msgType;
Number = dec;
Expand Down
1 change: 1 addition & 0 deletions MsgTrans.Library/MsgTrans.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Compile Include="bugcheck.cs" />
<Compile Include="BugCommand.cs" />
<Compile Include="CustomCommand.cs" />
<Compile Include="NtStatusToWin32.cs" />
<Compile Include="XmlCommand.cs" />
<Compile Include="ErrorCommand.cs" />
<Compile Include="HresultCommand.cs" />
Expand Down
5 changes: 5 additions & 0 deletions MsgTrans.Library/MsgTrans.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public enum MessageType
BugUrl = 5,
//
// Summary:
// NTSTATUS to Win32 code conversion
NtStatusToDos = 5,
//
// Summary:
// a custom Check code
Custom = 6
}
Expand Down Expand Up @@ -81,6 +85,7 @@ public MessageTranslator(IMsgOutput msgOutput,
bugcheckXml));
commands.Add(new WMCommand(this, wmXml));
commands.Add(new BugCommand(this, bugUrl));
commands.Add(new NtStatusToWin32(this, ntstatusXml, winerrorXml));
}

public bool ParseCommandMessage(MessageContext context,
Expand Down
53 changes: 38 additions & 15 deletions MsgTrans.Library/NtStatusCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,35 @@ public override bool Handle(MessageContext context,
return false;
}

long dec;
string hex;
NumberParser np = new NumberParser();
if (!np.Parse(ntstatusText))
if (np.Parse(ntstatusText))
{
return false;
dec = np.Decimal;
hex = np.Hex;
}

string description = GetNtstatusDescription(np.Decimal);
else
{
dec = GetNtStatusNumber(ntstatusText);
if (dec == -1)
{
return false;
}
hex = dec.ToString("X");
}

string description = GetNtstatusDescription(dec);
if (description != null)
{
AddMessage(MessageType.NTSTATUS,
np.Decimal,
np.Hex,
dec,
hex,
description,
null);

return true;
}/*
else
{
MsgTrans.MsgOutput.MsgOut(context,
String.Format("I don't know about NTSTATUS {0}.",
ntstatusText));
return false;
}*/
}

return false;
}
Expand All @@ -75,5 +79,24 @@ public string GetNtstatusDescription(long ntstatus)
else
return null;
}

private long GetNtStatusNumber(string ntstatus)
{
XmlElement root = base.m_XmlDocument.DocumentElement;
XmlNode node = root.SelectSingleNode(String.Format("Ntstatus[@text='{0}']",
ntstatus));
if (node != null)
{
XmlAttribute value = node.Attributes["value"];
if (value == null)
throw new Exception("Node has no value attribute.");

string hex = value.Value;

return Convert.ToInt64(hex, 16);
}
else
return -1;
}
}
}
108 changes: 108 additions & 0 deletions MsgTrans.Library/NtStatusToWin32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Xml;
using System.Runtime.InteropServices;

namespace MsgTrans.Library
{
public class NtStatusToWin32 : Command
{
NtStatusCommand NtstatusCodes = null;
WinerrorCommand WinerrorCodes = null;

[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string FileName);

[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string ProcName);

[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate ulong fRtlNtStatusToDosError(ulong NtStatusCode);


public NtStatusToWin32(MessageTranslator msgTrans,
string ntstatusXml,
string winerrorXml) :
base(msgTrans)
{
NtstatusCodes = new NtStatusCommand(msgTrans, ntstatusXml);
WinerrorCodes = new WinerrorCommand(msgTrans, winerrorXml);
}

public override string[] AvailableCommands
{
get { return new string[] { "ntstat2dos" }; }
}

public override bool Handle(MessageContext context,
string commandName,
string parameters)
{
string ntstatusText = parameters;
if (ntstatusText.Equals(String.Empty))
{
return false;
}

if (!NtstatusCodes.Handle(context, commandName, parameters))
{
return false;
}

long DosCode = GetDosCodeFromNtstatus(NtstatusCodes.Number);
if (DosCode == 0xFFFFFFFF)
{
return false;
}

if (!WinerrorCodes.Handle(context, commandName, DosCode.ToString()))
{
return false;
}

return true;
}

public override string Help()
{
return "blurgh <value>";
}

private long GetDosCodeFromNtstatus(long ntstatus)
{
ulong DosCode = 0xFFFFFFFF;
IntPtr hModule = LoadLibrary(@"ntdll.dll");
if (hModule != null)
{
IntPtr Address = GetProcAddress(hModule, "RtlNtStatusToDosError");

fRtlNtStatusToDosError RtlNtStatusToDosError =
(fRtlNtStatusToDosError)Marshal.GetDelegateForFunctionPointer(Address,
typeof(fRtlNtStatusToDosError));

DosCode = RtlNtStatusToDosError((ulong)ntstatus);

FreeLibrary(hModule);

}
return (long)DosCode;
}
}
}


/*
DWORD Status;
RTL_NTSTATUS_TO_DOS_ERROR pRtlNtStatusToDosError;
HMODULE hModule = LoadLibraryW(L"ntdll.dll");
if (hModule)
{
pRtlNtStatusToDosError = (RTL_NTSTATUS_TO_DOS_ERROR)GetProcAddress(hModule, "RtlNtStatusToDosError");
#define STATUS_PROCESS_IS_PROTECTED ((NTSTATUS)0xC0000712L)
Status = pRtlNtStatusToDosError(STATUS_PROCESS_IS_PROTECTED);
}
*/
52 changes: 37 additions & 15 deletions MsgTrans.Library/WinerrorCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,37 @@ public override bool Handle(MessageContext context,
return false;
}

long dec;
string hex;
NumberParser np = new NumberParser();
if (!np.Parse(winerrorText))
if (np.Parse(winerrorText))
{
return false;
dec = np.Decimal;
hex = np.Hex;
}

string description = GetWinerrorDescription(np.Decimal);
else
{
dec = GetNtStatusNumber(winerrorText);
if (dec == -1)
{
return false;
}
hex = dec.ToString("X");
}

string description = GetWinerrorDescription(dec);
if (description != null)
{
string message = new System.ComponentModel.Win32Exception(Convert.ToInt32(np.Decimal)).Message;
string message = new System.ComponentModel.Win32Exception(Convert.ToInt32(dec)).Message;

AddMessage(MessageType.WinError,
np.Decimal,
np.Hex,
dec,
hex,
description,
message);

return true;
}/*
else
{
MsgTrans.MsgOutput.MsgOut(context,
String.Format("I don't know about System Error Code {0}.",
winerrorText));
return false;
}*/
}

return false;
}
Expand All @@ -77,5 +82,22 @@ public string GetWinerrorDescription(long winerror)
else
return null;
}

private long GetNtStatusNumber(string winerrorName)
{
XmlElement root = base.m_XmlDocument.DocumentElement;
XmlNode node = root.SelectSingleNode(String.Format("Winerror[@text='{0}']",
winerrorName));
if (node != null)
{
XmlAttribute value = node.Attributes["value"];
if (value == null)
throw new Exception("Node has no value attribute.");

return Convert.ToInt64(value.Value);
}
else
return -1;
}
}
}

0 comments on commit 6fc7f87

Please sign in to comment.