本文整理汇总了C#中System.ComponentModel.Win32Exception类的典型用法代码示例。如果您正苦于以下问题:C# Win32Exception类的具体用法?C# Win32Exception怎么用?C# Win32Exception使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Win32Exception类属于System.ComponentModel命名空间,在下文中一共展示了Win32Exception类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UnmapFolderFromDrive
// --------------------------------------------------------------------------- -------------
// Class Name: VolumeFunctions
// Procedure Name: UnmapFolderFromDrive
// Purpose: Unmap a drive letter. We always unmp the drive, without checking the
// folder name.
// Parameters:
// - driveLetter (string) : Drive letter to be released, the the format "C:"
// - folderName (string) : Folder name that the drive is mapped to.
// --------------------------------------------------------------------------- -------------
internal static string UnmapFolderFromDrive(string driveLetter, string folderName)
{
DefineDosDevice(DDD_REMOVE_DEFINITION, driveLetter, folderName);
// Display the status of the "last" unmap we run.
string statusMessage = new Win32Exception(Marshal.GetLastWin32Error()).ToString();
return statusMessage.Substring(statusMessage.IndexOf(":") + 1);
}
示例2: ThrowCanNotLoadDll
static void ThrowCanNotLoadDll(string dllPath, int error)
{
var type = WinAPI.GetDllMachineType(dllPath);
if (type == MachineType.IMAGE_FILE_MACHINE_I386)
{
if (sizeof(long) == IntPtr.Size)
{
var message = string.Format("x64 process tries to load x86 dll = {0}; code = 0x{1:X}", dllPath, error);
throw new Exception(message);
}
}
else if (type == MachineType.IMAGE_FILE_MACHINE_AMD64)
{
if (sizeof(int) == IntPtr.Size)
{
var message = string.Format("x86 process tries to load x64 dll = {0}; code = 0x{1:X}", dllPath, error);
throw new Exception(message);
}
}
else
{
var message = string.Format("Unsupported machine type = {0} of dll = {1}; error = 0x{2:X}.", type, dllPath, error);
throw new Exception(message);
}
{
var ex = new Win32Exception(error);
var message = string.Format("Can not load library = {0} Code={1}", dllPath, error);
throw new Exception(message, ex);
}
}
示例3: ChangeOwner
public static void ChangeOwner(String s_Path, String s_UserName)
{
IntPtr pNewOwner, peUse;
Win32Exception Win32Error;
String domain_name;
int ret, sid_len, domain_len;
if (Privileges.SetPrivileges() == false)
throw new Exception("Required privilege not held by the user");
sid_len = SID_SIZE;
pNewOwner = Marshal.AllocHGlobal(sid_len);
domain_len = NAME_SIZE;
domain_name = String.Empty.PadLeft(domain_len);
peUse = IntPtr.Zero;
if (!Imports.LookupAccountName(null, s_UserName, pNewOwner, ref sid_len, domain_name, ref domain_len, ref peUse))
{
ret = Marshal.GetLastWin32Error();
Win32Error = new Win32Exception(ret);
throw new Exception(Win32Error.Message);
}
ret = Imports.SetNamedSecurityInfo(s_Path, SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, pNewOwner, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
if (ret != 0)
{
Win32Error = new Win32Exception(ret);
throw new Exception(Win32Error.Message);
}
Marshal.FreeHGlobal(pNewOwner);
}
示例4: InstantiateException
public static void InstantiateException()
{
int error = 5;
string message = "This is an error message.";
Exception innerException = new FormatException();
// Test each of the constructors and validate the properties of the resulting instance
Win32Exception ex = new Win32Exception();
Assert.Equal(expected: E_FAIL, actual: ex.HResult);
ex = new Win32Exception(error);
Assert.Equal(expected: E_FAIL, actual: ex.HResult);
Assert.Equal(expected: error, actual: ex.NativeErrorCode);
ex = new Win32Exception(message);
Assert.Equal(expected: E_FAIL, actual: ex.HResult);
Assert.Equal(expected: message, actual: ex.Message);
ex = new Win32Exception(error, message);
Assert.Equal(expected: E_FAIL, actual: ex.HResult);
Assert.Equal(expected: error, actual: ex.NativeErrorCode);
Assert.Equal(expected: message, actual: ex.Message);
ex = new Win32Exception(message, innerException);
Assert.Equal(expected: E_FAIL, actual: ex.HResult);
Assert.Equal(expected: message, actual: ex.Message);
Assert.Same(expected: innerException, actual: ex.InnerException);
}
示例5: DebugHelper
public DebugHelper(string exeName)
{
IntPtr handle = Process.GetCurrentProcess().Handle;
NativeMethods.SetErrorMode(NativeMethods.SetErrorFlags.SEM_FAILCRITICALERRORS | NativeMethods.SetErrorFlags.SEM_NOOPENFILEERRORBOX);
NativeMethods.SymSetOptions(NativeMethods.Options.SYMOPT_DEFERRED_LOADS | NativeMethods.Options.SYMOPT_DEBUG);
if (!NativeMethods.SymInitialize(handle, null, false))
{
var ex = new Win32Exception(Marshal.GetLastWin32Error());
LastErrorMessage = ex.Message;
}
_dllBase = NativeMethods.SymLoadModuleEx(handle,
IntPtr.Zero,
exeName,
null,
0,
0,
IntPtr.Zero,
NativeMethods.SymLoadModuleFlags.SLMFLAG_NONE);
if (_dllBase == 0)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
_libHandle = handle;
}
示例6: ctor
[Test] // ctor (int)
public void Constructor1 ()
{
Win32Exception ex;
ex = new Win32Exception (0);
Assert.AreEqual (-2147467259, ex.ErrorCode, "#A1");
Assert.IsNull (ex.InnerException, "#A2");
Assert.IsNotNull (ex.Message, "#A3");
Assert.IsFalse (ex.Message.IndexOf (ex.GetType ().FullName) != -1, "#A4");
Assert.AreEqual (0, ex.NativeErrorCode, "#A5");
ex = new Win32Exception (int.MinValue);
Assert.AreEqual (-2147467259, ex.ErrorCode, "#B1");
Assert.IsNull (ex.InnerException, "#B2");
Assert.IsNotNull (ex.Message, "#B3");
Assert.IsFalse (ex.Message.IndexOf (ex.GetType ().FullName) != -1, "#B4");
Assert.AreEqual (int.MinValue, ex.NativeErrorCode, "#B5");
ex = new Win32Exception (int.MaxValue);
Assert.AreEqual (-2147467259, ex.ErrorCode, "#C1");
Assert.IsNull (ex.InnerException, "#C2");
Assert.IsNotNull (ex.Message, "#C3");
Assert.IsFalse (ex.Message.IndexOf (ex.GetType ().FullName) != -1, "#C4");
Assert.AreEqual (int.MaxValue, ex.NativeErrorCode, "#C5");
}
示例7: HandleCreateDirectoryExError
/// <summary>
/// Handles any errors that CreateDirectoryEx may encounter
/// </summary>
private static void HandleCreateDirectoryExError(string template, string target, int errorCode)
{
Win32Exception win32Exception = new Win32Exception(errorCode);
Exception error;
switch ((Win32Error)errorCode)
{
case Win32Error.ACCESS_DENIED:
error = new UnauthorizedAccessException(
string.Format("Access was denied to clone '{0}' to '{1}'.", template, target),
win32Exception);
break;
case Win32Error.PATH_NOT_FOUND:
error = new DirectoryNotFoundException(
string.Format("The path '{0}' or '{1}' could not be found.", template, target),
win32Exception);
break;
case Win32Error.SHARING_VIOLATION:
error = new SharingViolationException(
string.Format("The source or destination file was in use when copying '{0}' to '{1}'.", template, target),
win32Exception);
break;
case Win32Error.ALREADY_EXISTS:
//If the directory already exists don't worry about it.
return;
default:
error = win32Exception;
break;
}
throw error;
}
示例8: AquireLock
public bool AquireLock(int timeout)
{
bool success = false;
if (Monitor.TryEnter(syncRoot, timeout))
{
if (this.stream == null)
{
int fd = Syscall.open(PortFilePath, OpenFlags.O_RDWR | OpenFlags.O_EXCL);
if (fd == -1)
{
var e = new Win32Exception(Marshal.GetLastWin32Error());
Debug.WriteLine(string.Format("Error opening {0}: {1}", PortFilePath, e.Message));
Monitor.Exit(syncRoot);
}
else
{
this.stream = new UnixStream(fd);
success = true;
}
}
}
return success;
}
示例9: ExternalToolLaunchException
public ExternalToolLaunchException(string ExecutableFilePath, string WorkingDirectoryPath, IEnumerable<ILaunchParameter> Parameters,
Win32Exception inner)
: base(string.Format("{0} ({1})", ExceptionMessage, ExecutableFilePath.Split(Path.DirectorySeparatorChar).Last()), inner)
{
this.ExecutableFilePath = ExecutableFilePath;
this.WorkingDirectoryPath = WorkingDirectoryPath;
this.Parameters = Parameters;
}
示例10: GetExceptionForLastWin32Error
// WinUser.h
/// <summary>Returns an exception corresponding to the last Win32 error.</summary>
/// <returns>Returns an exception corresponding to the last Win32 error.</returns>
internal static Exception GetExceptionForLastWin32Error()
{
var errorCode = Marshal.GetLastWin32Error();
var ex = Marshal.GetExceptionForHR( errorCode );
if( ex == null )
ex = new Win32Exception( errorCode );
return ex;
}
示例11: CloseConsole
/// <summary>
/// Closes the console associate to the current process
/// </summary>
public static void CloseConsole()
{
bool result = FreeConsole();
if (!result)
{
Win32Exception ex = new Win32Exception(Marshal.GetLastWin32Error());
throw new Exception(string.Format("Failed to close console: {0}", ex.Message));
}
}
示例12: WriteValue
public void WriteValue(string strSection, string strKey, string strValue)
{
int res = WritePrivateProfileString(strSection, strKey, strValue, this.m_fileName);
if (res == 0)
{
Win32Exception wexp = new Win32Exception(Marshal.GetLastWin32Error());
throw new IniFileParsingException("win32:" + wexp.Message);
}
}
示例13: OnStartup
protected override void OnStartup(StartupEventArgs e)
{
var test = Shcore.SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE);
if (!test)
{
var err = new Win32Exception();
}
base.OnStartup(e);
}
示例14: LoadDll
/// <summary>
/// managed wrapper around LoadLibrary
/// </summary>
/// <param name="dllName"></param>
internal static void LoadDll(string dllName)
{
if (tempFolder == "")
throw new Exception("Please call ExtractEmbeddedDlls before LoadDll");
IntPtr h = LoadLibrary(dllName);
if (h == IntPtr.Zero)
{
Exception e = new Win32Exception();
throw new DllNotFoundException(String.Format("Unable to load library: {0} from {1}", dllName, tempFolder), e);
}
}
示例15: Read
public int Read(int BytesToRead)
{
int BytesRead = 0;
if (!ReadFile( pHandle, pBuffer, BytesToRead, &BytesRead, 0 ))
{
Win32Exception WE = new Win32Exception();
ApplicationException AE = new ApplicationException( "WinFileIO:Read - Error occurred reading a file. - " + WE.Message);
throw AE;
}
return BytesRead;
}