Getting the Windows System Error Code title/description from its hex number

后端 未结 4 1315
孤城傲影
孤城傲影 2020-12-13 07:00

I\'m messing around with some windows functions using p/invoke. Occasionally, I get an error code that is not ERROR_SUCCESS (such an odd name).

Is there a way to loo

4条回答
  •  有刺的猬
    2020-12-13 07:34

    I'm not sure if there's a niifty .NET wrapper, but you could call the FormatMessage API using P/Invoke.

    See this answer for how it would normally be called from native code. Though the question refers to grabbing error codes from HRESULTs, the answer also applies for retreiving codes from the regular OS error codes coming from GetLastError/GetLastWin32Error).

    EDIT: Thanks Malfist for pointing me to pinvoke.net, which includes alternative, managed API:

    using System.ComponentModel;
    
    string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
    Console.WriteLine(errorMessage);
    

提交回复
热议问题