Exemplo n.º 1
0
        private static string GetErrorMessage(int error, string customErrMsg)
        {
            string lastErrMsg = null;

            if (!customErrMsg.IsNull())
            {
                customErrMsg = customErrMsg.Trim(); //.NTrim();
            }
            try
            {
                StringBuilder lpBuffer = new StringBuilder(0x100);
                if (ChoKernel32.FormatMessage(0x3200, Win32Common.NullHandleRef, error, 0, lpBuffer, lpBuffer.Capacity + 1, IntPtr.Zero) == 0)
                {
                    lastErrMsg = "Unknown error (0x" + Convert.ToString(error, 0x10) + ")";
                }

                int length = lpBuffer.Length;
                while (length > 0)
                {
                    char ch = lpBuffer[length - 1];
                    if ((ch > ' ') && (ch != '.'))
                    {
                        break;
                    }
                    length--;
                }
                lastErrMsg = lpBuffer.ToString(0, length);
            }
            finally
            {
                if (!String.IsNullOrEmpty(customErrMsg))
                {
                    if (customErrMsg.EndsWith("."))
                    {
                        lastErrMsg = String.Format("{0} {1}.", customErrMsg, lastErrMsg);
                    }
                    else
                    {
                        lastErrMsg = String.Format("{0}. {1}.", customErrMsg, lastErrMsg);
                    }
                }
            }

            return(lastErrMsg);
        }
Exemplo n.º 2
0
        public static bool GrandShutdownPrivilege()
        {
            // This code mimics the MSDN defined way to adjust privilege for shutdown
            // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/shutting_down.asp

            IntPtr tokenHandle = IntPtr.Zero;

            try
            {
                if (!OpenProcessToken(ChoKernel32.GetCurrentProcess(), (int)Win32Common.TOKEN_ADJUST_PRIVILEGES | (int)Win32Common.TOKEN_QUERY, ref tokenHandle))
                {
                    return(false);
                }

                LUID luid = new LUID();
                LookupPrivilegeValue(null, Win32Common.SE_SHUTDOWN_NAME, ref luid);

                TOKEN_PRIVILEGES tokenPrivileges = new TOKEN_PRIVILEGES();
                tokenPrivileges.PrivilegeCount = 1;
                tokenPrivileges.Privileges     = new LUID_AND_ATTRIBUTES[] { new LUID_AND_ATTRIBUTES(luid, (int)Win32Common.SE_PRIVILEGE_ENABLED) };

                TOKEN_PRIVILEGES tokenPrivileges1 = new TOKEN_PRIVILEGES();

                int  retLen = 0;
                bool result = AdjustTokenPrivileges(tokenHandle, false, ref tokenPrivileges, 0, ref tokenPrivileges1, ref retLen);

                if (ChoKernel32.GetLastError() != 0)
                {
                    throw new Exception("Failed to grant shutdown privilege.");
                }

                return(true);
            }
            finally
            {
                if (tokenHandle != IntPtr.Zero)
                {
                    ChoKernel32.CloseHandle(tokenHandle);
                }
            }
        }