/////////////////////////////////////////////////////////////////////// public static ReturnCode Wait( Interpreter interpreter, long microseconds, bool timeout, bool strict, ref Result error ) /* THREAD-SAFE */ { ReturnCode code = ReturnCode.Ok; if (interpreter != null) { int waitCount; if ((waitCount = interpreter.EnterWait()) > 0) { if (microseconds == 0) { #if WINFORMS // // NOTE: If necessary, process all Windows messages // from the queue. // if (!strict) { code = WindowOps.ProcessEvents( interpreter, ref error); } if (code == ReturnCode.Ok) #endif { // // NOTE: Yield to other running threads. This // also gives them an opportunity to cancel // the script in progress on this thread. // HostOps.Yield(); } } else { // // NOTE: Keep track of how many iterations through // the loop we take. // int iterations = 0; // // HACK: Account for our processing overhead; use half // of the requested delay. // int milliseconds = ConversionOps.ToInt( PerformanceOps.GetMilliseconds(microseconds) / WaitDivisor); if (milliseconds < 0) { milliseconds = 0; } if (milliseconds > WaitMaximumSleepTime) { milliseconds = WaitMaximumSleepTime; } // // NOTE: For more precise timing, use the high-resolution // CPU performance counter. // long startCount = PerformanceOps.GetCount(); // // BUGFIX: Make sure the slop time does not exceed the // actual wait. // long slopMicroseconds = Math.Min( microseconds / WaitSlopDivisor, WaitSlopMinimumTime); // // NOTE: Delay for approximately the specified number of // microseconds, optionally timing out if we cannot // obtain the interpreter lock before the time period // elapses. // while (((code = Interpreter.EventReady(interpreter, timeout ? milliseconds : _Timeout.Infinite, ref error)) == ReturnCode.Ok) && !PerformanceOps.HasElapsed(startCount, microseconds, slopMicroseconds)) { #if WINFORMS if (!strict) { code = WindowOps.ProcessEvents(interpreter, ref error); if (code != ReturnCode.Ok) { break; } } #endif HostOps.SleepOrMaybeComplain(interpreter, milliseconds); iterations++; } long stopCount = PerformanceOps.GetCount(); double elapsedMicroseconds = PerformanceOps.GetMicroseconds( startCount, stopCount, 1); TraceOps.DebugTrace(String.Format( "Wait: code = {0}, iterations = {1}, microseconds = {2}, " + "elapsedMicroseconds = {3}, sleepMilliseconds = {4}, " + "slopMicroseconds = {5}, differenceMicroseconds = {6}, " + "waitCount = {7}, error = {8}", code, iterations, microseconds, elapsedMicroseconds, milliseconds, slopMicroseconds, elapsedMicroseconds - (double)microseconds, waitCount, FormatOps.WrapOrNull(true, true, error)), typeof(EventOps).Name, TracePriority.EventDebug); } /* IGNORED */ interpreter.ExitWait(); } else { error = "wait subsystem locked"; code = ReturnCode.Error; } } else { error = "invalid interpreter"; code = ReturnCode.Error; } return(code); }
/////////////////////////////////////////////////////////////////////// private static ReturnCode IsFileTrusted( string fileName, IntPtr fileHandle, bool userInterface, bool userPrompt, bool revocation, bool install, ref int returnValue, ref Result error ) { if (String.IsNullOrEmpty(fileName)) { error = "invalid file name"; return(ReturnCode.Error); } #if WINDOWS if (!PlatformOps.IsWindowsOperatingSystem()) { error = "not supported on this operating system"; return(ReturnCode.Error); } try { UnsafeNativeMethods.WINTRUST_FILE_INFO file = new UnsafeNativeMethods.WINTRUST_FILE_INFO(); file.cbStruct = (uint)Marshal.SizeOf( typeof(UnsafeNativeMethods.WINTRUST_FILE_INFO)); file.pcwszFilePath = fileName; file.hFile = fileHandle; file.pgKnownSubject = IntPtr.Zero; IntPtr pFile = IntPtr.Zero; try { pFile = Marshal.AllocCoTaskMem((int)file.cbStruct); if (pFile != IntPtr.Zero) { Marshal.StructureToPtr(file, pFile, false); UnsafeNativeMethods.WINTRUST_DATA winTrustData = new UnsafeNativeMethods.WINTRUST_DATA(); winTrustData.cbStruct = (uint)Marshal.SizeOf( typeof(UnsafeNativeMethods.WINTRUST_DATA)); winTrustData.pPolicyCallbackData = IntPtr.Zero; winTrustData.pSIPClientData = IntPtr.Zero; winTrustData.dwUIChoice = userInterface && userPrompt ? UnsafeNativeMethods.WTD_UI_ALL : UnsafeNativeMethods.WTD_UI_NONE; winTrustData.fdwRevocationChecks = revocation ? UnsafeNativeMethods.WTD_REVOKE_WHOLECHAIN : UnsafeNativeMethods.WTD_REVOKE_NONE; winTrustData.dwUnionChoice = UnsafeNativeMethods.WTD_CHOICE_FILE; winTrustData.pFile = pFile; winTrustData.dwStateAction = UnsafeNativeMethods.WTD_STATEACTION_IGNORE; winTrustData.hWVTStateData = IntPtr.Zero; winTrustData.pwszURLReference = null; winTrustData.dwProvFlags = UnsafeNativeMethods.WTD_SAFER_FLAG; winTrustData.dwUIContext = install ? UnsafeNativeMethods.WTD_UICONTEXT_INSTALL : UnsafeNativeMethods.WTD_UICONTEXT_EXECUTE; IntPtr hWnd = userInterface ? WindowOps.GetInteractiveHandle() : INVALID_HANDLE_VALUE; Guid actionId = GetActionId(); returnValue = UnsafeNativeMethods.WinVerifyTrust( hWnd, actionId, ref winTrustData); return(ReturnCode.Ok); } else { error = "out of memory"; } } finally { if (pFile != IntPtr.Zero) { Marshal.FreeCoTaskMem(pFile); pFile = IntPtr.Zero; } } } catch (Exception e) { error = e; } #else error = "not implemented"; #endif TraceOps.DebugTrace(String.Format( "IsFileTrusted: file {0} trust failure, " + "userInterface = {1}, revocation = {2}, " + "install = {3}, returnValue = {4}, error = {5}", FormatOps.WrapOrNull(fileName), userInterface, revocation, install, returnValue, FormatOps.WrapOrNull(error)), typeof(SecurityOps).Name, TracePriority.SecurityError); return(ReturnCode.Error); }