forked from PowerBiDevCamp/DevCampExternalToolDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerBiDesktopUtilities.cs
194 lines (156 loc) · 5.81 KB
/
PowerBiDesktopUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Management;
using System.Collections;
using System.Net.NetworkInformation;
using System.Net;
using System.Runtime.InteropServices;
using DevCampExternalToolDemo.Models;
namespace DevCampExternalToolDemo.Services {
class PowerBiDesktopUtilities {
public static List<DatasetConnection> GetActiveDatasetConnections() {
var sessions = new List<DatasetConnection>();
var tcpTable = ManagedIpHelper.GetExtendedTcpTable();
foreach (Process process in Process.GetProcessesByName("msmdsrv")) {
// get all Power BI Desktop sessions on local machine
var parent = process.GetParent();
// get tcpRow to determine port number of this local AS engine session
var tcpRow = tcpTable.SingleOrDefault((r) => r.ProcessId == process.Id &&
r.State == TcpState.Listen &&
IPAddress.IsLoopback(r.LocalEndPoint.Address));
sessions.Add(new DatasetConnection {
DatasetName = process.GetParent().MainWindowTitle.Replace(" - Power BI Desktop", "") + ".pbix",
ConnectString = "localhost:" + tcpRow?.LocalEndPoint.Port.ToString(),
ConnectionType = ConnectionType.PowerBiDesktop
});
}
return sessions;
}
}
public static class ProcessExtensions {
public static Process GetParent(this Process process) {
try {
using (var query = new ManagementObjectSearcher("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId=" + process.Id)) {
return query
.Get()
.OfType<ManagementObject>()
.Select(p => Process.GetProcessById((int)(uint)p["ParentProcessId"]))
.FirstOrDefault();
}
}
catch {
return null;
}
}
}
public class TcpTable : IEnumerable<TcpRow> {
private IEnumerable<TcpRow> tcpRows;
public TcpTable(IEnumerable<TcpRow> tcpRows) {
this.tcpRows = tcpRows;
}
public IEnumerable<TcpRow> Rows {
get { return this.tcpRows; }
}
public IEnumerator<TcpRow> GetEnumerator() {
return this.tcpRows.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return this.tcpRows.GetEnumerator();
}
}
public class TcpRow {
private IPEndPoint localEndPoint;
private IPEndPoint remoteEndPoint;
private TcpState state;
private int processId;
public TcpRow(IpHelper.TcpRow tcpRow) {
this.state = tcpRow.state;
this.processId = tcpRow.owningPid;
int localPort = (tcpRow.localPort1 << 8) + (tcpRow.localPort2) + (tcpRow.localPort3 << 24) + (tcpRow.localPort4 << 16);
long localAddress = tcpRow.localAddr;
this.localEndPoint = new IPEndPoint(localAddress, localPort);
int remotePort = (tcpRow.remotePort1 << 8) + (tcpRow.remotePort2) + (tcpRow.remotePort3 << 24) + (tcpRow.remotePort4 << 16);
long remoteAddress = tcpRow.remoteAddr;
this.remoteEndPoint = new IPEndPoint(remoteAddress, remotePort);
}
public IPEndPoint LocalEndPoint {
get { return this.localEndPoint; }
}
public IPEndPoint RemoteEndPoint {
get { return this.remoteEndPoint; }
}
public TcpState State {
get { return this.state; }
}
public int ProcessId {
get { return this.processId; }
}
}
public static class ManagedIpHelper {
public static TcpTable GetExtendedTcpTable(bool sorted = false) {
List<TcpRow> tcpRows = new List<TcpRow>();
IntPtr tcpTable = IntPtr.Zero;
int tcpTableLength = 0;
if (IpHelper.GetExtendedTcpTable(tcpTable, ref tcpTableLength, sorted, 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) {
tcpRows.Add(new TcpRow((IpHelper.TcpRow)Marshal.PtrToStructure(rowPtr, typeof(IpHelper.TcpRow))));
rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(typeof(IpHelper.TcpRow)));
}
}
}
finally {
if (tcpTable != IntPtr.Zero) {
Marshal.FreeHGlobal(tcpTable);
}
}
}
return new TcpTable(tcpRows);
}
}
public static class IpHelper {
public const string DllName = "iphlpapi.dll";
public const int AfInet = 2;
[DllImport(IpHelper.DllName, SetLastError = true)]
public static extern uint GetExtendedTcpTable(IntPtr tcpTable, ref int tcpTableLength, bool sort, int ipVersion, TcpTableType tcpTableType, int reserved);
public enum TcpTableType {
BasicListener,
BasicConnections,
BasicAll,
OwnerPidListener,
OwnerPidConnections,
OwnerPidAll,
OwnerModuleListener,
OwnerModuleConnections,
OwnerModuleAll,
}
[StructLayout(LayoutKind.Sequential)]
public struct TcpTable {
public uint length;
public TcpRow row;
}
[StructLayout(LayoutKind.Sequential)]
public struct TcpRow {
public TcpState state;
public uint localAddr;
public byte localPort1;
public byte localPort2;
public byte localPort3;
public byte localPort4;
public uint remoteAddr;
public byte remotePort1;
public byte remotePort2;
public byte remotePort3;
public byte remotePort4;
public int owningPid;
}
}
}