Exemple #1
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (!disposed)
            {
                disposed = true;

                if (handle != null && !handle.IsClosed)
                {
                    handle.Dispose();
                    handle = null;
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Loads the User Instance dll.
        /// </summary>
        private bool LoadUserInstanceDll()
        {
            long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("<sc.SNI.LocalDB.Windows.LoadUserInstanceDll |SNI>");

            try
            {
                // Check in a non thread-safe way if the handle is already set for performance.
                if (_sqlUserInstanceLibraryHandle != null)
                {
                    return(true);
                }

                lock (this)
                {
                    if (_sqlUserInstanceLibraryHandle != null)
                    {
                        return(true);
                    }
                    //Get UserInstance Dll path
                    LocalDBErrorState registryQueryErrorState;

                    // Get the LocalDB instance dll path from the registry
                    string dllPath = GetUserInstanceDllPath(out registryQueryErrorState);

                    // If there was no DLL path found, then there is an error.
                    if (dllPath == null)
                    {
                        SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, MapLocalDBErrorStateToCode(registryQueryErrorState), MapLocalDBErrorStateToErrorMessage(registryQueryErrorState));
                        SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.LocalDB.Windows.LoadUserInstanceDll |SNI|ERR >User instance DLL path is null.");
                        return(false);
                    }

                    // In case the registry had an empty path for dll
                    if (string.IsNullOrWhiteSpace(dllPath))
                    {
                        SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBInvalidSqlUserInstanceDllPath, Strings.SNI_ERROR_55);
                        SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.LocalDB.Windows.LoadUserInstanceDll |SNI|ERR > User instance DLL path is invalid. DLL path ={0}", dllPath);
                        return(false);
                    }

                    // Load the dll
                    SafeLibraryHandle libraryHandle = Interop.Kernel32.LoadLibraryExW(dllPath.Trim(), IntPtr.Zero, 0);

                    if (libraryHandle.IsInvalid)
                    {
                        SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBFailedToLoadDll, Strings.SNI_ERROR_56);
                        SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.LocalDB.Windows.LoadUserInstanceDll |SNI|ERR > Library Handle is invalid. Could not load the dll.");
                        libraryHandle.Dispose();
                        return(false);
                    }

                    // Load the procs from the DLLs
                    _startInstanceHandle = Interop.Kernel32.GetProcAddress(libraryHandle, ProcLocalDBStartInstance);

                    if (_startInstanceHandle == IntPtr.Zero)
                    {
                        SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBBadRuntime, Strings.SNI_ERROR_57);
                        SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.LocalDB.Windows.LoadUserInstanceDll |SNI|ERR > Was not able to load the PROC from DLL. Bad Runtime.");
                        libraryHandle.Dispose();
                        return(false);
                    }

                    // Set the delegate the invoke.
                    localDBStartInstanceFunc = (LocalDBStartInstance)Marshal.GetDelegateForFunctionPointer(_startInstanceHandle, typeof(LocalDBStartInstance));

                    if (localDBStartInstanceFunc == null)
                    {
                        SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBBadRuntime, Strings.SNI_ERROR_57);
                        libraryHandle.Dispose();
                        _startInstanceHandle = IntPtr.Zero;
                        return(false);
                    }

                    _sqlUserInstanceLibraryHandle = libraryHandle;
                    SqlClientEventSource.Log.TrySNITraceEvent("<sc.SNI.LocalDB.Windows.LoadUserInstanceDll |SNI|INFO > User Instance DLL was loaded successfully.");
                    return(true);
                }
            }
            finally
            {
                SqlClientEventSource.Log.TrySNIScopeLeaveEvent(scopeID);
            }
        }
        /// <summary>
        /// Loads the User Instance dll.
        /// </summary>
        private bool LoadUserInstanceDll()
        {
            // Check in a non thread-safe way if the handle is already set for performance.
            if (_sqlUserInstanceLibraryHandle != null)
            {
                return(true);
            }

            lock (this)
            {
                if (_sqlUserInstanceLibraryHandle != null)
                {
                    return(true);
                }
                //Get UserInstance Dll path
                LocalDBErrorState registryQueryErrorState;

                // Get the LocalDB instance dll path from the registry
                string dllPath = GetUserInstanceDllPath(out registryQueryErrorState);

                // If there was no DLL path found, then there is an error.
                if (dllPath == null)
                {
                    SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, MapLocalDBErrorStateToCode(registryQueryErrorState), string.Empty);
                    return(false);
                }

                // In case the registry had an empty path for dll
                if (string.IsNullOrWhiteSpace(dllPath))
                {
                    SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBInvalidSqlUserInstanceDllPath, string.Empty);
                    return(false);
                }

                // Load the dll
                SafeLibraryHandle libraryHandle = Interop.Kernel32.LoadLibraryExW(dllPath.Trim(), IntPtr.Zero, 0);

                if (libraryHandle.IsInvalid)
                {
                    SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBFailedToLoadDll, string.Empty);
                    libraryHandle.Dispose();
                    return(false);
                }

                // Load the procs from the DLLs
                _startInstanceHandle = Interop.Kernel32.GetProcAddress(libraryHandle, ProcLocalDBStartInstance);

                if (_startInstanceHandle == IntPtr.Zero)
                {
                    SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBBadRuntime, string.Empty);
                    libraryHandle.Dispose();
                    return(false);
                }

                // Set the delegate the invoke.
                localDBStartInstanceFunc = (LocalDBStartInstance)Marshal.GetDelegateForFunctionPointer(_startInstanceHandle, typeof(LocalDBStartInstance));

                if (localDBStartInstanceFunc == null)
                {
                    SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBBadRuntime, string.Empty);
                    libraryHandle.Dispose();
                    _startInstanceHandle = IntPtr.Zero;
                    return(false);
                }

                _sqlUserInstanceLibraryHandle = libraryHandle;

                return(true);
            }
        }
