Esempio n. 1
0
        public static List <NavisionClientInformation> CheckRot()
        {
            List <NavisionClientInformation> navisionClientsTemp = new List <NavisionClientInformation>();
            bool isTimeout = true;

            List <RunningNavObject> runningNavObjects = null;
            Task t2 = new Task(
                () =>
            {
                runningNavObjects = RunningObjectTable.GetRunningCOMObjectsByName(DEFAULTMONIKERNAME, COMPANYCHECKFORNAVISIONCLIENT);
                isTimeout         = false;
            });

            runningNavObjects = RunningObjectTable.GetRunningCOMObjectsByName(DEFAULTMONIKERNAME, COMPANYCHECKFORNAVISIONCLIENT);
            t2.WithTimeout(new TimeSpan(0, 0, 5));
            t2.Start();
            t2.Wait(5500);
            if (isTimeout)
            {
                throw new TimeoutException("Timeout");
            }

            if (!runningNavObjects.Any())
            {
                return(navisionClientsTemp);
            }

            foreach (RunningNavObject de in runningNavObjects)
            {
                RunningNavObject de1 = de;

                Task t = new Task(
                    () =>
                {
                    INSObjectDesigner navObjectDesigner = de1.ComInstance as INSObjectDesigner;
                    INSHyperlink hyperlink     = de1.ComInstance as INSHyperlink;
                    INSApplication application = de1.ComInstance as INSApplication;

                    if (navObjectDesigner == null)
                    {
                        return;
                    }



                    NavisionClientInformation nci = new NavisionClientInformation();

                    try
                    {
                        navObjectDesigner.GetDatabaseName(out string value);
                        if (value != null)
                        {
                            nci.DatabaseName = Encoding.GetEncoding(NavisionEncoding).GetString(Encoding.Default.GetBytes(value));
                        }

                        navObjectDesigner.GetCompanyName(out value);
                        if (value != null)
                        {
                            nci.CompanyName = Encoding.GetEncoding(NavisionEncoding).GetString(Encoding.Default.GetBytes(value));
                        }

                        navObjectDesigner.GetServerName(out value);
                        if (value != null)
                        {
                            nci.ServerName = Encoding.GetEncoding(NavisionEncoding).GetString(Encoding.Default.GetBytes(value));
                        }

                        navObjectDesigner.GetServerType(out int value2);

                        nci.ServerType = (ServerType)value2;

                        nci.ObjectDesigner = navObjectDesigner;
                        nci.Application    = application;

                        try
                        {
                            navObjectDesigner.GetCSIDEVersion(out string version);
                            string[] str = version.Split(new[] { " " }, StringSplitOptions.None);
                            nci.Version  = float.Parse(str[1], CultureInfo.InvariantCulture);
                        }
                        catch
                        {
                        }


                        hyperlink.GetNavWindowHandle(out int handle);
                        nci.WindowHandle = handle;

                        GetWindowThreadProcessId((IntPtr)handle, out uint processID);
                        nci.ProcessID = (int)processID;

                        nci.ExecutionPath                 = Path.GetDirectoryName(GetProcessPath(new IntPtr(handle)));
                        nci.ExecutionFileName             = GetProcessPath(new IntPtr(handle));
                        FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(nci.ExecutionFileName);
                        nci.MajorVersion = myFileVersionInfo.FileMajorPart;
                    }
                    catch
                    {
                        navisionClientsTemp = null;
                        throw new Exception("Communication Problem");
                    }

                    if (navisionClientsTemp == null)
                    {
                        isTimeout = false;
                        return;
                    }

                    if (navisionClientsTemp.Any(
                            info =>
                            info.ServerName == nci.ServerName &&
                            info.DatabaseName == nci.DatabaseName))
                    {
                        navisionClientsTemp = null;
                        throw new Exception("Multiple Clients");
                    }

                    navisionClientsTemp.Add(nci);
                    isTimeout = false;
                });
                t.WithTimeout(new TimeSpan(0, 0, 2));
                t.Start();
                t.Wait(2500);
            }

            if (isTimeout)
            {
                throw new Exception("Timeout");
            }

            return(navisionClientsTemp);
        }
Esempio n. 2
0
        public static List <RunningNavObject> GetRunningCOMObjectsByName(string objectDisplayName, string objectDisplayNameCheck2)
        {
            IRunningObjectTable     runningObjectTable   = null;
            List <RunningNavObject> runningNavObjectList = new List <RunningNavObject>();
            IEnumMoniker            monikerList          = null;

            try
            {
                if (GetRunningObjectTable(0, out runningObjectTable) != 0 || runningObjectTable == null)
                {
                    return(null);
                }

                runningObjectTable.EnumRunning(out monikerList);
                monikerList.Reset();
                IMoniker[] monikerContainer = new IMoniker[1];

                IntPtr pointerFetchedMonikers = IntPtr.Zero;
                while (monikerList.Next(1, monikerContainer, pointerFetchedMonikers) == 0)
                {
                    IBindCtx bindInfo;
                    string   displayName;
                    CreateBindCtx(0, out bindInfo);

                    try
                    {
                        monikerContainer[0].GetDisplayName(bindInfo, null, out displayName);
                        Marshal.ReleaseComObject(bindInfo);
                    }
                    catch (Exception)
                    {
                        Marshal.ReleaseComObject(bindInfo);
                        continue;
                    }

                    if (displayName.IndexOf(objectDisplayName, StringComparison.Ordinal) == -1)
                    {
                        continue;
                    }
                    if (displayName.IndexOf(objectDisplayNameCheck2, StringComparison.Ordinal) != -1 ||
                        (displayName.IndexOf("servername=", StringComparison.Ordinal) != -1 && displayName.IndexOf("database=", StringComparison.Ordinal) != -1))
                    {
                        object comInstance;
                        runningObjectTable.GetObject(monikerContainer[0], out comInstance);
                        if (comInstance == null)
                        {
                            continue;
                        }
                        RunningNavObject navObject = new RunningNavObject(displayName, comInstance);

                        string[] split  = displayName.ToLower().Split(new[] { "?" }, StringSplitOptions.RemoveEmptyEntries);
                        string[] split2 = split[1].Split(new[] { "&" }, StringSplitOptions.RemoveEmptyEntries);
                        if (split2.Length >= 1)
                        {
                            navObject.Servername = split2[0].Replace("servername=", string.Empty);
                        }
                        if (split2.Length >= 2)
                        {
                            navObject.Database = split2[1].Replace("database=", string.Empty);
                        }
                        if (split2.Length >= 3)
                        {
                            navObject.Company = split2[2].Replace("company=", string.Empty);
                        }

                        if (!string.IsNullOrEmpty(navObject.Company))
                        {
                            continue;
                        }

                        runningNavObjectList.Add(navObject);
                    }
                }

                return(runningNavObjectList);
            }
            catch (TimeoutException e)
            {
                throw new TimeoutException(e.ToString());
            }
            finally
            {
                if (runningObjectTable != null)
                {
                    Marshal.ReleaseComObject(runningObjectTable);
                }
                if (monikerList != null)
                {
                    Marshal.ReleaseComObject(monikerList);
                }
            }
        }