public static void SafeHandle_invalid() { MySafeHandle mch = new MySafeHandle(); Assert.Equal(false, mch.IsClosed); Assert.Equal(true, mch.IsInvalid); }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1: Verify ctor can set correct handle value"); try { MySafeHandle msf = new MySafeHandle(); if (msf.Handle != IntPtr.Zero) { TestLibrary.TestFramework.LogError("001.1", "Ctor can not set correct handle value"); TestLibrary.TestFramework.LogInformation("WARNING: [LOCAL VARIABLES] msf.Handle = " + msf.Handle.ToString()); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("001.0", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest2: Check IsClosed return false when SetHandleAsInvalid method is not called and don't excute any close handle operation "); try { MySafeHandle msh = new MySafeHandle(); IntPtr myIptr = new IntPtr(1000); msh.MySetHandle(myIptr); if (msh.IsClosed) { TestLibrary.TestFramework.LogError("002.1", "IsClosed should return false "); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest2: Verify the handle is a protected value, and can set a value"); try { int randValue = TestLibrary.Generator.GetInt32(-55); IntPtr value = new IntPtr(randValue); MySafeHandle msf = new MySafeHandle(value); if (null == msf) { TestLibrary.TestFramework.LogError("002.1", "Failed to allocate a new safe handle instance"); TestLibrary.TestFramework.LogInformation("[LOCAL VARIABLES] randValue = " + randValue); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1: Check IsClosed return true when SetHandleAsInvalid method is called "); try { MySafeHandle msh = new MySafeHandle(); IntPtr myIptr = new IntPtr(1000); msh.MySetHandle(myIptr); msh.SetHandleAsInvalid(); if (!msh.IsClosed ) { TestLibrary.TestFramework.LogError("001.1", "IsClosed should return true"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest2: Check IsInvalid return false when the handle value is Released. "); try { MySafeHandle msh = new MySafeHandle(); IntPtr myIptr = new IntPtr(1000); msh.MySetHandle(myIptr); msh.MyReleaseInvoke(); if (msh.IsInvalid) { TestLibrary.TestFramework.LogError("002.1", "IsInvalid should return false "); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest2: Check IsClosed return false when SetHandleAsInvalid method is not called and don't excute any close handle operation "); try { MySafeHandle msh = new MySafeHandle(); IntPtr myIptr = new IntPtr(1000); msh.MySetHandle(myIptr); if (msh.IsClosed) { TestLibrary.TestFramework.LogError("002.1", "IsClosed should return false "); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1: Verify the handle is a protected value"); try { MySafeHandle msf = new MySafeHandle(); if (null == msf) { TestLibrary.TestFramework.LogError("001.1", "Failed to allocate a new safe handle instance"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest2: Check IsInvalid return false when the handle value is Released. "); try { MySafeHandle msh = new MySafeHandle(); IntPtr myIptr = new IntPtr(1000); msh.MySetHandle(myIptr); msh.MyReleaseInvoke(); if (msh.IsInvalid) { TestLibrary.TestFramework.LogError("002.1", "IsInvalid should return false "); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1: Call SafeHandle.SetHandleAsInvalid "); try { MySafeHandle msh = new MySafeHandle(); IntPtr myIptr = new IntPtr(1000); msh.MySetHandle(myIptr); msh.SetHandleAsInvalid(); if (!msh.IsClosed || msh.GetHandle() != myIptr) { TestLibrary.TestFramework.LogError("001.1", "SetHandleAsInvalid has error "); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1: Verify ctor can set correct handle value"); try { MySafeHandle msf = new MySafeHandle(); if (msf.Handle != IntPtr.Zero) { TestLibrary.TestFramework.LogError("001.1", "Ctor can not set correct handle value"); TestLibrary.TestFramework.LogInformation("WARNING: [LOCAL VARIABLES] msf.Handle = " + msf.Handle.ToString()); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("001.0", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
public static void SafeHandle_valid() { MySafeHandle mch = new MySafeHandle(new IntPtr(1)); Assert.Equal(false, mch.IsClosed); Assert.Equal(false, mch.IsInvalid); }
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest2: Verify ctor can set correct handle value for constructor with parameters"); try { IntPtr ptr = new IntPtr(TestLibrary.Generator.GetInt32(-55)); MySafeHandle msf = new MySafeHandle(ptr); if (msf.Handle != ptr) { TestLibrary.TestFramework.LogError("002.1", "Ctor can not set correct handle value"); TestLibrary.TestFramework.LogInformation("WARNING: [LOCAL VARIABLES] msf.Handle = " + msf.Handle.ToString() + ", desiredValue = " + ptr.ToString()); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002.0", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1: Call SafeHandle.SetHandleAsInvalid "); try { MySafeHandle msh = new MySafeHandle(); IntPtr myIptr = new IntPtr(1000); msh.MySetHandle(myIptr); msh.SetHandleAsInvalid(); if (!msh.IsClosed || msh.GetHandle()!=myIptr ) { TestLibrary.TestFramework.LogError("001.1", "SetHandleAsInvalid has error "); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1: Verify the handle is a protected value"); try { MySafeHandle msf = new MySafeHandle(); if (null == msf) { TestLibrary.TestFramework.LogError("001.1", "Failed to allocate a new safe handle instance"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public static void SafeHandle_valid_close() { MySafeHandle mch = new MySafeHandle(new IntPtr(1)); mch.Close(); Assert.True(mch.IsClosed); Assert.False(mch.IsInvalid); Assert.True(mch.IsReleased); }
public static void SafeHandle_SetHandleAsInvalid() { IntPtr ptr = new IntPtr(1); MySafeHandle handle = new MySafeHandle(ptr); handle.SetHandleAsInvalid(); Assert.True(handle.IsClosed); Assert.False(handle.IsInvalid); Assert.False(handle.IsReleased); Assert.Equal(ptr, handle.DangerousGetHandle()); }
public static void SafeHandle_invalid() { MySafeHandle mch = new MySafeHandle(); Assert.False(mch.IsClosed); Assert.True(mch.IsInvalid); Assert.False(mch.IsReleased); mch.Dispose(); Assert.True(mch.IsClosed); Assert.True(mch.IsInvalid); Assert.False(mch.IsReleased); }
public MySafeHandle AllocateHandle() { // Allocate SafeHandle first to avoid failure later. MySafeHandle sh = new MySafeHandle(); RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { MyStruct myStruct = new MyStruct(); NativeAllocateHandle(ref myStruct); sh.SetHandle(myStruct.m_outputHandle); } return sh; }
public static void SafeHandle_invalid() { MySafeHandle mch = new MySafeHandle(); Assert.False(mch.IsClosed); Assert.True(mch.IsInvalid); Assert.False(mch.IsReleased); Assert.Equal(IntPtr.Zero, mch.DangerousGetHandle()); mch.Dispose(); Assert.True(mch.IsClosed); Assert.True(mch.IsInvalid); Assert.False(mch.IsReleased); // Make sure we can dispose multiple times mch.Dispose(); Assert.True(mch.IsClosed); Assert.True(mch.IsInvalid); Assert.False(mch.IsReleased); }
public void MethodWithSafeHandleDangerousGetHandleCall() { MySafeHandle myHandle = new MySafeHandle(); IntPtr handlePtr = myHandle.DangerousGetHandle(); }
internal static extern int GetModuleFileNameEx( MySafeHandle hProcess, IntPtr hModule, StringBuilder lpFilename, int nSize);
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest2: Verify the handle is a protected value, and can set a value"); try { int randValue = TestLibrary.Generator.GetInt32(-55); IntPtr value = new IntPtr(randValue); MySafeHandle msf = new MySafeHandle(value); if (null == msf) { TestLibrary.TestFramework.LogError("002.1", "Failed to allocate a new safe handle instance"); TestLibrary.TestFramework.LogInformation("[LOCAL VARIABLES] randValue = " + randValue); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002.2", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
internal static extern bool IsWow64Process( MySafeHandle hProcess, ref bool Wow64Process);
internal static extern bool OpenProcessToken(MySafeHandle ProcessHandle, UInt32 DesiredAccess, out MySafeHandle TokenHandle);
internal static extern bool GetTokenInformation( MySafeHandle TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, IntPtr TokenInformation, uint TokenInformationLength, out uint ReturnLength);
internal static extern bool Process32Next( MySafeHandle hSnapshot, [MarshalAs(UnmanagedType.Struct)][In][Out] ref ProcessEntry32 lppe);
internal static extern bool Module32Next( MySafeHandle hSnapshot, [MarshalAs(UnmanagedType.Struct)][In][Out] ref MODULEENTRY32 lppe);
public static extern bool QueryFullProcessImageName([In] MySafeHandle hProcess, [In] int dwFlags, [Out] StringBuilder lpExeName, ref int lpdwSize);
internal static extern bool EnumProcessModulesEx( MySafeHandle hProcess, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U4)][Out] IntPtr[] lphModule, uint cb, [MarshalAs(UnmanagedType.U4)] out uint lpcbNeeded, uint dwFilterFlag);
public void Hook(HookType hookType, HookReceived hookReceivedCallback) { _hookReceivedCallback = hookReceivedCallback; _hookId = new MySafeHandle(NativeMethods.SetWindowsHookEx((int)hookType, _hookedProc, IntPtr.Zero, 0)); }
internal static extern uint GetModuleBaseName( MySafeHandle hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In][MarshalAs(UnmanagedType.U4)] int nSize);
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest2: Verify ctor can set correct handle value for constructor with parameters"); try { IntPtr ptr = new IntPtr(TestLibrary.Generator.GetInt32(-55)); MySafeHandle msf = new MySafeHandle(ptr); if (msf.Handle != ptr) { TestLibrary.TestFramework.LogError("002.1", "Ctor can not set correct handle value"); TestLibrary.TestFramework.LogInformation("WARNING: [LOCAL VARIABLES] msf.Handle = " + msf.Handle.ToString() + ", desiredValue = " + ptr.ToString()); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002.0", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }