/////////////////////////////////////////////////////////////////////// public static int WaitAnyEvent( EventWaitHandle[] events, int milliseconds ) { try { if (events != null) { #if !MONO && !MONO_HACKS && (NET_20_SP2 || NET_40) return(EventWaitHandle.WaitAny(events, milliseconds)); #else return(EventWaitHandle.WaitAny(events, milliseconds, false)); #endif } else { TraceOps.DebugTrace( "WaitAnyEvent: invalid event", typeof(ThreadOps).Name, TracePriority.HandleError); } } catch (Exception e) { TraceOps.DebugTrace( e, typeof(ThreadOps).Name, TracePriority.HandleError); } return(WaitHandle.WaitTimeout); }
/////////////////////////////////////////////////////////////////////////////////////////// #region Public Methods public ReturnCode Populate( ref bool returnValue, ref Result error ) { try { returnValue = UnsafeNativeMethods.EnumWindows( EnumWindowCallback, IntPtr.Zero); if (!returnValue) { error = NativeOps.GetErrorMessage(); } return(ReturnCode.Ok); } catch (Exception e) { if (traceException) { TraceOps.DebugTrace( e, typeof(WindowOps).Name, TracePriority.NativeError); } error = e; } return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////////////////////////////// private static ReturnCode WaitForSingleHandle( WaitHandle waitHandle, int milliseconds, bool userInterface, ref uint returnValue ) { ReturnCode code; Result error = null; code = WaitForSingleHandle( waitHandle, milliseconds, userInterface, ref returnValue, ref error); if (code != ReturnCode.Ok) { DebugOps.Complain(code, error); } if (traceWait) { TraceOps.DebugTrace(String.Format( "WaitForSingleHandle: exited, waitHandle = {0}, " + "milliseconds = {1}, userInterface = {2}, " + "returnValue = {3}, code = {4}, error = {5}", FormatOps.DisplayWaitHandle(waitHandle), milliseconds, userInterface, returnValue, code, FormatOps.WrapOrNull( true, true, error)), typeof(WindowOps).Name, TracePriority.NativeDebug); } return(code); }
/////////////////////////////////////////////////////////////////////// public static bool WaitEvent( EventWaitHandle @event ) { try { if (@event != null) { return(@event.WaitOne()); } else { TraceOps.DebugTrace( "WaitEvent: invalid event", typeof(ThreadOps).Name, TracePriority.HandleError); } } catch (Exception e) { TraceOps.DebugTrace( e, typeof(ThreadOps).Name, TracePriority.HandleError); } return(false); }
/////////////////////////////////////////////////////////////////////// public static bool WaitEvent( EventWaitHandle @event, int milliseconds ) { try { if (@event != null) { #if !MONO && !MONO_HACKS && (NET_20_SP2 || NET_40) return(@event.WaitOne(milliseconds)); #else return(@event.WaitOne(milliseconds, false)); #endif } else { TraceOps.DebugTrace( "WaitEvent: invalid events", typeof(ThreadOps).Name, TracePriority.HandleError); } } catch (Exception e) { TraceOps.DebugTrace( e, typeof(ThreadOps).Name, TracePriority.HandleError); } return(false); }
/////////////////////////////////////////////////////////////////////// public static EventWaitHandle OpenEvent( string name ) { EventWaitHandle @event = null; try { @event = EventWaitHandle.OpenExisting(name); } catch (Exception e) { TraceOps.DebugTrace( e, typeof(ThreadOps).Name, TracePriority.HandleError); } #if DEBUG && VERBOSE TraceOps.DebugTrace(String.Format( "OpenEvent: {0}, name = {1}", (@event != null) ? "success" : "failure", FormatOps.WrapOrNull(name)), typeof(ThreadOps).Name, TracePriority.EventDebug); #endif return(@event); }
/////////////////////////////////////////////////////////////////////// public static void CloseEvent( ref EventWaitHandle @event ) { try { if (@event != null) { @event.Close(); @event = null; } else { TraceOps.DebugTrace( "CloseEvent: invalid event", typeof(ThreadOps).Name, TracePriority.HandleError); } } catch (Exception e) { TraceOps.DebugTrace( e, typeof(ThreadOps).Name, TracePriority.HandleError); } }
/////////////////////////////////////////////////////////////////////// public static X509Certificate2 GetCertificate2( Assembly assembly ) { if (assembly != null) { X509Certificate certificate = null; if (GetCertificate(assembly, ref certificate) == ReturnCode.Ok) { try { return((certificate != null) ? new X509Certificate2(certificate) : null); } catch (Exception e) { // // NOTE: Nothing we can do here except log the failure. // TraceOps.DebugTrace( e, typeof(AssemblyOps).Name, TracePriority.SecurityError); } } } return(null); }
/////////////////////////////////////////////////////////////////////// private /* protected virtual */ void Dispose( bool disposing ) { TraceOps.DebugTrace(String.Format( "Dispose: disposing = {0}, interpreter = {1}, disposed = {2}", disposing, FormatOps.InterpreterNoThrow(interpreter), disposed), typeof(VariableContext).Name, TracePriority.CleanupDebug); if (!disposed) { if (disposing) { //////////////////////////////////// // dispose managed resources here... //////////////////////////////////// Free(true); } ////////////////////////////////////// // release unmanaged resources here... ////////////////////////////////////// disposed = true; } }
/////////////////////////////////////////////////////////////////////// public static ReturnCode GetCertificate( byte[] assemblyBytes, ref X509Certificate certificate, ref Result error ) { if (assemblyBytes != null) { try { certificate = new X509Certificate(assemblyBytes); return(ReturnCode.Ok); } catch (Exception e) { error = e; } } else { error = "invalid assembly bytes"; } TraceOps.DebugTrace(String.Format( "GetCertificate: query failure, error = {0}", FormatOps.WrapOrNull( true, true, error)), typeof(AssemblyOps).Name, TracePriority.SecurityError); return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////// private /* protected virtual */ void Dispose( bool disposing ) { TraceOps.DebugTrace(String.Format( "Dispose: called, disposing = {0}, disposed = {1}", disposing, disposed), typeof(EngineThread).Name, TracePriority.CleanupDebug); if (!disposed) { if (disposing) { //////////////////////////////////// // dispose managed resources here... //////////////////////////////////// interpreter = null; /* NOT OWNED, DO NOT DISPOSE. */ threadStart = null; parameterizedThreadStart = null; thread = null; /* NOT OWNED, DO NOT DISPOSE. */ } ////////////////////////////////////// // release unmanaged resources here... ////////////////////////////////////// disposed = true; } }
/////////////////////////////////////////////////////////////////////// public void Report() { TraceOps.DebugTrace(String.Format( "Report: completed operation {0} in {1}", FormatOps.WrapOrNull(operation), this), typeof(PerformanceClientData).Name, TracePriority.TestDebug); }
private static string Trace( Configuration configuration, Exception exception, string category ) { return(TraceOps.Trace(configuration, exception, category)); }
private static string Trace( Configuration configuration, string message, string category ) { return(TraceOps.Trace(configuration, message, category)); }
/////////////////////////////////////////////////////////////////////// #region Private Methods private static bool IsUsable( string version ) { if (version == null) { TraceOps.DebugTrace( "IsUsable: invalid version string", typeof(NativeUtility).Name, TracePriority.NativeError); return(false); } if (version.IndexOf( optionUse32BitSizeT, StringOps.SystemStringComparisonType) == Index.Invalid) { TraceOps.DebugTrace(String.Format( "IsUsable: missing option {0}", FormatOps.WrapOrNull(optionUse32BitSizeT)), typeof(NativeUtility).Name, TracePriority.NativeError); return(false); } #if NATIVE_UTILITY_BSTR if (version.IndexOf( optionUseSysStringLen, StringOps.SystemStringComparisonType) == Index.Invalid) { TraceOps.DebugTrace(String.Format( "IsUsable: missing option {0}", FormatOps.WrapOrNull(optionUseSysStringLen)), typeof(NativeUtility).Name, TracePriority.NativeError); return(false); } #else if (version.IndexOf( optionUseSysStringLen, StringOps.SystemStringComparisonType) != Index.Invalid) { TraceOps.DebugTrace(String.Format( "IsUsable: mismatched option {0}", FormatOps.WrapOrNull(optionUseSysStringLen)), typeof(NativeUtility).Name, TracePriority.NativeError); return(false); } #endif return(true); }
/////////////////////////////////////////////////////////////////////// #region Public Methods public static bool IsFileTrusted( Configuration configuration, string fileName, IntPtr fileHandle, bool userInterface, bool userPrompt, bool revocation, bool install, ref string error ) { #if !DEBUG /* !SUCCESS */ int returnValue = (int)UnsafeNativeMethods.ERROR_SUCCESS + 1; string localError = null; if ((IsFileTrusted( fileName, fileHandle, userInterface, userPrompt, revocation, install, ref returnValue, ref localError)) && (returnValue == UnsafeNativeMethods.ERROR_SUCCESS)) { return(true); } else { if (localError != null) { error = localError; } else if (returnValue != UnsafeNativeMethods.ERROR_SUCCESS) { error = String.Format( "WinVerifyTrust() failed with error 0x{0:X}.", returnValue); } return(false); } #else // // NOTE: Emit a log entry so that the user knows for sure // that we did NOT actually verify the file trust. // TraceOps.Trace(configuration, String.Format( "File \"{0}\" certificate unchecked: " + "WinVerifyTrust use is disabled.", fileName), typeof(WinTrustEx).Name); // // NOTE: In-development version, fake it. We can do this // because DEBUG builds are never officially released. // return(true); #endif }
/////////////////////////////////////////////////////////////////////// /* System.Threading.ThreadStart */ public void ThreadStart() { CheckDisposed(); try { #if NATIVE && WINDOWS RuntimeOps.RefreshNativeStackPointers(); #endif if (threadStart != null) { threadStart(); } else if (parameterizedThreadStart != null) { parameterizedThreadStart(null); } else { TraceOps.DebugTrace( "ThreadStart: no delegates available", typeof(EngineThread).Name, TracePriority.ThreadError); } } catch (ThreadAbortException e) { Thread.ResetAbort(); TraceOps.DebugTrace( e, typeof(EngineThread).Name, TracePriority.ThreadError); } catch (ThreadInterruptedException e) { TraceOps.DebugTrace( e, typeof(EngineThread).Name, TracePriority.ThreadError); } catch (Exception e) { TraceOps.DebugTrace( e, typeof(EngineThread).Name, TracePriority.ThreadError); } finally { if (interpreter != null) { interpreter.MaybeDisposeThread(); } } }
/////////////////////////////////////////////////////////////////////// private static bool CheckAndMaybeModifyReferenceCount( bool?increment, ref int referenceCount ) { try { string variable = null; string value = null; GetEnvironmentVariableAndValue(ref variable, ref value); int localReferenceCount = 0; if (!String.IsNullOrEmpty(variable) && ((value == null) || (Value.GetInteger2( value, ValueFlags.AnyInteger, null, ref localReferenceCount) == ReturnCode.Ok))) { if (increment != null) { if ((bool)increment) { localReferenceCount++; } else { localReferenceCount--; } if (localReferenceCount > 0) { CommonOps.Environment.SetVariable( variable, localReferenceCount.ToString()); } else { CommonOps.Environment.UnsetVariable(variable); } } referenceCount = localReferenceCount; return(true); } } catch (Exception e) { TraceOps.DebugTrace( e, typeof(ConsoleOps).Name, TracePriority.HostError); } return(false); }
/////////////////////////////////////////////////////////////////////////////////////////// #region Private Methods private bool EnumWindowCallback( IntPtr hWnd, IntPtr lParam ) { try { string text = null; int length = UnsafeNativeMethods.GetWindowTextLength(hWnd); if (length > 0) { length++; /* NUL terminator */ buffer = StringOps.NewStringBuilder(buffer, length); if (UnsafeNativeMethods.GetWindowText( hWnd, buffer, length) > 0) { text = buffer.ToString(); } } string @class = null; length = UnsafeNativeMethods.MAX_CLASS_NAME; buffer = StringOps.NewStringBuilder(buffer, length); if (UnsafeNativeMethods.GetClassName( hWnd, buffer, length) > 0) { @class = buffer.ToString(); } windows[hWnd] = new Pair <string>(@class, text); return(true); } catch (Exception e) { if (traceException) { // // NOTE: Nothing much we can do here except log the // failure. // TraceOps.DebugTrace( e, typeof(EnumWindowCallback).Name, TracePriority.NativeError); } } return(false); }
/////////////////////////////////////////////////////////////////////// private /* protected virtual */ void Dispose( bool disposing ) { TraceOps.DebugTrace(String.Format( "Dispose: disposing = {0}, interpreter = {1}, disposed = {2}", disposing, FormatOps.InterpreterNoThrow(interpreter), disposed), typeof(InteractiveContext).Name, TracePriority.CleanupDebug); if (!disposed) { if (disposing) { //////////////////////////////////// // dispose managed resources here... //////////////////////////////////// interpreter = null; /* NOT OWNED: Do not dispose. */ threadId = 0; /////////////////////////////////////////////////////////// interactive = false; interactiveInput = null; previousInteractiveInput = null; interactiveMode = null; activeInteractiveLoops = 0; totalInteractiveLoops = 0; interactiveLoopData = null; interactiveCommandCallback = null; #if HISTORY historyLoadData = null; historySaveData = null; historyInfoFilter = null; historyLoadFilter = null; historySaveFilter = null; historyFileName = null; #endif } ////////////////////////////////////// // release unmanaged resources here... ////////////////////////////////////// disposed = true; } }
/////////////////////////////////////////////////////////////////////// #region Public Methods public static bool IsStrongNameSigned( Configuration configuration, string fileName, bool force, ref string error ) { #if !DEBUG bool returnValue = false; bool verified = false; string localError = null; if ((IsStrongNameSigned( fileName, force, ref returnValue, ref verified, ref localError)) && returnValue && verified) { return(true); } else { if (localError != null) { error = localError; } else { error = "StrongNameSignatureVerificationEx() failed."; } return(false); } #else // // NOTE: Emit a log entry so that the user knows for sure // that we did NOT actually verify the strong name // signature. // TraceOps.Trace(configuration, String.Format( "File \"{0}\" strong name unchecked: " + "StrongNameSignatureVerificationEx use is disabled.", fileName), typeof(StrongNameEx).Name); // // NOTE: In-development version, fake it. We can do this // because DEBUG builds are never officially released. // return(true); #endif }
/////////////////////////////////////////////////////////////////////// private static bool UnloadNativeLibrary( Interpreter interpreter /* NOT USED */ ) { lock (syncRoot) /* TRANSACTIONAL */ { if (nativeModule == IntPtr.Zero) { return(true); } try { UnsetNativeDelegates(); int lastError; if (NativeOps.FreeLibrary( nativeModule, out lastError)) /* throw */ { nativeModule = IntPtr.Zero; nativeFileName = null; TraceOps.DebugTrace( "UnloadNativeLibrary: successfully unloaded", typeof(NativeUtility).Name, TracePriority.NativeDebug); return(true); } else { TraceOps.DebugTrace(String.Format( "FreeLibrary(0x{1:X}) failed with error {0}: {2}", lastError, nativeModule, NativeOps.GetDynamicLoadingError(lastError).Trim()), typeof(NativeUtility).Name, TracePriority.NativeError); } } catch (Exception e) { TraceOps.DebugTrace( e, typeof(NativeUtility).Name, TracePriority.NativeError); } return(false); } }
/////////////////////////////////////////////////////////////////////// public static ReturnCode GetHash( Assembly assembly, ref Hash hash, ref Result error ) { if (assembly != null) { Evidence evidence = assembly.Evidence; if (evidence != null) { try { foreach (object item in evidence) { if (item is Hash) { hash = (Hash)item; return(ReturnCode.Ok); } } error = "no hash found"; return(ReturnCode.Error); } catch (Exception e) { error = e; } } else { error = "invalid evidence"; } } else { error = "invalid assembly"; } TraceOps.DebugTrace(String.Format( "GetHash: assembly {0} query failure, error = {1}", FormatOps.WrapOrNull(assembly), FormatOps.WrapOrNull(true, true, error)), typeof(AssemblyOps).Name, TracePriority.SecurityError); return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////// public static ReturnCode GetCertificate2( Assembly assembly, bool strict, ref X509Certificate2 certificate2, ref Result error ) { X509Certificate certificate = null; if (GetCertificate(assembly, ref certificate, ref error) == ReturnCode.Ok) { if (certificate != null) { try { certificate2 = new X509Certificate2(certificate); return(ReturnCode.Ok); } catch (Exception e) { error = e; } } else if (!strict) { certificate2 = null; return(ReturnCode.Ok); } else { error = "invalid certificate"; } } #if DEBUG if (!GlobalState.IsAssembly(assembly)) #endif { TraceOps.DebugTrace(String.Format( "GetCertificate2: assembly {0} query failure, error = {1}", FormatOps.WrapOrNull(assembly), FormatOps.WrapOrNull(true, true, error)), typeof(AssemblyOps).Name, TracePriority.SecurityError); } return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////////////////////////////// public static ReturnCode ProcessEvents( Interpreter interpreter, /* NOT USED */ ref Result error ) { try { #if NATIVE && WINDOWS // // NOTE: If this thread has a message queue and there // appears to be anything in it, process it now. // if (PlatformOps.IsWindowsOperatingSystem()) { if (HasMessageQueue( GlobalState.GetCurrentNativeThreadId(), ref error)) { uint flags = UnsafeNativeMethods.QS_ALLINPUT; if (UnsafeNativeMethods.GetQueueStatus(flags) != 0) #endif DoEvents(); #if NATIVE && WINDOWS } } else { DoEvents(); } #endif return(ReturnCode.Ok); } catch (Exception e) { if (traceException) { TraceOps.DebugTrace( e, typeof(WindowOps).Name, TracePriority.NativeError); } error = e; } return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////// private static void WriteCoreNoThrow( string value ) { try { WriteCore(value); /* throw */ } catch (Exception e) { TraceOps.DebugTrace( e, typeof(ConsoleOps).Name, TracePriority.HostError); } }
/////////////////////////////////////////////////////////////////////// #region Private Methods private static ReturnCode IsStrongNameVerified( string fileName, bool force, ref bool returnValue, ref bool verified, ref Result error ) { if (String.IsNullOrEmpty(fileName)) { error = "invalid file name"; return(ReturnCode.Error); } #if WINDOWS && !MONO if (!PlatformOps.IsWindowsOperatingSystem()) { error = "not supported on this operating system"; return(ReturnCode.Error); } try { returnValue = UnsafeNativeMethods.StrongNameSignatureVerificationEx( fileName, force, ref verified); return(ReturnCode.Ok); } catch (Exception e) { error = e; } #else error = "not implemented"; #endif TraceOps.DebugTrace(String.Format( "IsStrongNameVerified: file {0} verification " + "failure, force = {1}, returnValue = {2}, " + "verified = {3}, error = {4}", FormatOps.WrapOrNull(fileName), force, returnValue, verified, FormatOps.WrapOrNull(error)), typeof(SecurityOps).Name, TracePriority.SecurityError); return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////// #region Private Windows-Specific Methods #if WINDOWS private static ReturnCode WindowsIsAdministrator( ref bool administrator, ref Result error ) { try { // // NOTE: Are we running on Windows 2000 SP4 or higher? // if (PlatformOps.CheckVersion(PlatformID.Win32NT, 5, 0, 4, 0)) { // // HACK: Use a "documented" function for Windows // 2000 SP4+, Windows XP, and Vista (this // function used to be undocumented). // administrator = UnsafeNativeMethods.IsUserAnAdmin(); } else { // // HACK: Use a different undocumented function for // Windows NT and Windows 2000 RTM to SP3. // uint reserved2 = 0; administrator = UnsafeNativeMethods.IsNTAdmin( 0, ref reserved2); } return(ReturnCode.Ok); } catch (Exception e) { error = e; } TraceOps.DebugTrace(String.Format( "WindowsIsAdministrator: administrator = {0}, error = {1}", administrator, FormatOps.WrapOrNull(error)), typeof(SecurityOps).Name, TracePriority.SecurityError); return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////// public static ReturnCode GetCertificate( Assembly assembly, ref X509Certificate certificate, ref Result error ) { if (assembly != null) { Module module = assembly.ManifestModule; if (module != null) { try { certificate = module.GetSignerCertificate(); return(ReturnCode.Ok); } catch (Exception e) { error = e; } } else { error = "invalid module"; } } else { error = "invalid assembly"; } #if DEBUG if (!GlobalState.IsAssembly(assembly)) #endif { TraceOps.DebugTrace(String.Format( "GetCertificate: assembly {0} query failure, error = {1}", FormatOps.WrapOrNull(assembly), FormatOps.WrapOrNull(true, true, error)), typeof(AssemblyOps).Name, TracePriority.SecurityError); } return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////// public static EventWaitHandle CreateEvent( bool automatic ) { try { return(new EventWaitHandle(false, automatic ? EventResetMode.AutoReset : EventResetMode.ManualReset)); } catch (Exception e) { TraceOps.DebugTrace( e, typeof(ThreadOps).Name, TracePriority.HandleError); throw; } }