/////////////////////////////////////////////////////////////////////// #region Public Methods public IntPtr GetModule( bool load ) { if (load) { return(module); } return(NativeOps.IsValidHandle(module) ? NativeOps.IntPtrOne : IntPtr.Zero); }
/////////////////////////////////////////////////////////////////////// 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); }
/////////////////////////////////////////////////////////////////////// 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); }