private static SafeLibraryHandle ExtractAndLoadEmbededResource(string relativePath)
        {
            var assembly = typeof(AEngine).Assembly;
            var resource = EmbededResourceHelper.FindByName(assembly, relativePath);

            if (resource != null)
            {
                string tempFolderPath = GetTempFolderPath();
                var    output_file    = Path.Combine(tempFolderPath, relativePath);
                EmbededResourceHelper.ExtractToFile(assembly, resource, output_file);
                return(SafeLibraryHandle.LoadLibrary(output_file));
            }

            return(null);
        }
예제 #2
0
        public static string GetDllResourceString(string dllPath, int resourceId)
        {
            StringBuilder stringBuilder = new StringBuilder();

            using (SafeLibraryHandle safeLibraryHandle = SafeLibraryHandle.LoadLibrary(dllPath))
            {
                if (!safeLibraryHandle.IsInvalid)
                {
                    int num = NativeMethods.LoadString(safeLibraryHandle, resourceId, stringBuilder, 0);
                    if (num != 0)
                    {
                        stringBuilder.EnsureCapacity(num + 1);
                        NativeMethods.LoadString(safeLibraryHandle, resourceId, stringBuilder, stringBuilder.Capacity);
                    }
                }
            }
            return(stringBuilder.ToString());
        }
예제 #3
0
        private static SafeLibraryHandle LoadDll()
        {
            //determine if we should use x86/AutoHotkey.dll or x64/AutoHotkey.dll
            //then try to load it by the file directory or
            //extract the embeded ones into a temp directory
            //and load them

            string processor_type = ProcessorType.Is32Bit() ? "x86" : "x64";
            string relativePath   = processor_type + "/AutoHotkey.dll";

            if (File.Exists(relativePath))
            {
                return(SafeLibraryHandle.LoadLibrary(relativePath));
            }
            else
            {
                return(ExtractAndLoadEmbededResource(relativePath));
            }
        }
예제 #4
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();
        }