DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Daniel has posted 5 posts at DZone. View Full User Profile

NetUserGetInfo In Pure Ruby

11.18.2008
| 2133 views |
  • submit to reddit
        This snippet lets you get user information for a particular local or global user account.

require 'windows/error'
require 'windows/unicode'
require 'windows/msvcrt/buffer'
require 'windows/msvcrt/string'
require 'windows/network/management'

include Windows::Error
include Windows::Unicode
include Windows::MSVCRT::Buffer
include Windows::MSVCRT::String
include Windows::Network::Management

# The NetUserGetInfo function requires wide character strings
name = multi_to_wide('some_user')
host = multi_to_wide('your_domain_server')
ptr  = 0.chr * 4

err = NetUserGetInfo.call(host, name, 20, ptr)

if err != NERR_Success
   raise get_last_error(err)
end

ptr = ptr.unpack('L')[0]   # Convert back to a numeric address
usri20_buffer = 0.chr * 20 # USER_INFO_20 Structure

# Reserve space for the name, full name and comment
usri20_name      = 0.chr * 256
usri20_full_name = 0.chr * 256
usri20_comment   = 0.chr * 256

# Get the buffer the pointer points to
memcpy(usri20_buffer, ptr, usri20_buffer.size)

wcscpy(usri20_name, usri20_buffer[0,4].unpack('L')[0])      # usri20_name
wcscpy(usri20_full_name, usri20_buffer[4,4].unpack('L')[0]) # usri20_full_name
wcscpy(usri20_comment, usri20_buffer[8,4].unpack('L')[0])   # usri20_comment

puts "Name: " + wide_to_multi(usri20_name)
puts "Full Name: " + wide_to_multi(usri20_full_name)
puts "Comment: " + wide_to_multi(usri20_comment)

puts "Flags: " + usri20_buffer[12,4].unpack('L')[0].to_s    # usri20_flags
puts "User ID: " + usri20_buffer[16,4].unpack('L')[0].to_s  # usri20_user_id