回答編集履歴
3
answer
CHANGED
@@ -38,7 +38,6 @@
|
|
38
38
|
Dim tokenHandle As IntPtr
|
39
39
|
Try
|
40
40
|
If OpenProcessToken(process.Handle, TOKEN_QUERY, tokenHandle) Then
|
41
|
-
CloseHandle(tokenHandle)
|
42
41
|
Return New WindowsIdentity(tokenHandle)
|
43
42
|
End If
|
44
43
|
Catch ex As Exception
|
2
handle Close 漏れ
answer
CHANGED
@@ -35,14 +35,17 @@
|
|
35
35
|
End Sub
|
36
36
|
|
37
37
|
Public Function GetWindowsIdentity(ByVal process As Process) As WindowsIdentity
|
38
|
+
Dim tokenHandle As IntPtr
|
38
39
|
Try
|
39
|
-
Dim tokenHandle As IntPtr
|
40
40
|
If OpenProcessToken(process.Handle, TOKEN_QUERY, tokenHandle) Then
|
41
|
+
CloseHandle(tokenHandle)
|
41
42
|
Return New WindowsIdentity(tokenHandle)
|
43
|
+
End If
|
44
|
+
Catch ex As Exception
|
45
|
+
Finally
|
46
|
+
If tokenHandle <> IntPtr.Zero Then
|
42
47
|
CloseHandle(tokenHandle)
|
43
48
|
End If
|
44
|
-
Catch ex As Exception
|
45
|
-
|
46
49
|
End Try
|
47
50
|
Return Nothing
|
48
51
|
End Function
|
1
WindowsIdentity を使った方法を追加
answer
CHANGED
@@ -14,4 +14,49 @@
|
|
14
14
|
というロジックで良いと思います、
|
15
15
|
|
16
16
|
WMI はちょっと遅いので API でゴリゴリ書いた例を紹介しておきます。(C#)
|
17
|
-
[http://kozhouse.homeip.net/dotnet/etc/10/](http://kozhouse.homeip.net/dotnet/etc/10/)
|
17
|
+
[http://kozhouse.homeip.net/dotnet/etc/10/](http://kozhouse.homeip.net/dotnet/etc/10/)
|
18
|
+
|
19
|
+
###追記
|
20
|
+
Framework に何か部品がないかなーと調べてみたのですが、System.Security.Principal.WindowsIdentity を使うと、もっと簡単になります。
|
21
|
+
|
22
|
+
```VB.NET
|
23
|
+
Imports System.Runtime.InteropServices
|
24
|
+
Imports System.Security.Principal
|
25
|
+
|
26
|
+
Module Module1
|
27
|
+
|
28
|
+
Sub Main()
|
29
|
+
For Each p In Process.GetProcesses()
|
30
|
+
Using wid As WindowsIdentity = GetWindowsIdentity(p)
|
31
|
+
Debug.WriteLine("{0} {1}", p.ProcessName, If(wid Is Nothing, "(Error)", wid.Name))
|
32
|
+
End Using
|
33
|
+
p.Dispose()
|
34
|
+
Next
|
35
|
+
End Sub
|
36
|
+
|
37
|
+
Public Function GetWindowsIdentity(ByVal process As Process) As WindowsIdentity
|
38
|
+
Try
|
39
|
+
Dim tokenHandle As IntPtr
|
40
|
+
If OpenProcessToken(process.Handle, TOKEN_QUERY, tokenHandle) Then
|
41
|
+
Return New WindowsIdentity(tokenHandle)
|
42
|
+
CloseHandle(tokenHandle)
|
43
|
+
End If
|
44
|
+
Catch ex As Exception
|
45
|
+
|
46
|
+
End Try
|
47
|
+
Return Nothing
|
48
|
+
End Function
|
49
|
+
|
50
|
+
Private Const TOKEN_QUERY = &H8
|
51
|
+
|
52
|
+
<DllImport("advapi32.dll", SetLastError:=True)>
|
53
|
+
Private Function OpenProcessToken(ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Boolean
|
54
|
+
End Function
|
55
|
+
|
56
|
+
<DllImport("kernel32.dll", SetLastError:=True)>
|
57
|
+
Private Function CloseHandle(ByVal hHandle As IntPtr) As Boolean
|
58
|
+
End Function
|
59
|
+
|
60
|
+
End Module
|
61
|
+
```
|
62
|
+
OpenProcessToken を公開しているメソッドがあれば、API 使わなくて済むのですが、今のところ見つからないです。
|