Get the Current Domain Name in Windows

B

Brian Takita

Hello,

I am trying to find out the domain name for the current user.
This is my first endeavor into dl/win32 and I am having no luck getting
the data.

Here is a reference to the function I am trying to call:
http://msdn.microsoft.com/library/d...en-us/netmgmt/netmgmt/netwkstausergetinfo.asp

Here is a reference to the struct where I am trying to extract the data
from:
http://msdn.microsoft.com/library/d...-us/netmgmt/netmgmt/wksta_user_info_1_str.asp

Here is my code with the results in the header:

# username:
# logon_domain:
# oth_domains:
# logon_server:

require 'dl/win32'

getUserNameInfo = Win32API.new('Netapi32.dll', 'NetWkstaUserGetInfo',
%w{p l P}, 'i')
bufferFree = Win32API.new('Netapi32.dll', 'NetApiBufferFree', %w{p},
'l')

reserved = "\0" * 4
buf = " " * 4 * 255

begin
getUserNameInfo.call(reserved, 1, buf)
username, logon_domain, oth_domains, logon_server = buf.unpack('AAAA')
puts 'username: ' + username
puts 'logon_domain: ' + logon_domain
puts 'oth_domains: ' + oth_domains
puts 'logon_server: ' + logon_server
ensure
bufferFree.call(buf)
end

Thank you,
Brian Takita
 
C

Craig Moran

Try this for a simple solution:

require 'win32ole'
p ENV['USERDOMAIN']

These extra ENV arguments should work as well. To cite the source, I
Googled and found them in this blog:
http://blogs.officezealot.com/charles/archive/2004/12/10/3574.aspx

ALLUSERSPROFILE
APPDATA
AVENGINE
CLIENTNAME
CommonProgramFiles
COMPUTERNAME
ComSpec
FP_NO_HOST_CHECK
HOMEDRIVE
HOMEPATH
INCLUDE
INOCULAN
LIB
LOGONSERVER
NUMBER_OF_PROCESSORS
OS
Path
PATHEXT
PROCESSOR_ARCHITECTURE
PROCESSOR_IDENTIFIER
PROCESSOR_LEVEL
PROCESSOR_REVISION
ProgramFiles
SESSIONNAME
SystemDrive
SystemRoot
TEMP
TMP
USERDOMAIN
USERNAME
USERPROFILE
VS71COMNTOOLS
WecVersionForRosebud.FF0
windir
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top