Link to home
Start Free TrialLog in
Avatar of rajani_n
rajani_n

asked on

Remote login

Hi,
  I need to connect to a machine programtically as a specific user and retrieve the contents of a file. Can anyone suggest which API would be appropriate? LogonUser() API creates a session on the localhost, whereas I want to login to the server as a specific user.

Sample code would be appreciated.

thanks in advance.


Avatar of jkr
jkr
Flag of Germany image

Use "LogonUser()" - it also works remotely. All you have to do is specify "<machine>\<username" as the UserId.
Avatar of rajani_n
rajani_n

ASKER

I tried providing <machine>\<username>. however it does not seem to be working. Any thing else i need to do?
So you basically want to connect to a remote computer, open a file, do whatever you have to do, close the file, and disconnect.
I would use the WNetAddConnection2() API.  Here is some code:

BOOL connect(string servername, string share
             string user, string pw)
{
   DWORD dwConnectStatus;
   BOOL     bResult = FALSE;  // assume failure
   char     fullpath[50] = {0};
   int     x;
   NETRESOURCE     n;  // network resource struct

   ZeroMemory( &n, sizeof( NETRESOURCE ) );

   // concat server and share for remote name
   wsprintf( fullpath, "%s%s", server,sharename );

   // set network resource
   n.dwType = RESOURCETYPE_DISK;
   n.lpRemoteName = (LPTSTR) fullpath;
   n.lpLocalName =  (LPTSTR) driveletter;

   print("Attempting to connect to network");

   // Only loop max 10 times - we can't wait forever!
   for( x=0; x<10; x++ )
   {
      // attempt connection
      dwConnectStatus =
         WNetAddConnection2( &n, password, username, 0 );

      if( dwConnectStatus == NO_ERROR )
      {
         bResult = TRUE;
      break;
      }

      // non-fatal, disconnect and try again
      if( dwConnectStatus == ERROR_ALREADY_ASSIGNED )
     dwConnectStatus =
        WNetCancelConnection2( driveletter,  
                    CONNECT_UPDATE_PROFILE, TRUE );
     else
     {
     // connection error - log and exit
     LogError(dwConnectStatus, n);    
          break;
     }
   }
     
   // set state
   bConnected = bResult;

   return bResult;
}

Open the file as you would any other file...

Call:
WNetCancelConnection2( driveletter,  
                       CONNECT_UPDATE_PROFILE, TRUE );

when you are done.

Let me know if this helps.
Jim
>>however it does not seem to be working

What error do you get?
I tried using the code provided by Jim, I get an error 1219(The credentials supplied conflict with an existing set of credentials.) when I supply the username and pwd to be different than the one who has logged into the remote system. However, the code works if I supply the UserId and password of the user who has logged into remote system.

My apllication will be providing a random user.

Well, what error do you get with 'LogonUser()'? Do you want to _connect_ to a remote machine or do a _logon_?
That error occurs if are already attached to the remote computer as another user.  Windows only allows one login per remote connection.  So either disconnect from the remote computer, or use your login/password.
When I had to do this, the local machine did not have any connections to the remote computer until the WNetAddConnection2() call.  
If your app provides a random user, you just have to make sure that the user is on the system and has access to the files.

jim
LogonUser(..) call fails by returning false when I provider <machine>/<username>.

My requirement is as follows: I need to access a share on a remote machine that has specific user permissions for read. From my system, I will provide the username/password(these may be different from the currenly logged in user) to access the share and should be able to access the files located in that share.
You cannot use LogonUser() to connect to a remote computer.  

From MSDN:
The LogonUser function attempts to log a user on to the local computer, that is, to the computer from which LogonUser was called. You cannot use LogonUser to log on to a remote computer... etc.

As per your requirements, WNetAddConnection2() will do all of that for you.  You must remember the credentials conflict when using it - no mapped drives or connections when connecting as someone else.

The code I provided does work.  You can remove the looping since that was for a specific purpose.  One call should determine if your connection was valid.
>>You cannot use LogonUser() to connect to a remote computer.  

Yes, you can. The terminology is misleading here, as a little lower it states

LOGON32_LOGON_INTERACTIVE

This logon type is intended for users who will be interactively using the machine, such as a user being logged on by a terminal server, remote shell, or similar process.  

'LogonUser()' doesn't do a graphical logon of course, but creates a user token.
Hi,
  I found that NetUseAdd(.) api allows to login to remote computer from documentation. Can anyone provide me with the work code? I tried using it, however m geting an "access voilation" error. All the other APIs like NetGetDCName(..) works fine for me.

Any help will be appreciated.

Thanks
Rajani
See http://www.mvps.org/win32/network/nusea.cpp for a fully featured sample on 'NetUseAdd()'
Hi Jim,
   after making the connection, I tried retiving the file by specifying the UNC name, "\\\\server\\share\\file". However, it does not seem to be working. Can you please helpe me figure out what the problem could be.

I am using the code supplied by you. Initially, it did not work as I had a mapped connection on my m/c. I took it off and it works.

thanks
Rajani
After making the connection you should have a drive letter for the remote connection.  Use the drive instead of the UNC name.

Jim
Also I wanted to know if WNetAddConnection* APIs or NetUseAdd APIs are used, how can we maintain the sessions. I don't get any handle using these APIs. Also, I don't want to use the drive name, it will be NULL. So if I open some 5 connections at a time, how will I know which one was for which of the connections?

ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial