Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
NetWkstaUserGetInfo (netapi32)
 
.
Summary
Returns information about the currently logged-on user. This function must be called in the context of the logged-on user.

C# Signature:

[DllImport("netapi32.dll", SetLastError=true)]
static extern int NetWkstaUserGetInfo(
    [MarshalAs(UnmanagedType.LPWStr)]string reserved,
    int level, out IntPtr lpBuffer);

VB Signature:

Declare Function NetWkstaUserGetInfo Lib "netapi32.dll" (ByVal reserved As String, ByVal level As Integer, ByRef lpBuffer As IntPtr) As Integer

User-Defined Types:

[StructLayout(LayoutKind.Sequential)]
public struct WKSTA_USER_INFO_1
{
    [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string wkui1_username;

    [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string wkui1_logon_domain;

    [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string wkui1_oth_domains;

    [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
    public string wkui1_logon_server;
}

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    Private Sub GetLoggedInUserInfo()
        Dim lpBuffer As IntPtr
        Dim result As Integer = NetWkstaUserGetInfo(Nothing, 1, lpBuffer)
        If result = 0 Then
            Dim wksInfo As WKSTA_USER_INFO_1 = Marshal.PtrToStructure(lpBuffer, GetType(WKSTA_USER_INFO_1))
            ''' Do something
            NetApiBufferFree(lpBuffer)
        End If
    End Sub

C# Sample

/// <summary>
/// Retrieves the default domain for the currently logged in user.
/// </summary>
/// <returns>The name of the domain currently logged into.</returns>
public static string GetDefaultDomain()
{
    IntPtr buffer = new IntPtr();
    string domain = "";
    int result;
    WKSTA_USER_INFO_1 wksInfo;

    result = NetWkstaUserGetInfo(null, 1, out buffer);
    if (result == 0)
    {
        wksInfo = (WKSTA_USER_INFO_1)Marshal.PtrToStructure(
            buffer, typeof(WKSTA_USER_INFO_1));

        domain = wksInfo.wkui1101_logon_domain;
        System.Windows.Forms.MessageBox.Show(domain);
        NetApiBufferFree(buffer);
    }

    return domain;
}

Please add some more!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions