Windows Service cannot access mapped network drive
-
Dear all, I have a console application that writes data to shared network drives. No problem if I launch it from command prompt or Windows explorer. But things go wrong if I call it from a Windows service created with sc command, The appliaction runs in memeory that is for sure, but for some reason it cannot get acess to network drives. ( both the console application and the windows service are wriiten in VC8 ) Please Help Regards
-
Dear all, I have a console application that writes data to shared network drives. No problem if I launch it from command prompt or Windows explorer. But things go wrong if I call it from a Windows service created with sc command, The appliaction runs in memeory that is for sure, but for some reason it cannot get acess to network drives. ( both the console application and the windows service are wriiten in VC8 ) Please Help Regards
-
-
Dear all, I have a console application that writes data to shared network drives. No problem if I launch it from command prompt or Windows explorer. But things go wrong if I call it from a Windows service created with sc command, The appliaction runs in memeory that is for sure, but for some reason it cannot get acess to network drives. ( both the console application and the windows service are wriiten in VC8 ) Please Help Regards
Hi, The reason why you are able to access the drive in when you normally run the executable from command prompt is that when u are executing it as normal exe you are running that application in the User account from which you have logged on . And that user has the privileges to access the network. But , when you install the executable as a service , by default if you see in the task manage it runs under 'SYSTEM' account . And you might be knowing that the 'SYSTEM' doesn't have rights to access network resources. There can be two solutions to this problem. 1. As Sauro Viti already pointed out in the link he gave you , to map the drive as persistent. 2. There is one more approach that can be followed. If you open the service manager by typing in the 'services.msc'you can go to your service and in the properties of your service there is a logOn tab where you can specify the account as any other account than 'System' you can either start service from your own logged on user account or through 'Network Service'. When you do this .. the service can access any network component and drive even if they are not persistent also. To achieve this programmatically you can look into 'CreateService' function at http://msdn.microsoft.com/en-us/library/ms682450(v=vs.85).aspx and can set the parameter 'lpServiceStartName ' to 'NT AUTHORITY\NetworkService'. This will start your service under 'Network Service' account and then you are done. 3. You can also try by making the service as interactive by specifying SERVICE_INTERACTIVE_PROCESS in the servicetype parameter flag of your CreateService() function but this will be limited only till XP as Vista and 7 donot support this feature. Hope the solutions help you.. Let me know if this worked for you . -Kushagra
-
Hi, The reason why you are able to access the drive in when you normally run the executable from command prompt is that when u are executing it as normal exe you are running that application in the User account from which you have logged on . And that user has the privileges to access the network. But , when you install the executable as a service , by default if you see in the task manage it runs under 'SYSTEM' account . And you might be knowing that the 'SYSTEM' doesn't have rights to access network resources. There can be two solutions to this problem. 1. As Sauro Viti already pointed out in the link he gave you , to map the drive as persistent. 2. There is one more approach that can be followed. If you open the service manager by typing in the 'services.msc'you can go to your service and in the properties of your service there is a logOn tab where you can specify the account as any other account than 'System' you can either start service from your own logged on user account or through 'Network Service'. When you do this .. the service can access any network component and drive even if they are not persistent also. To achieve this programmatically you can look into 'CreateService' function at http://msdn.microsoft.com/en-us/library/ms682450(v=vs.85).aspx and can set the parameter 'lpServiceStartName ' to 'NT AUTHORITY\NetworkService'. This will start your service under 'Network Service' account and then you are done. 3. You can also try by making the service as interactive by specifying SERVICE_INTERACTIVE_PROCESS in the servicetype parameter flag of your CreateService() function but this will be limited only till XP as Vista and 7 donot support this feature. Hope the solutions help you.. Let me know if this worked for you . -Kushagra
Hi Kushagra, Thanks a lot for your help. I will just try with the methods you described. I have also came accros some posts where it is said to use CreateProcessAsUser function to launch the child process. But I could find a proper code to get the "Token Handle" to be used as a parameter to this function. Could you please help me in this area. ? with thanks and regards Ranojay
-
Hi Kushagra, Thanks a lot for your help. I will just try with the methods you described. I have also came accros some posts where it is said to use CreateProcessAsUser function to launch the child process. But I could find a proper code to get the "Token Handle" to be used as a parameter to this function. Could you please help me in this area. ? with thanks and regards Ranojay
Ranojay wrote:
I have also came accros some posts where it is said to use CreateProcessAsUser function to launch the child process. But I could find a proper code to get the "Token Handle" to be used as a parameter to this function. Could you please help me in this area. ?
For doing this .. First you will have to make the service as 'INTERACTIVE' as I mentioned in the earlier reply. Then if u are using the function CreateProcessAsUser() you will have to fallow the process as mentioned in this link : http://msdn.microsoft.com/en-us/library/aa379608(VS.85).aspx This should solve your problem. -Kushagra PS: If you like my Answers .. Please rate them ... That will give me a lot of confidence :)
modified on Wednesday, September 29, 2010 9:23 AM