特定のユーザーとして実行する必要がある .Net 2.0 Windows Forms アプリケーションがあります (右クリックして実行)。
どのユーザーが起動したかを確認し、特定のユーザーでない場合は停止できるようにする必要があります。
私が見つけたすべての例は、ログインしているユーザーを示しています。
ユーザー名を実行しているアプリケーションにアクセスするにはどうすればよいですか?
多分これはあなたを助けるでしょう:C#でプロセスの所有者をどのように決定しますか?
これはC#ですが、VB.NETに簡単に変換できます。Googleで「C#toVB」を検索してください:)
thisに基づいて、 thisから少し助けて、私はこれを思いつきました:
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Public Class GetProcessOwner
<DllImport("advapi32.dll", SetLastError:=True)> _
Public Shared Function OpenProcessToken(ByVal processHandle As IntPtr, ByVal desiredAccess As Integer, ByRef tokenHandle As IntPtr) As Boolean
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function CloseHandle(ByVal hObject As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Private Const TokenQuery As UInteger = &H8
Public Shared Function GetProcessOwner(ByVal processName As String) As String
Dim ownerName As String = String.Empty
For Each p As Process In Process.GetProcesses()
If p.ProcessName = processName Then
Dim ph As IntPtr = IntPtr.Zero
Try
OpenProcessToken(p.Handle, TokenQuery, ph)
Dim wi As WindowsIdentity = New WindowsIdentity(ph)
ownerName = wi.Name
Catch ex As Exception
ownerName = String.Empty
Finally
If ph <> IntPtr.Zero Then
CloseHandle(ph)
End If
End Try
End If
Next
Return ownerName
End Function
End Class