Exemple #4
0
        /// <summary>
        /// Loads the 8bf filters from the specified file.
        /// </summary>
        /// <param name="path">The path of the plug-in.</param>
        /// <returns>
        /// An enumerable collection containing the filters within the specified file.
        /// </returns>
        internal static IEnumerable <PluginData> LoadFiltersFromFile(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (!PEFile.CheckProcessorArchitecture(path))
            {
                return(System.Linq.Enumerable.Empty <PluginData>());
            }

            List <PluginData> pluginData = new List <PluginData>();

#if DEBUG
            DebugUtils.GlobalDebugFlags |= DebugFlags.PiPL;
#endif
            SafeLibraryHandle dll = UnsafeNativeMethods.LoadLibraryExW(path, IntPtr.Zero, NativeConstants.LOAD_LIBRARY_AS_DATAFILE);
            try
            {
                if (!dll.IsInvalid)
                {
                    QueryFilter queryFilter = new QueryFilter(path);

                    GCHandle handle       = GCHandle.Alloc(queryFilter, GCHandleType.Normal);
                    bool     needsRelease = false;
                    System.Runtime.CompilerServices.RuntimeHelpers.PrepareConstrainedRegions();
                    try
                    {
                        dll.DangerousAddRef(ref needsRelease);
                        IntPtr callback = GCHandle.ToIntPtr(handle);
                        if (UnsafeNativeMethods.EnumResourceNamesW(dll.DangerousGetHandle(), "PiPl", new UnsafeNativeMethods.EnumResNameDelegate(EnumPiPL), callback))
                        {
                            queryFilter = (QueryFilter)GCHandle.FromIntPtr(callback).Target;

                            pluginData.AddRange(queryFilter.plugins);
                        }
                        else if (UnsafeNativeMethods.EnumResourceNamesW(dll.DangerousGetHandle(), "PiMI", new UnsafeNativeMethods.EnumResNameDelegate(EnumPiMI), callback))
                        {
                            // If there are no PiPL resources scan for Photoshop 2.5's PiMI resources.
                            queryFilter = (QueryFilter)GCHandle.FromIntPtr(callback).Target;

                            pluginData.AddRange(queryFilter.plugins);
                        }
#if DEBUG
                        else
                        {
                            DebugUtils.Ping(DebugFlags.Error, string.Format("EnumResourceNames(PiPL, PiMI) failed for {0}", path));
                        }
#endif
                    }
                    finally
                    {
                        if (handle.IsAllocated)
                        {
                            handle.Free();
                        }

                        if (needsRelease)
                        {
                            dll.DangerousRelease();
                        }
                    }
                }
#if DEBUG
                else
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("LoadLibrary() returned 0x{0:X8}", Marshal.GetLastWin32Error()));
                }
