4

How can the return error code be specified on application exit? If this were a VC++ application, I could use the SetLastError(ERROR_ACCESS_DENIED) -- return GetLastError() APIs. Is there a way to do this in C#?

  static int Main(string[] args)
  {
     Tool.Args = args;

     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.Run(new Download_Tool());

     return Tool.ErrorCode;
  }

How can the Tool.ErrorCode value be set intelligably? If I try something like Tool.ErrorCode = ERROR_ACCESS_DENIED, I get an error, "The name ERROR_ACCESS_DENIED does not exist in the current context." Thanks.

ADDITIONAL INFORMATION

My example is over-simplified. Is there a way to something like this:

Tool.ErrorCode = ERROR_ACCESS_DENIED;
return Tool.ErrorCode;

...which generates a compile-error, rather than this:

Tool.ErrorCode = 5;
return Tool.ErrorCode;

...which works, but uses a "magic number." I'd like to avoid the use of magic numbers.

0

2 Answers 2

9

http://msdn.microsoft.com/en-us/library/system.environment.exit.aspx

Environment.Exit(exitCode)

Update

The reason why you get a compile error with "ERROR_ACCESS_DENIED" is because you have not defined it. You need to define it yourself:

const int ERROR_ACCESS_DENIED = 5;

Then you can use:

Environment.Exit(ERROR_ACCESS_DENIED)

Update 2

If you are looking for a ready-made set of winerror.h constants for your C# needs, then here it is:

http://www.pinvoke.net/default.aspx/Constants/WINERROR.html

I would probably modify the GetErrorName(...) method to do some caching though, e.g.:

private static Dictionary<int, string> _FieldLookup;

public static bool TryGetErrorName(int result, out string errorName)
{
    if (_FieldLookup == null)
    {
        Dictionary<int, string> tmpLookup = new Dictionary<int, string>();

        FieldInfo[] fields = typeof(ResultWin32).GetFields();

        foreach (FieldInfo field in fields)
        {
            int errorCode = (int)field.GetValue(null);

            tmpLookup.Add(errorCode, field.Name);
        }

        _FieldLookup = tmpLookup;
    }

    return _FieldLookup.TryGetValue(result, out errorName);
}
Sign up to request clarification or add additional context in comments.

6 Comments

This does not address the problem of using magic numbers to set the exit code.
I'd like to know why this was voted down. Hopefully it wasn't after the question was updated. I'm not a mind reader... That would be pretty poor.
Microsoft already has these error codes defined somewhere. In a VC++ application they can be accessed by #include "Winerror.h". Is there not something similar for C#? msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx
@Jim To my knowledge no there is not. In the past when doing PInvoke I have simply taken header files and extracted the constants into a C# helper class to get the parity you are after. C# in not Win32.
@Jim I have updated the answer to include a link to C# code for a class that has all the winerror.h constants. HTH.
|
-1

Set Environment.ExitCode.

static void Main(string[] args)
  {
     Tool.Args = args;

     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.Run(new Download_Tool());

     Environment.ExitCode = Tool.ErrorCode;
  }

See MSDN - Environment.ExitCode Property

3 Comments

This does not address the problem of using magic numbers to set the exit code.
The problem with this code snippet is that the value of Environment.ExitCode is ignored if the Main method returns anything other than void (i.e. int in this case).
I changed the code snippet to return void, so that Environment.ExitCode will not be ignored.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.