tags:

views:

49

answers:

2

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.

A: 

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

Environment.Exit(exitCode)

Update

I'm assuming that I was voted down after the question was updated - sheesh. And by the author too...

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);
}
chibacity
This does not address the problem of using magic numbers to set the exit code.
Jim Fell
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.
chibacity
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#?http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx
Jim Fell
@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.
chibacity
@Jim I have updated the answer to include a link to C# code for a class that has all the winerror.h constants. HTH.
chibacity
A: 

Set Environment.ExitCode.

static int 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

Mufasa
This does not address the problem of using magic numbers to set the exit code.
Jim Fell