static void Main(string[] args) { int errorResult; int errorInfo; Console.WriteLine("Test of Beep"); errorResult = Kernel32Wrap.Beep(2000, 500); if (errorResult == 0) // In case of an error { Console.WriteLine("Call of Beep returned an error."); } Console.WriteLine("Will now try to provoke an error - does not work on Windows 7 or 8 (no error here)."); errorResult = Kernel32Wrap.Beep(10, 1000); // Herz must above 36 if (errorResult == 0) // In case of an error { Console.WriteLine("Call of Beep returned an error."); errorInfo = Marshal.GetLastWin32Error(); Console.WriteLine(" Last Win32 error: {0}", errorInfo); Console.WriteLine(" Error name: {0}", ResultWin32.GetErrorName(errorInfo)); } Console.ReadLine(); BeepMusic.Play(); }
public static void ReportMsiCallFailure(string funcCall, int status) { StringBuilder errMsg = new StringBuilder("Error calling " + funcCall + " WIN API occured. Error: " + status); string errName = ResultWin32.GetErrorName(status); if (errName != String.Empty) { errMsg.Append(" (" + errName + ")"); } throw new MsiException(errMsg.ToString(), status); }
public void IsSameFileTestSymLinkDir() { var currentDir = Directory.GetCurrentDirectory(); var folder = Path.Combine("the", "fat", "cat"); Directory.CreateDirectory(folder); var filename = Path.Combine(folder, "myfile.txt"); File.WriteAllLines(filename, new[] { "first line" }); var linkdir = "catdir"; var lastError = NativeMethods.CreateSymbolicLinkAndGetError(linkdir, folder, NativeMethods.SymbolicLink.Directory); var errorMessage = ResultWin32.GetErrorName(lastError); PathExtensions.IsSameFile(folder, linkdir).Should().BeTrue(); }
public static uint WNetAddConnection(string username, string password, string remoteName, string localName) { NetResource netResource = new NetResource(); netResource.Scope = ResourceScope.GlobalNetwork; //RESOURCE_GLOBALNET netResource.ResourceType = ResourceType.Any; //RESOURCETYPE_ANY netResource.DisplayType = ResourceDisplaytype.Share; //RESOURCEDISPLAYTYPE_GENERIC netResource.Usage = ResourceUsage.CONNECTABLE; //RESOURCEUSAGE_CONNECTABLE netResource.LocalName = localName; netResource.RemoteName = remoteName.TrimEnd('/', '\\'); //netResource.lpRemoteName = lpComment; //netResource.lpProvider = null; uint result = WNetAddConnection2(netResource, password, username, 0); if (result != ResultWin32.ERROR_SUCCESS && result != ResultWin32.ERROR_SESSION_CREDENTIAL_CONFLICT) { string errName = ResultWin32.GetErrorName((int)result); throw new StorageConnectExceptin("用户:" + username + " 登录 " + localName + " 失败:" + errName + "(0x" + result.ToString("X") + ")"); } return(result); }
/// <summary> /// RegHiveUnload,<KeyName> /// /// Unload HiveFile from local machine /// </summary> /// <param name="cmd"></param> /// <returns></returns> private LogInfo[] RegHiveUnload(BakeryCommand cmd) { ArrayList logs = new ArrayList(); string logResult = string.Empty; LogState resState = LogState.Success; // Must-have operand : 2 if (cmd.Operands.Length < 2) { throw new InvalidOperandException("Not enough operand"); } else if (2 < cmd.Operands.Length) { throw new InvalidOperandException("Too many operands"); } string keyName = cmd.Operands[0]; string hiveFileName = cmd.Operands[1]; if (!File.Exists(hiveFileName)) { throw new FileNotFoundException(hiveFileName + " does not exists"); } int ret = RegLoadKey(HKLM, keyName, hiveFileName); if (ret != ResultWin32.ERROR_SUCCESS) { logResult = string.Format("RegLoadKey API returned {0}:{1}", ret, ResultWin32.GetErrorName(ret)); resState = LogState.Error; } logs.Add(new LogInfo(cmd, logResult, resState)); return(logs.ToArray(typeof(LogInfo)) as LogInfo[]); }
/* * Registry Commands */ /// <summary> /// RegHiveLoad,<KeyName>,<HiveFileName> /// /// Load HiveFile into local machine's key /// </summary> /// <param name="cmd"></param> /// <returns></returns> private LogInfo[] RegHiveLoad(BakeryCommand cmd) { ArrayList logs = new ArrayList(); // Must-have operand : 2 if (cmd.Operands.Length < 2) { throw new InvalidOperandException("Not enough operand"); } else if (2 < cmd.Operands.Length) { throw new InvalidOperandException("Too many operands"); } string keyName = cmd.Operands[0]; string hiveFileName = cmd.Operands[1]; if (!File.Exists(hiveFileName)) { throw new FileNotFoundException(hiveFileName + " does not exists"); } int ret = RegLoadKey(HKLM, keyName, hiveFileName); if (ret == ResultWin32.ERROR_SUCCESS) { logs.Add(new LogInfo(cmd, string.Concat("Loaded ", hiveFileName, " into HKLM\\", keyName), LogState.Success)); } else { logs.Add(new LogInfo(cmd, string.Concat("RegLoadKey API returned error : ", ret, " (", ResultWin32.GetErrorName(ret), ")"), LogState.Error)); } return(logs.ToArray(typeof(LogInfo)) as LogInfo[]); }
internal void Restore(object sender, EventArgs e) { try { // ignore extra params // first, restore the window rectangles and normal/maximized/minimized states Dictionary <string, Window> newWindows = new Dictionary <string, Window>(); foreach (var hwndWindowPair in HwndWindowDictionary) { IntPtr hwnd = new IntPtr(Convert.ToInt32(hwndWindowPair.Key, 16)); Window win = hwndWindowPair.Value; if (RestrictedWindows.Contains(win.ProcessName)) { Logger.Console($"Not moving restricted window: {win.Details}"); continue; } if (!TryMoveWindow(hwnd, win)) { hwnd = FindHwndForDetachedWindow(win); if (newWindows.ContainsKey(hwnd.ToString())) { Logger.Console($"Not moving window: Duplicate hwnd found for window {win.Details}: {hwnd}"); continue; } if (hwnd == IntPtr.Zero) { Logger.Console($"Could not find hwnd for window {win.Details}"); continue; } if (!TryMoveWindow(hwnd, win)) { var lastErrorCode = GetLastError(); Logger.Log($"Can't move window {GetWindowName(null, hwnd)} ({hwnd}): {ResultWin32.GetErrorName(lastErrorCode)} ({lastErrorCode})"); continue; } } if (!newWindows.ContainsKey(hwnd.ToString())) { newWindows.Add(hwnd.ToString(), win); } else { Logger.Console($"Duplicate hwnd found for window {win.Details}: {hwnd}"); } } // now update the z-orders WindowsBackToTop = WindowsBackToTop.FindAll(IsWindowVisible); var positionStructure = BeginDeferWindowPos(WindowsBackToTop.Count); for (var i = 0; i < WindowsBackToTop.Count; i++) { positionStructure = UpdateZOrder(i, positionStructure); } HwndWindowDictionary.Clear(); foreach (var kvp in newWindows) { HwndWindowDictionary.Add(kvp.Key, kvp.Value); } EndDeferWindowPos(positionStructure); } catch (Exception ex) { Logger.Log($"Error restoring snapshot: {ex}"); } }