Remote Login to another computer

I want to remote login to another computer (one that I have access to)
through ruby code. The following is what I have so far:

require ‘Win32API’

CONNECT_UPDATE_PROFILE = 0x1
RESOURCETYPE_ANY = 0x3
remote_name = “\\remote_computer”

netresource_struct = [
RESOURCETYPE_ANY, # dwType
nil, # lpLocalName
remote_name, # lpRemoteName
nil, # lpProvider
]

add_connection = Win32API.new(‘mpr’, ‘WNetAddConnection2’, ‘PPPP’, ‘I’)

return_value = add_connection.call(
netresource_struct.pack(‘LPPP’), # lpNetResource
“1234”, # lpPassword
“admin”, # lpUsername
nil # dwFlags
)

puts “connecting to #{remote_name}”
puts “#{return_value}”

I’m getting the right path when it says the remote_name, but I am
receiving a return value of 487 - ERROR_INVALID_ADDRESS. According to
WNetAddConnection2A function (winnetwk.h) - Win32 apps | Microsoft Learn, the
invalid address means that:

An attempt was made to access an invalid address. This error is returned
if the dwFlags parameter specifies a value of CONNECT_REDIRECT, but the
lpLocalName member of the NETRESOURCE structure pointed to by the
lpNetResource parameter was unspecified.

I am not sure how to remedy this. If anyone could take a look at this, I
would greatly appreciate it. Thank you!

The issue you’re running in to comes from not specifying the NETRESOURCE
correctly. I wrote a more detailed explanation with links to all the
documentation over at my blog that provides my take on a solution:
http://ruby.elevatedintel.com/blog/connecting-to-a-remote-machine-using-win32api-and-ruby/