protected void refreshInfo()
 {
     try
     {
         txtFile.Text = ActiveXImporter.ActiveXInfo.OcxFile;
         TypeRec rec = ActiveXImporter.ActiveXInfo.GetOcxInfo();
         listBox1.Items.Clear();
         if (rec != null)
         {
             int m = rec.ClassCount;
             for (short i = 0; i < m; i++)
             {
                 listBox1.Items.Add(rec.ClassRecord(i));
             }
         }
         btNext.Enabled = (listBox1.Items.Count > 0);
     }
     catch (Exception er)
     {
         string s = LoadActiveXInfoException.FormExceptionText(er);
         MessageBox.Show(this, s, "Load ActiveX", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
     }
 }
Exemple #2
0
        public void LoadTypes(System.Windows.Forms.ListBox listBox1)
        {
            bool   bAbort = false;
            bool   bSkip  = false;
            int    n;
            string sExt;

            listBox1.Visible = false;
            Microsoft.Win32.RegistryKey keyTypeLib = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("TypeLib", false);
            bCancel = false;
            if (keyTypeLib == null)
            {
                MessageBox.Show(this, "Cannot find registry TypeLib", Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                bool bTryRegsvr = false;
                bSkip = true;                 //not to confuse the amerturers
                //LoadActiveXInfoException
                List <Type> skipTypes = new List <Type>();
                string[]    types     = keyTypeLib.GetSubKeyNames();
                if (types != null)
                {
                    for (int i = 0; i < types.Length; i++)
                    {
                        Application.DoEvents();
                        if (bAbort)
                        {
                            break;
                        }
                        Microsoft.Win32.RegistryKey keyType = keyTypeLib.OpenSubKey(types[i], false);
                        if (bCancel)
                        {
                            break;
                        }
                        if (keyType != null)
                        {
                            string[] vers = keyType.GetSubKeyNames();
                            if (vers != null)
                            {
                                for (int j = 0; j < vers.Length; j++)
                                {
                                    Application.DoEvents();
                                    if (bAbort)
                                    {
                                        break;
                                    }
                                    short MainVer = 0;
                                    short MinVer  = 0;
                                    try
                                    {
                                        n = vers[j].IndexOf('.');
                                        if (n == 0)
                                        {
                                            string s = vers[j].Substring(1);
                                            if (!string.IsNullOrEmpty(s))
                                            {
                                                if (!short.TryParse(s, out MinVer))
                                                {
                                                    if (!short.TryParse(s, System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture, out MinVer))
                                                    {
                                                        throw new LoadActiveXInfoException("Invalid MinVer:" + s);
                                                    }
                                                }
                                            }
                                        }
                                        else if (n < 0)
                                        {
                                            if (!string.IsNullOrEmpty(vers[j]))
                                            {
                                                if (!short.TryParse(vers[j], out MainVer))
                                                {
                                                    if (!short.TryParse(vers[j], System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture, out MainVer))
                                                    {
                                                        throw new LoadActiveXInfoException("Invalid MinVer:" + vers[j]);
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            string s = vers[j].Substring(0, n);
                                            if (!short.TryParse(s, out MainVer))
                                            {
                                                if (!short.TryParse(s, System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture, out MainVer))
                                                {
                                                    throw new LoadActiveXInfoException("Invalid MinVer:" + s);
                                                }
                                            }
                                            s = vers[j].Substring(n + 1);
                                            if (!short.TryParse(s, out MinVer))
                                            {
                                                if (!short.TryParse(s, System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture, out MinVer))
                                                {
                                                    throw new LoadActiveXInfoException("Invalid MinVer:" + s);
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception err)
                                    {
                                        if (!bSkip)
                                        {
                                            if (!skipTypes.Contains(err.GetType()))
                                            {
                                                FormError dlg = new FormError();
                                                dlg.SetMessage(LoadActiveXInfoException.FormExceptionText(err));
                                                DialogResult ret = dlg.ShowDialog(this);
                                                if (ret == DialogResult.Ignore)
                                                {
                                                    bSkip = true;
                                                }
                                                else if (ret == DialogResult.Abort)
                                                {
                                                    bAbort = true;
                                                }
                                                else if (ret == DialogResult.Cancel)
                                                {
                                                    skipTypes.Add(err.GetType());
                                                }
                                            }
                                        }
                                    }
                                    Microsoft.Win32.RegistryKey keyVer = keyType.OpenSubKey(vers[j], false);
                                    if (bCancel)
                                    {
                                        break;
                                    }
                                    if (keyVer != null)
                                    {
                                        string[] LCIDs = keyVer.GetSubKeyNames();
                                        if (LCIDs != null)
                                        {
                                            for (int k = 0; k < LCIDs.Length; k++)
                                            {
                                                if (bAbort)
                                                {
                                                    break;
                                                }
                                                TypeRec rec = null;
                                                try
                                                {
                                                    rec = new TypeRec();
                                                }
                                                catch (Exception eRec)
                                                {
                                                    bAbort     = true;
                                                    bTryRegsvr = true;
                                                    MessageBox.Show(this, eRec.Message, "Search ActiveX Libraries", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                                }
                                                if (rec != null && !bAbort)
                                                {
                                                    try
                                                    {
                                                        rec.GUID    = types[i];
                                                        rec.MainVer = MainVer;
                                                        rec.MinVer  = MinVer;
                                                        if (!string.IsNullOrEmpty(LCIDs[k]))
                                                        {
                                                            int cid;
                                                            if (int.TryParse(LCIDs[k], out cid))
                                                            {
                                                                rec.LCID = cid;
                                                                rec.LoadInfo();
                                                                sExt = System.IO.Path.GetExtension(rec.File);
                                                                sExt = sExt.ToLowerInvariant();
                                                                if (string.Compare(sExt, ".dll", StringComparison.OrdinalIgnoreCase) == 0)
                                                                {
                                                                    listBox1.Items.Add(rec);
                                                                }
                                                                lblInfo.Text = rec.ToString();
                                                                Application.DoEvents();
                                                                Application.DoEvents();
                                                            }
                                                        }
                                                    }
                                                    catch (ExceptionTLI etli)
                                                    {
                                                        MessageBox.Show(this, etli.Message, "Search ActiveX Librarues", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                                        bAbort     = true;
                                                        bTryRegsvr = true;
                                                    }
                                                    catch (Exception err)
                                                    {
                                                        if (!bSkip)
                                                        {
                                                            if (!skipTypes.Contains(err.GetType()))
                                                            {
                                                                FormError dlg = new FormError();
                                                                dlg.SetMessage(LoadActiveXInfoException.FormExceptionText(err));
                                                                DialogResult ret = dlg.ShowDialog(this);
                                                                if (ret == DialogResult.Ignore)
                                                                {
                                                                    bSkip = true;
                                                                }
                                                                else if (ret == DialogResult.Abort)
                                                                {
                                                                    bAbort = true;
                                                                }
                                                                else if (ret == DialogResult.Cancel)
                                                                {
                                                                    skipTypes.Add(err.GetType());
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            keyType.Close();
                        }
                    }
                }
                keyTypeLib.Close();
                if (bTryRegsvr)
                {
                    string tliFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "TLBINF32.DLL");
                    if (File.Exists(tliFile))
                    {
                        if (MessageBox.Show(this, string.Format(CultureInfo.InvariantCulture, "Limnor Studio is trying to fix the problem by execyting regsvr32 \"{0}\"\r\n After executing the command, you may try importing ActiveX again.\r\n\r\nDo you want to continue?", tliFile), "Search ActiveX Libraries", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            string           stdout;
                            string           errout;
                            Process          p   = new Process();
                            ProcessStartInfo psI = new ProcessStartInfo("cmd");
                            psI.UseShellExecute        = false;
                            psI.RedirectStandardInput  = false;
                            psI.RedirectStandardOutput = true;
                            psI.RedirectStandardError  = true;
                            psI.CreateNoWindow         = true;
                            p.StartInfo           = psI;
                            p.StartInfo.FileName  = "regsvr32";
                            p.StartInfo.Arguments = string.Format(CultureInfo.InvariantCulture, "\"{0}\"", tliFile);
                            p.Start();

                            stdout = p.StandardOutput.ReadToEnd();
                            errout = p.StandardError.ReadToEnd();

                            p.WaitForExit();
                            if (p.ExitCode != 0)
                            {
                                MessageBox.Show(this, string.Format(CultureInfo.InvariantCulture, "regsvr32 failed. Error code {0}, output:{1}, error:{2}", p.ExitCode, stdout, errout), "Search ActiveX Libraries", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            else
                            {
                                MessageBox.Show(this, "TLBINF32.DLL is registered. You may try to import ActiveX libraries again", "Search ActiveX Libraries", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show(this, string.Format(CultureInfo.InvariantCulture, "File not found:{0}", tliFile), "Search ActiveX Libraries", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            listBox1.Visible = true;
        }