Andrey Neverov
Andrey Neverov

Reputation: 2165

Remote installing of windows service

I need to remotely install windows service on number of computers, so I use CreateService() and other service functions from winapi. I know admin password and user name for machines that I need access to. In order to gain access to remote machine I impersonate calling process with help of LogonUser like this:

//all variables are initialized correctly  
int status = 0;        
status = LogonUser(lpwUsername,        
               lpwDomain,         
                   lpwPassword,          
                   LOGON32_LOGON_NEW_CREDENTIALS,       
                   LOGON32_PROVIDER_DEFAULT,   
                   &hToken);            


if (status == 0)    
{   
         //here comes a error  
}  

status = ImpersonateLoggedOnUser(hToken);     
if (status == 0)                
{     
    //once again a error     
}      

//ok, now we are impersonated, do all service work there

So, I gain access to machine in a domain, but some of computers are out of domain. On machines that are out of domain this code doesn't work. Is there any way to access service manager on machine out of domain?

Upvotes: 0

Views: 1592

Answers (3)

Andrey Neverov
Andrey Neverov

Reputation: 2165

OK, problem resolved (not really very good, but rather OK). I used WNetAddConnection() to ipc$ on remote machine.

Upvotes: 0

Jon Grant
Jon Grant

Reputation: 11530

Rather than rolling your own, why not just use the SC built-in command?

Upvotes: 0

Tony Edgecombe
Tony Edgecombe

Reputation:

You can do it , the account needs to exist on the remote machine and you need to use the machine name for the domain name in the LogonUser call.

Upvotes: 2

Related Questions