# Remote Login to another computer

**URL:** <https://www.ruby-forum.com/t/remote-login-to-another-computer/227905>\
**Category:** Ruby\
**Created:** [April 2, 2013, 9:08pm UTC](https://www.ruby-forum.com/t/remote-login-to-another-computer/227905 "2013-04-02T21:08:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hotjuneshen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f19dbf/32.png) [@hotjuneshen](https://www.ruby-forum.com/u/hotjuneshen)\
**Post date:** [April 2, 2013, 9:08pm UTC](https://www.ruby-forum.com/t/remote-login-to-another-computer/227905/1 "2013-04-02T21:08:47Z")

</div>

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](http://msdn.microsoft.com/en-us/library/aa385413%28VS.85%29.aspx), 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!

---

<div class="post-metadata">

**Author:** ![hotjuneshen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f19dbf/32.png) [@hotjuneshen](https://www.ruby-forum.com/u/hotjuneshen)\
**Post date:** [April 9, 2013, 4:20am UTC](https://www.ruby-forum.com/t/remote-login-to-another-computer/227905/2 "2013-04-09T04:20:36Z")

</div>

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/](http://ruby.elevatedintel.com/blog/connecting-to-a-remote-machine-using-win32api-and-ruby/)
