Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET2.0Windows窗体-如何获取执行用户_Vb.net_Winforms_.net 2.0 - Fatal编程技术网

VB.NET2.0Windows窗体-如何获取执行用户

VB.NET2.0Windows窗体-如何获取执行用户,vb.net,winforms,.net-2.0,Vb.net,Winforms,.net 2.0,我有一个.Net 2.0 Windows窗体应用程序,需要以特定用户身份运行(右键单击,以用户身份运行) 我需要能够检查哪个用户启动了它,如果不是特定用户,则停止 我找到的所有示例都显示了登录用户 如何访问执行用户名的应用程序?这可能会帮助您: 它是用C语言编写的,但很容易转换成VB.NET,只需在谷歌上搜索“C到VB”:)基于,在我的一点帮助下,我得出了以下结论: Imports System.Runtime.InteropServices Imports System.Security.Pr

我有一个.Net 2.0 Windows窗体应用程序,需要以特定用户身份运行(右键单击,以用户身份运行)

我需要能够检查哪个用户启动了它,如果不是特定用户,则停止

我找到的所有示例都显示了登录用户


如何访问执行用户名的应用程序?

这可能会帮助您:

它是用C语言编写的,但很容易转换成VB.NET,只需在谷歌上搜索“C到VB”:)

基于,在我的一点帮助下,我得出了以下结论:

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
导入System.Runtime.InteropServices
导入System.Security.Principal
公共类GetProcessOwner
_
作为布尔值的公共共享函数OpenProcessToken(ByVal processHandle作为IntPtr,ByVal desiredAccess作为整数,ByRef tokenHandle作为IntPtr)
端函数
_
公共共享函数CloseHandle(ByVal hObject作为IntPtr)作为布尔值
端函数
私有常量令牌查询为UInteger=&H8
公共共享函数GetProcessOwner(ByVal processName作为字符串)作为字符串
Dim ownerName As String=String.Empty
对于Process.GetProcesses()中的每个p As进程
如果p.ProcessName=ProcessName,则
调暗ph值为IntPtr=IntPtr.0
尝试
OpenProcessToken(p.Handle,TokenQuery,ph)
作为WindowsIdentity的Dim wi=新WindowsIdentity(ph)
ownerName=wi.Name
特例
ownerName=String.Empty
最后
如果ph值为零,则
闭合手柄(ph)
如果结束
结束尝试
如果结束
下一个
返回所有者名称
端函数
末级