public static Dictionary <int, TcpRow> GetExtendedTcpDictionary()
        {
            Dictionary <int, TcpRow> tcpRows = new Dictionary <int, TcpRow>();

            IntPtr tcpTable       = IntPtr.Zero;
            int    tcpTableLength = 0;

            if (IpHelper.GetExtendedTcpTable(tcpTable, ref tcpTableLength, false, IpHelper.AfInet, IpHelper.TcpTableType.OwnerPidAll, 0) != 0)
            {
                try
                {
                    tcpTable = Marshal.AllocHGlobal(tcpTableLength);
                    if (IpHelper.GetExtendedTcpTable(tcpTable, ref tcpTableLength, true, IpHelper.AfInet, IpHelper.TcpTableType.OwnerPidAll, 0) == 0)
                    {
                        IpHelper.TcpTable table = (IpHelper.TcpTable)Marshal.PtrToStructure(tcpTable, typeof(IpHelper.TcpTable));

                        IntPtr rowPtr = (IntPtr)((long)tcpTable + Marshal.SizeOf(table.length));
                        for (int i = 0; i < table.length; ++i)
                        {
                            TcpRow row = new TcpRow((IpHelper.TcpRow)Marshal.PtrToStructure(rowPtr, typeof(IpHelper.TcpRow)));
                            // HACK: only add first row that is in a Listening state
                            if (row.State == TcpState.Listen)
                            {
                                if (!tcpRows.Keys.Contains(row.ProcessId))
                                {
                                    tcpRows.Add(row.ProcessId, row);
                                }
                            }
                            rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(typeof(IpHelper.TcpRow)));
                        }
                    }
                }
                finally
                {
                    if (tcpTable != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(tcpTable);
                    }
                }
            }

            return(tcpRows);
        }
        public static List <PowerBIInstance> GetLocalInstances()
        {
            List <PowerBIInstance> _instances = new List <PowerBIInstance>();

            _instances.Clear();

            var dict = ManagedIpHelper.GetExtendedTcpDictionary();

            foreach (var proc in Process.GetProcessesByName("msmdsrv"))
            {
                int _port = 0;
                EmbeddedSSASIcon _icon = EmbeddedSSASIcon.PowerBI;
                var parent             = proc.GetParent();

                // exit here if the parent == "services" then this is a SSAS instance
                if (parent.ProcessName.Equals("services", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                // exit here if the parent == "RSHostingService" then this is a SSAS instance
                if (parent.ProcessName.Equals("RSHostingService", StringComparison.OrdinalIgnoreCase))
                {
                    // only show PBI Report Server if we are running as admin
                    // otherwise we won't have any access to the models
                    if (IsAdministrator())
                    {
                        _icon = EmbeddedSSASIcon.PowerBIReportServer;
                    }
                    else
                    {
                        continue;
                    }
                }

                // if the process was launched from Visual Studio change the icon
                if (parent.ProcessName.Equals("devenv", StringComparison.OrdinalIgnoreCase))
                {
                    _icon = EmbeddedSSASIcon.Devenv;
                }

                // get the window title so that we can parse out the file name
                var parentTitle = parent.MainWindowTitle;
                if (parentTitle.Length == 0)
                {
                    // for minimized windows we need to use some Win32 api calls to get the title
                    //parentTitle = WindowTitle.GetWindowTitleTimeout( parent.Id, 300);
                    parentTitle = WindowTitle.GetWindowTitle(parent.Id);
                }

                // try and get the tcp port from the Win32 TcpTable API
                try
                {
                    TcpRow tcpRow = null;
                    dict.TryGetValue(proc.Id, out tcpRow);
                    if (tcpRow != null)
                    {
                        _port = tcpRow.LocalEndPoint.Port;
                        _instances.Add(new PowerBIInstance(parentTitle, _port, _icon));
                        //Log.Debug("{class} {method} PowerBI found on port: {port}", "PowerBIHelper", "Refresh", _port);
                    }
                    else
                    {
                        //Log.Debug("{class} {method} PowerBI port not found for process: {processName} PID: {pid}", "PowerBIHelper", "Refresh", proc.ProcessName, proc.Id);
                    }
                }
                catch (Exception ex)
                {
                    //Log.Error("{class} {Method} {Error} {StackTrace}", "PowerBIHelper", "Refresh", ex.Message, ex.StackTrace);
                }
            }
            return(_instances);
        }