コード例 #1
0
        public static HRESULT ChangeWindowMessageFilterEx(IntPtr hwnd, WM message, MSGFLT action, out MSGFLTINFO filterInfo)
        {
            filterInfo = MSGFLTINFO.NONE;

            if (!Utility.IsOSVistaOrNewer)
            {
                return(HRESULT.S_FALSE);
            }
            if (!Utility.IsOSWindows7OrNewer)
            {
                if (!NativeMethods._ChangeWindowMessageFilter(message, action))
                {
                    return((HRESULT)Win32Error.GetLastError());
                }
                return(HRESULT.S_OK);
            }
            else
            {
                CHANGEFILTERSTRUCT changefilterstruct = new CHANGEFILTERSTRUCT
                {
                    cbSize = (uint)Marshal.SizeOf(typeof(CHANGEFILTERSTRUCT))
                };
                if (!NativeMethods._ChangeWindowMessageFilterEx(hwnd, message, action, ref changefilterstruct))
                {
                    return((HRESULT)Win32Error.GetLastError());
                }
                filterInfo = changefilterstruct.ExtStatus;
                return(HRESULT.S_OK);
            }
        }
コード例 #2
0
        public static HRESULT ChangeWindowMessageFilterEx(IntPtr hwnd, WM message, MSGFLT action, out MSGFLTINFO filterInfo)
        {
            filterInfo = MSGFLTINFO.NONE;
            if (!Utility.IsOSVistaOrNewer)
            {
                return(HRESULT.S_FALSE);
            }
            if (!Utility.IsOSWindows7OrNewer)
            {
                if (Standard.NativeMethods.ChangeWindowMessageFilter(message, action))
                {
                    return(HRESULT.S_OK);
                }
                return((HRESULT)Win32Error.GetLastError());
            }
            CHANGEFILTERSTRUCT cHANGEFILTERSTRUCT = new CHANGEFILTERSTRUCT()
            {
                cbSize = (uint)Marshal.SizeOf(typeof(CHANGEFILTERSTRUCT))
            };
            CHANGEFILTERSTRUCT cHANGEFILTERSTRUCT1 = cHANGEFILTERSTRUCT;

            if (!Standard.NativeMethods.ChangeWindowMessageFilterEx_1(hwnd, message, action, ref cHANGEFILTERSTRUCT1))
            {
                return((HRESULT)Win32Error.GetLastError());
            }
            filterInfo = cHANGEFILTERSTRUCT1.ExtStatus;
            return(HRESULT.S_OK);
        }
コード例 #3
0
ファイル: ErrorCodes.cs プロジェクト: Master-MiShutka/TMPApps
        /// <summary>
        /// Convert the result of Win32 GetLastError() into a raised exception.
        /// </summary>
        public static void ThrowLastError()
        {
            ((HRESULT)Win32Error.GetLastError()).ThrowIfFailed();

            // Only expecting to call this when we're expecting a failed GetLastError()
            Assert.Fail();
        }
コード例 #4
0
 public static void ThrowLastError()
 {
     ((HRESULT)Win32Error.GetLastError()).ThrowIfFailed();
 }
コード例 #5
0
        public static IEnumerable <FileInfo> GetFiles(DirectoryInfo startDirectory, string pattern, bool recurse)
        {
            // We suppressed this demand for each p/invoke call, so demand it upfront once
            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();

            // Validate parameters
            Verify.IsNotNull(startDirectory, "startDirectory");
            Verify.IsNeitherNullNorEmpty(pattern, "pattern");

            // Setup
            var findData    = new WIN32_FIND_DATAW();
            var directories = new Stack <DirectoryInfo>();

            directories.Push(startDirectory);

            // Process each directory.  Only push new directories if we're recursing.
            ErrorModes origErrorMode = NativeMethods.SetErrorMode(ErrorModes.FailCriticalErrors);

            try
            {
                while (directories.Count > 0)
                {
                    // Get the name of the next directory and the corresponding search pattern
                    DirectoryInfo dir     = directories.Pop();
                    string        dirPath = dir.FullName.Trim();
                    if (dirPath.Length == 0)
                    {
                        continue;
                    }
                    char lastChar = dirPath[dirPath.Length - 1];
                    if (lastChar != Path.DirectorySeparatorChar && lastChar != Path.AltDirectorySeparatorChar)
                    {
                        dirPath += Path.DirectorySeparatorChar;
                    }

                    // Process all files in that directory
                    using (SafeFindHandle handle = NativeMethods.FindFirstFileW(dirPath + pattern, findData))
                    {
                        Win32Error error;
                        if (handle.IsInvalid)
                        {
                            error = Win32Error.GetLastError();
                            if (error == Win32Error.ERROR_ACCESS_DENIED || error == Win32Error.ERROR_FILE_NOT_FOUND)
                            {
                                continue;
                            }
                            Assert.AreNotEqual(Win32Error.ERROR_SUCCESS, error);
                            ((HRESULT)error).ThrowIfFailed();
                        }

                        do
                        {
                            if (!Utility.IsFlagSet((int)findData.dwFileAttributes, (int)FileAttributes.Directory))
                            {
                                yield return(new FileInfo(dirPath + findData.cFileName));
                            }
                        }while (NativeMethods.FindNextFileW(handle, findData));
                        error = Win32Error.GetLastError();
                        if (error != Win32Error.ERROR_NO_MORE_FILES)
                        {
                            ((HRESULT)error).ThrowIfFailed();
                        }
                    }

                    // Push subdirectories onto the stack if we are recursing.
                    if (recurse)
                    {
                        // In a volatile system we can't count on all the file information staying valid.
                        // Catch reasonable exceptions and move on.
                        try
                        {
                            foreach (DirectoryInfo childDir in dir.GetDirectories())
                            {
                                try
                                {
                                    FileAttributes attrib = File.GetAttributes(childDir.FullName);
                                    // If it's not a hidden, system folder, nor a reparse point
                                    if (!Utility.IsFlagSet((int)attrib, (int)(FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReparsePoint)))
                                    {
                                        directories.Push(childDir);
                                    }
                                }
                                catch (FileNotFoundException)
                                {
                                    // Shouldn't see this.
                                    Assert.Fail();
                                }
                                catch (DirectoryNotFoundException) { }
                            }
                        }
                        catch (DirectoryNotFoundException) { }
                    }
                }
            }
            finally
            {
                NativeMethods.SetErrorMode(origErrorMode);
            }
        }