GetComputerName using Win32API

# GetComputerName using Win32API

require 'Win32API'

def get_computer_name()

    params = [ 'P', # lpBuffer
                      'P' # lpnSize
                   ]

    rVal = 'L'

    myname = "\0" * 255
    mysize = [255].pack('L')
    
    getComputerNameA = Win32API.new("kernel32","GetComputerNameA",params,rVal)
    results = getComputerNameA.call(myname,mysize)

    return myname.gsub(/\000/,''), mysize.unpack('L')[0]
end

p get_computer_name

···

----- Original Message -----
  From: LeRoy, Brian D.
  To: 'Shashank Date'
  Sent: Thursday, March 20, 2003 3:16 PM
  Subject: RE: win32api createprocess trouble

  Thanks for the help! There's something I'm not understanding here. I had the snapshot working in a previous app, but now I'm having trouble. If you have time, my simple sample test app is resulting in a seg fault:

  params = [

              'P', #lpBuffer

              'L' #lpnSize

              ]

  rVal = 'L'

  myname = '' * 16

  mysize = 16

  GCM = Win32API.new("kernel32","GetComputerNameA",params,rVal)

  results = GCM.call(myname,0)

  What am I missing? Is it something obvious?

Hi,

···

At Fri, 21 Mar 2003 11:07:12 +0900, Shashank Date wrote:

GetComputerName using Win32API

require ‘Win32API’

def get_computer_name()

require ‘socket’

p Socket.gethostname


Nobu Nakada

Shashank Date wrote:

GetComputerName using Win32API

require ‘Win32API’

def get_computer_name()

require ‘socket’

p Socket.gethostname

Also:

ENV[“COMPUTERNAME”]

(Although that may not work across all Windows … it should work NT/2k/XP)