#endif
            }
            finally
            {
                if (!dll.IsClosed)
                {
                    dll.Dispose();
                    dll = null;
                }
            }

            if (pluginData.Count > 1)
            {
                // If the DLL contains more than one filter, add a list of all the entry points to each individual filter.
                // Per the SDK only one entry point in a module will display the about box the rest are dummy calls so we must call all of them.

                string[] entryPoints = new string[pluginData.Count];

                for (int i = 0; i < entryPoints.Length; i++)
                {
                    entryPoints[i] = pluginData[i].EntryPoint;
                }

                for (int i = 0; i < entryPoints.Length; i++)
                {
                    pluginData[i].ModuleEntryPoints = entryPoints;
                }
            }

            return(pluginData);
        }
Exemple #5
0
        public virtual void Initialize()
        {
            if (!this.validTool)
            {
                return;
            }
            if (string.IsNullOrEmpty(this.name))
            {
                this.validTool = false;
                this.AddErrorMessage(Strings.NameError);
            }
            if (string.IsNullOrEmpty(this.type))
            {
                this.validTool = false;
                this.AddErrorMessage(Strings.TypeError);
            }
            else if (string.Compare(this.type, "SnapIn", true) != 0 && string.Compare(this.type, "Executable", true) != 0 && string.Compare(this.type, "MonadScript", true) != 0 && string.Compare(this.type, "DynamicURL", true) != 0 && string.Compare(this.type, "StaticURL", true) != 0)
            {
                this.validTool = false;
                this.AddErrorMessage(Strings.InvalidType(this.type));
            }
            if (string.IsNullOrEmpty(this.command) && string.Compare(this.type, "DynamicURL", true) != 0 && string.IsNullOrEmpty(this.command) && string.Compare(this.type, "StaticURL", true) != 0)
            {
                this.validTool = false;
                this.AddErrorMessage(Strings.CommandError);
            }
            SafeLibraryHandle safeLibraryHandle = new SafeLibraryHandle();

            if (string.IsNullOrEmpty(this.assembly))
            {
                this.validTool = false;
                this.AddErrorMessage(Strings.AssemblyError);
            }
            else
            {
                string text = this.assembly;
                if (!Path.IsPathRooted(this.assembly))
                {
                    if (PSConnectionInfoSingleton.GetInstance().Type == OrganizationType.Cloud)
                    {
                        text = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(text));
                    }
                    else
                    {
                        text = Path.Combine(this.workingFolder, this.assembly);
                    }
                }
                if (!File.Exists(text))
                {
                    this.validTool = false;
                    this.AddErrorMessage(Strings.AssemblyMissing(this.assembly));
                }
                else
                {
                    if (safeLibraryHandle != null)
                    {
                        safeLibraryHandle.Dispose();
                        safeLibraryHandle = null;
                    }
                    safeLibraryHandle = SafeLibraryHandle.LoadLibrary(text);
                }
            }
            if (string.Compare(this.type, "MonadScript", true) == 0 || string.Compare(this.type, "SnapIn", true) == 0)
            {
                if (string.IsNullOrEmpty(this.commandFile))
                {
                    this.validTool = false;
                    this.AddErrorMessage(Strings.CommandFileError(this.name));
                }
                else if (string.Compare(this.type, "SnapIn", true) == 0)
                {
                    if (string.Compare(Path.GetFileName(this.command), "mmc.exe", true) != 0)
                    {
                        this.validTool = false;
                        this.AddErrorMessage(Strings.InvalidSnapinTool(this.command));
                    }
                    else
                    {
                        string empty = string.Empty;
                        if (!Tool.safeToolsList.TryGetValue(this.name, out empty))
                        {
                            this.validTool = false;
                            this.AddErrorMessage(Strings.SnapInNotInSafeList(this.name));
                        }
                        else if (string.Compare(Path.GetFileName(this.commandFile.Replace("\"", "")), empty, true) != 0)
                        {
                            this.validTool = false;
                            this.AddErrorMessage(Strings.SnapInCommandFileNotInSafeList(this.commandFile, this.name));
                        }
                    }
                }
                else if (string.Compare(Path.GetFileName(this.command), "PowerShell.exe", true) != 0)
                {
                    this.validTool = false;
                    this.AddErrorMessage(Strings.InvalidCmdletTool(this.command));
                }
                else
                {
                    string empty2 = string.Empty;
                    if (!Tool.safeToolsList.TryGetValue(this.name, out empty2))
                    {
                        this.validTool = false;
                        this.AddErrorMessage(Strings.SnapInNotInSafeList(this.name));
                    }
                    else if (string.Compare(Path.GetFileName(this.commandFile.Replace("\"", "")), empty2, true) != 0)
                    {
                        this.validTool = false;
                        this.AddErrorMessage(Strings.SnapInCommandFileNotInSafeList(this.commandFile, this.name));
                    }
                }
            }
            else if (string.Compare(this.type, "Executable", true) == 0)
            {
                if (string.IsNullOrEmpty(this.command))
                {
                    this.validTool = false;
                    this.AddErrorMessage(Strings.CommandError);
                }
                else
                {
                    string empty3 = string.Empty;
                    if (!Tool.safeToolsList.TryGetValue(this.name, out empty3))
                    {
                        this.validTool = false;
                        this.AddErrorMessage(Strings.ExecutableNotInSafeList(this.name));
                    }
                    else if (string.Compare(Path.GetFileName(this.command), empty3, true) != 0)
                    {
                        this.validTool = false;
                        this.AddErrorMessage(Strings.ExecutableCommandNotInSafeList(this.command, this.name));
                    }
                }
            }
            try
            {
                this.nonEdgeTool = Tool.nonEdgeToolsList.Contains(this.Name);
                this.cloudAndRemoteOnPremiseTool = Tool.cloudAndRemoteOnPremiseToolsList.Contains(this.Name);
                if (!safeLibraryHandle.IsInvalid)
                {
                    if (this.localizedDescription != 0)
                    {
                        string @string = this.GetString(this.localizedDescription, safeLibraryHandle);
                        if (!string.IsNullOrEmpty(@string))
                        {
                            this.description = @string;
                        }
                    }
                    if (this.localizedGroupName != 0)
                    {
                        string @string = this.GetString(this.LocalizedGroupName, safeLibraryHandle);
                        if (!string.IsNullOrEmpty(@string))
                        {
                            this.groupName = @string;
                        }
                    }
                    if (this.localizedName != 0)
                    {
                        string @string = this.GetString(this.LocalizedName, safeLibraryHandle);
                        if (!string.IsNullOrEmpty(@string))
                        {
                            this.name = @string;
                        }
                    }
                }
                this.LoadIcon(safeLibraryHandle);
            }
            finally
            {
                safeLibraryHandle.Dispose();
            }
            this.UpdateGroup();
        }