[DllImport("advapi32.dll", SetLastError = true)]
static extern bool GetUserName(System.Text.StringBuilder sb, ref Int32 length);
<DllImport("advapi32.dll", EntryPoint:="GetUserNameW", SetLastError:=True)> _
Public Function GetUserName(<MarshalAs(UnmanagedType.LPTSTR)>sb As System.Text.StringBuilder, _
ByRef length As Integer) As <MarshalAs(UnmanagedType.Bool)>Boolean
End Function
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, ByRef nMax As Integer) As Boolean
None.
Environment.UserName (System.Environment)
None.
Please add some!
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace GetUserNameExample
{
class Class1
{
[DllImport("Advapi32.dll")]
static extern bool GetUserName(StringBuilder lpBuffer, ref int nSize);
[STAThread]
static void Main(string[] args)
{
StringBuilder Buffer = new StringBuilder(64);
int nSize=64;
GetUserName(Buffer, ref nSize);
Console.WriteLine(Buffer.ToString());
}
}
}
Imports System
Imports System.Text
Imports System.Runtime.InteropServices
Namespace GetUserNameExample
Class Class1
<DllImport("advapi32.dll", EntryPoint:="GetUserNameA", SetLastError:=True)> _
Public Function GetUserName(<MarshalAs(UnamagedType.LPTSTR)>System.Text.StringBuilder sb, _
ByRef length As Integer) As <MarshalAs(UnmanagedType.Bool)>Boolean
End Function
<STAThread> _
Public Sub Main()
Dim Buffer = new StringBuilder(64)
Dim nSize As Integer =64
GetUserName(Buffer, nSize)
Console.WriteLine(Buffer.ToString())
End Sub
End Class
End Namespace
Dim xstr as String = Space(255)
Dim max as Integer = 255
Dim rc as Integer
rc = GetUserName(xstr,max)
' max will now contain the total number of
' characters written to the buffer (here: xstr)
MessageBox.Show(Mid(xstr,1,max))
$sig = @'
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool GetUserName(System.Text.StringBuilder sb, ref Int32 length);
'@
Add-Type -MemberDefinition $sig -Namespace Advapi32 -Name Util
$size = 64
$str = New-Object System.Text.StringBuilder -ArgumentList $size
[Advapi32.util]::GetUserName($str,[ref]$size) |Out-Null
$str.ToString()