/////////////////////////////////////////////////////////////////////////////////////////// #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); }
/////////////////////////////////////////////////////////////////////// #region Public Methods public IntPtr GetModule( bool load ) { if (load) { return(module); } return(NativeOps.IsValidHandle(module) ? NativeOps.IntPtrOne : IntPtr.Zero); }
/////////////////////////////////////////////////////////////////////// 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 SimulateReturnKey( IntPtr handle, ref Result error ) { try { if (handle != IntPtr.Zero) { UIntPtr virtualKey = new UIntPtr(UnsafeNativeMethods.VK_RETURN); if (UnsafeNativeMethods.PostMessage( handle, UnsafeNativeMethods.WM_KEYDOWN, virtualKey, IntPtr.Zero)) { if (UnsafeNativeMethods.PostMessage( handle, UnsafeNativeMethods.WM_KEYUP, virtualKey, IntPtr.Zero)) { return(ReturnCode.Ok); } } error = NativeOps.GetErrorMessage(); } else { error = "invalid window handle"; } } catch (Exception e) { if (traceException) { TraceOps.DebugTrace( e, typeof(WindowOps).Name, TracePriority.NativeError); } error = e; } return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////////////////////////////// public static ReturnCode GetWindowThreadProcessId( IntPtr handle, ref int processId, ref int threadId, ref Result error ) { try { if (handle != IntPtr.Zero) { int localProcessId = 0; int localThreadId = UnsafeNativeMethods.GetWindowThreadProcessId( handle, ref localProcessId); if (localThreadId != 0) { processId = localProcessId; threadId = localThreadId; return(ReturnCode.Ok); } error = NativeOps.GetErrorMessage(); } else { error = "invalid window handle"; } } catch (Exception e) { if (traceException) { TraceOps.DebugTrace( e, typeof(WindowOps).Name, TracePriority.NativeError); } error = e; } return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////////////////////////////// public static ReturnCode CloseWindow( IntPtr handle, ref bool returnValue, ref Result error ) { try { if (handle != IntPtr.Zero) { IntPtr result = UnsafeNativeMethods.SendMessage( handle, UnsafeNativeMethods.WM_CLOSE, UIntPtr.Zero, IntPtr.Zero); returnValue = (result == IntPtr.Zero); if (returnValue) { return(ReturnCode.Ok); } else { error = NativeOps.GetErrorMessage(); } } else { error = "invalid window handle"; } } catch (Exception e) { if (traceException) { TraceOps.DebugTrace( e, typeof(WindowOps).Name, TracePriority.NativeError); } error = e; } return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////////////////////////////// #if NATIVE && WINDOWS && WINFORMS private static bool HasMessageQueue( int threadId, ref Result error ) { try { if (UnsafeNativeMethods.PostThreadMessage(threadId, UnsafeNativeMethods.WM_NULL, UIntPtr.Zero, IntPtr.Zero)) { return(true); } else { int lastError = Marshal.GetLastWin32Error(); if (lastError == UnsafeNativeMethods.ERROR_INVALID_THREAD_ID) { return(false); } error = NativeOps.GetErrorMessage(lastError); } } catch (Exception e) { if (traceException) { TraceOps.DebugTrace( e, typeof(WindowOps).Name, TracePriority.NativeError); } error = e; } return(false); }
/////////////////////////////////////////////////////////////////////////////////////////////// #if NATIVE && WINDOWS public static ReturnCode GetLastInputTickCount( ref Result result ) { try { UnsafeNativeMethods.LASTINPUTINFO lastInputInfo = new UnsafeNativeMethods.LASTINPUTINFO(); lastInputInfo.cbSize = (uint)Marshal.SizeOf( typeof(UnsafeNativeMethods.LASTINPUTINFO)); if (UnsafeNativeMethods.GetLastInputInfo( ref lastInputInfo)) { result = lastInputInfo.dwTime; return(ReturnCode.Ok); } else { result = NativeOps.GetErrorMessage(); } } catch (Exception e) { if (traceException) { TraceOps.DebugTrace( e, typeof(WindowOps).Name, TracePriority.NativeError); } result = e; } return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////// public ReturnCode VerifyModule( ref Result error ) { if (String.IsNullOrEmpty(fileName)) { error = "invalid Tcl native module file name"; return(ReturnCode.Error); } if (!NativeOps.IsValidHandle(module)) { error = "invalid Tcl native module handle"; return(ReturnCode.Error); } // // HACK: We cannot actually verify the native module handle on any // non-Windows operating system. // if (!PlatformOps.IsWindowsOperatingSystem()) { return(ReturnCode.Ok); } try { IntPtr newModule = NativeOps.GetModuleHandle(fileName); if (newModule == IntPtr.Zero) { error = String.Format( "bad Tcl native module handle {0}, file name {1} is " + "no longer loaded", module, FormatOps.WrapOrNull( fileName)); TraceOps.DebugTrace(String.Format( "VerifyModule: {0}", FormatOps.WrapOrNull(error)), typeof(TclModule).Name, TracePriority.NativeError); return(ReturnCode.Error); } if (newModule != module) { // // NOTE: This situation should really never happen. If it // does, that indicates that the native Tcl module // was unloaded and then reloaded out from under the // native Tcl integration subsystem. // error = String.Format( "bad Tcl native module handle {0}, got {1} for file " + "name {2}", module, newModule, FormatOps.WrapOrNull( fileName)); TraceOps.DebugTrace(String.Format( "VerifyModule: {0}", FormatOps.WrapOrNull(error)), typeof(TclModule).Name, TracePriority.NativeError); return(ReturnCode.Error); } return(ReturnCode.Ok); } catch (Exception e) { error = e; } return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////// #region Public Methods (Internal Use Only) public ReturnCode UnloadNoThrow( /* EXEMPT: object-15.11 */ ref int loaded, ref Result error ) { // CheckDisposed(); /* EXEMPT */ lock (syncRoot) /* TRANSACTIONAL */ { // // NOTE: If the module was already loaded previously, // do nothing. // if (module == IntPtr.Zero) { return(ReturnCode.Ok); } // // NOTE: If there are still outstanding references to // the native module, do nothing. // if (Interlocked.Decrement(ref referenceCount) > 0) { return(ReturnCode.Ok); } // // NOTE: If the native module has been locked in place // (because it cannot be cleanly unloaded?), then // leave it alone. // if (FlagOps.HasFlags( flags, ModuleFlags.NoUnload, true)) { return(ReturnCode.Ok); } try { int lastError; if (NativeOps.FreeLibrary( module, out lastError)) /* throw */ { Interlocked.Decrement(ref loaded); module = IntPtr.Zero; return(ReturnCode.Ok); } else { error = String.Format( "FreeLibrary(0x{1:X}) failed with error {0}: {2}", lastError, module, NativeOps.GetDynamicLoadingError( lastError)); } } catch (Exception e) { error = e; } } return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////// public ReturnCode Load( ref int loaded, ref Result error ) { CheckDisposed(); lock (syncRoot) /* TRANSACTIONAL */ { try { if (module != IntPtr.Zero) { return(ReturnCode.Ok); } if (String.IsNullOrEmpty(fileName)) { error = "invalid file name"; return(ReturnCode.Error); } int lastError; module = NativeOps.LoadLibrary( fileName, out lastError); /* throw */ if (NativeOps.IsValidHandle(module)) { Interlocked.Increment(ref loaded); return(ReturnCode.Ok); } else { error = String.Format( "LoadLibrary({1}) failed with error {0}: {2}", lastError, FormatOps.WrapOrNull(fileName), NativeOps.GetDynamicLoadingError(lastError)); } } catch (Exception e) { error = e; } finally { // // NOTE: If the module handle is valid then we know // the module was loaded successfully -OR- was // already loaded; therefore, increment the // reference count. // if (module != IntPtr.Zero) { Interlocked.Increment(ref referenceCount); } } } return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////// private ReturnCode PrivateResolve( ref Result error, ref Exception exception ) { lock (syncRoot) /* TRANSACTIONAL */ { if (type == null) { error = "invalid type"; return(ReturnCode.Error); } if (!ConversionOps.IsDelegateType(type, false)) { error = "type is not a delegate type"; return(ReturnCode.Error); } if (module == null) { error = "invalid module"; return(ReturnCode.Error); } if (String.IsNullOrEmpty(functionName)) { error = "invalid export name"; return(ReturnCode.Error); } if (module.Load(ref moduleLoaded, ref error) == ReturnCode.Ok) { try { int lastError; address = NativeOps.GetProcAddress( module.Module, functionName, out lastError); /* throw */ if (address != IntPtr.Zero) { // // NOTE: The GetDelegateForFunctionPointer method // of the Marshal class is how we get the // delegate we need to invoke the library // function itself and this is why we went // through all the trouble to creating and // populating the delegate type dynamically. // To see exactly how this is accomplished, // please refer to CreateNativeDelegateType // in DelegateOps). // @delegate = Marshal.GetDelegateForFunctionPointer( address, type); /* throw */ return(ReturnCode.Ok); } else { error = String.Format( "GetProcAddress({1}, \"{2}\") failed with " + "error {0}: {3}", lastError, module, functionName, NativeOps.GetDynamicLoadingError( lastError)); } } catch (Exception e) { error = e; exception = e; } } } return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////// private static bool LoadNativeLibrary( Interpreter interpreter ) { lock (syncRoot) /* TRANSACTIONAL */ { if (nativeModule != IntPtr.Zero) { return(true); } try { string fileName = GetNativeLibraryFileName(interpreter); if (!String.IsNullOrEmpty(fileName)) { TraceOps.DebugTrace(String.Format( "LoadNativeLibrary: using file name {0}", FormatOps.WrapOrNull(fileName)), typeof(NativeUtility).Name, TracePriority.NativeDebug); } else { TraceOps.DebugTrace(String.Format( "LoadNativeLibrary: file name {0} is invalid", FormatOps.WrapOrNull(fileName)), typeof(NativeUtility).Name, TracePriority.NativeError); return(false); } // // NOTE: Check if the native library file name actually // exists. If not, do nothing and return failure // after tracing the issue. // if (!File.Exists(fileName)) { TraceOps.DebugTrace(String.Format( "LoadNativeLibrary: file name {0} does not exist", FormatOps.WrapOrNull(fileName)), typeof(NativeUtility).Name, TracePriority.NativeError); return(false); } // // BUGFIX: Stop loading "untrusted" native libraries // when running with a "trusted" core library. // if (!RuntimeOps.ShouldLoadNativeLibrary(fileName)) { TraceOps.DebugTrace(String.Format( "LoadNativeLibrary: file name {0} is untrusted", FormatOps.WrapOrNull(fileName)), typeof(NativeUtility).Name, TracePriority.NativeError); return(false); } int lastError; nativeModule = NativeOps.LoadLibrary( fileName, out lastError); /* throw */ if (nativeModule != IntPtr.Zero) { InitializeNativeDelegates(true); Result error = null; if (SetNativeDelegates(ref error)) { nativeFileName = fileName; TraceOps.DebugTrace( "LoadNativeLibrary: successfully loaded", typeof(NativeUtility).Name, TracePriority.NativeDebug); return(true); } else { TraceOps.DebugTrace(String.Format( "LoadNativeLibrary: file name {0} delegate " + "setup error: {1}", FormatOps.WrapOrNull(fileName), error), typeof(NativeUtility).Name, TracePriority.NativeError); /* IGNORED */ UnloadNativeLibrary(interpreter); } } else { TraceOps.DebugTrace(String.Format( "LoadLibrary({1}) failed with error {0}: {2}", lastError, FormatOps.WrapOrNull(fileName), NativeOps.GetDynamicLoadingError(lastError).Trim()), typeof(NativeUtility).Name, TracePriority.NativeError); } } catch (Exception e) { TraceOps.DebugTrace( e, typeof(NativeUtility).Name, TracePriority.NativeError); } return(false); } }