473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with services

I am very new to .NET and Windows programming in general so please bare with
me...

I created a Windows service by deriving a class from ServiceBase and
installing it via the 'sc create' commands as follows:

sc create <name> binPath= <path>

Where path is the fully qualified path name. The new service appeared in the
Services GUI and I was able to start and stop it without problems. Next I
recreated the service via the 'sc create' command where binPath used a mapped
drive letter via the subst command i.e. 'subst x: c:\actual\path\name' The
service is created but it does not start and I get the following error
message:

Could not start the <name> service on Local Computer. Error 3: The system
cannot find the path specified.

Unfortunately it is a requirement of my project to be able to used a mapped
driver letter. It appears that when the service is started it has no
knowledge of the drive mappings in my environment. After some research it
also appears that using the subst command is discouraged in later versions of
Windows and the preferred method is to use net commands. I have not been
entirely successful with the net commands and hope there is another approach
to solving this problem. Any suggestions???
Mar 1 '06 #1
17 2540
AFAIK, threre is a problem with .NET apps to work from mapped drive, because
system can't work (use metadata) from that drive.

I reckon u need to write unmanaged service (C++/VB), and it should work

"djmob" wrote:
I am very new to .NET and Windows programming in general so please bare with
me...

I created a Windows service by deriving a class from ServiceBase and
installing it via the 'sc create' commands as follows:

sc create <name> binPath= <path>

Where path is the fully qualified path name. The new service appeared in the
Services GUI and I was able to start and stop it without problems. Next I
recreated the service via the 'sc create' command where binPath used a mapped
drive letter via the subst command i.e. 'subst x: c:\actual\path\name' The
service is created but it does not start and I get the following error
message:

Could not start the <name> service on Local Computer. Error 3: The system
cannot find the path specified.

Unfortunately it is a requirement of my project to be able to used a mapped
driver letter. It appears that when the service is started it has no
knowledge of the drive mappings in my environment. After some research it
also appears that using the subst command is discouraged in later versions of
Windows and the preferred method is to use net commands. I have not been
entirely successful with the net commands and hope there is another approach
to solving this problem. Any suggestions???


--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

Mar 1 '06 #2
The cause of this is you have to remember that the Service has to be
running under a certain context.
Whenever any process is run on windows it is run within a Context. (A
context being things like user credentials, current working directory
etc)

When the service starts the CWD will be system32 since it was invoked
by the SCM eg c:\windows\system32

You must change the working directory to the directory of where your
executable in your OnStart or in your Main() of your service
implementation you can do this by doing the following:

protected override void OnStart(string[] args)
{

//EventLog.WriteEntry("Current working Directory Before
Assignment:"+Environment.CurrentDirectory);

Environment.CurrentDirectory =
System.AppDomain.CurrentDomain.BaseDirectory;

//EventLog.WriteEntry("Current working Directory
is:"+Environment.CurrentDirectory);

//rest of OnStart Code here...
}


djmob wrote:
I am very new to .NET and Windows programming in general so please bare with
me...

I created a Windows service by deriving a class from ServiceBase and
installing it via the 'sc create' commands as follows:

sc create <name> binPath= <path>

Where path is the fully qualified path name. The new service appeared in the
Services GUI and I was able to start and stop it without problems. Next I
recreated the service via the 'sc create' command where binPath used a mapped
drive letter via the subst command i.e. 'subst x: c:\actual\path\name' The
service is created but it does not start and I get the following error
message:

Could not start the <name> service on Local Computer. Error 3: The system
cannot find the path specified.

Unfortunately it is a requirement of my project to be able to used a mapped
driver letter. It appears that when the service is started it has no
knowledge of the drive mappings in my environment. After some research it
also appears that using the subst command is discouraged in later versions of
Windows and the preferred method is to use net commands. I have not been
entirely successful with the net commands and hope there is another approach
to solving this problem. Any suggestions???


Mar 1 '06 #3
Are mapped drives not only mapped when a user logs in? Thus how would the
service start when the computer was restarted and the mapped drives does not
exist?
"djmob" <u19229@uwe> wrote in message news:5c9b5a53c89d9@uwe...
I am very new to .NET and Windows programming in general so please bare
with
me...

I created a Windows service by deriving a class from ServiceBase and
installing it via the 'sc create' commands as follows:

sc create <name> binPath= <path>

Where path is the fully qualified path name. The new service appeared in
the
Services GUI and I was able to start and stop it without problems. Next I
recreated the service via the 'sc create' command where binPath used a
mapped
drive letter via the subst command i.e. 'subst x: c:\actual\path\name'
The
service is created but it does not start and I get the following error
message:

Could not start the <name> service on Local Computer. Error 3: The system
cannot find the path specified.

Unfortunately it is a requirement of my project to be able to used a
mapped
driver letter. It appears that when the service is started it has no
knowledge of the drive mappings in my environment. After some research it
also appears that using the subst command is discouraged in later versions
of
Windows and the preferred method is to use net commands. I have not been
entirely successful with the net commands and hope there is another
approach
to solving this problem. Any suggestions???

Mar 1 '06 #4
Thanks alot for the help. I will give it a shot and see what happens.

Amir_Hasan wrote:
The cause of this is you have to remember that the Service has to be
running under a certain context.
Whenever any process is run on windows it is run within a Context. (A
context being things like user credentials, current working directory
etc)

When the service starts the CWD will be system32 since it was invoked
by the SCM eg c:\windows\system32

You must change the working directory to the directory of where your
executable in your OnStart or in your Main() of your service
implementation you can do this by doing the following:

protected override void OnStart(string[] args)
{

//EventLog.WriteEntry("Current working Directory Before
Assignment:"+Environment.CurrentDirectory);

Environment.CurrentDirectory =
System.AppDomain.CurrentDomain.BaseDirectory;

//EventLog.WriteEntry("Current working Directory
is:"+Environment.CurrentDirectory);

//rest of OnStart Code here...

}
I am very new to .NET and Windows programming in general so please bare with
me...

[quoted text clipped - 21 lines]
entirely successful with the net commands and hope there is another approach
to solving this problem. Any suggestions???

Mar 1 '06 #5


"djmob" <u19229@uwe> wrote in message news:5c9b5a53c89d9@uwe...
|I am very new to .NET and Windows programming in general so please bare
with
| me...
|
| I created a Windows service by deriving a class from ServiceBase and
| installing it via the 'sc create' commands as follows:
|
| sc create <name> binPath= <path>
|
| Where path is the fully qualified path name. The new service appeared in
the
| Services GUI and I was able to start and stop it without problems. Next I
| recreated the service via the 'sc create' command where binPath used a
mapped
| drive letter via the subst command i.e. 'subst x: c:\actual\path\name'
The
| service is created but it does not start and I get the following error
| message:
|
| Could not start the <name> service on Local Computer. Error 3: The system
| cannot find the path specified.
|
| Unfortunately it is a requirement of my project to be able to used a
mapped
| driver letter. It appears that when the service is started it has no
| knowledge of the drive mappings in my environment. After some research it
| also appears that using the subst command is discouraged in later versions
of
| Windows and the preferred method is to use net commands. I have not been
| entirely successful with the net commands and hope there is another
approach
| to solving this problem. Any suggestions???
Service executables should be stored on local drives, what you are trying is
not a supported scenario. The SCM can not start a service that is located on
a remote server, the reason is that SCM (Service.exe) runs as SYSTEM
(localsystem) which is a local security account that has no network access
token. Note that it's possible for SYSTEM to access remote resources from
domain members running in a W2K Active Directory domain, provided that the
"server" starting the service is granted access to the remote share (the
server name is used to authenticate over the network in this case), but even
then there is in guarantee that the service will get loaded, for instance
what if the remote server is not up?
Honestly, I don't see any reason why you need to load a service from a
remote share, I'm I missing something?.

Willy.
Mar 1 '06 #6
Drive mappings are associated with a User who is logged in. a user maps
drives in Explorer. Services are run in general under a system account.
You can change a service user to use a specific account but I believe
that this is so that the Context the service is running in can get
specific priviliges.
I don't think this will work altough you can try it

1) using explorer Create a permenant drive mapping to a network
Location
2) using explorer create a txt file with some test data on that network
drive
3) Write a .NET service that will read that file using the drive
mapping file path and write the context it to a local folder
c:\documents and settings\user\my documents\testfile.txt
4) Register the service account details to be that user
5) Set the service to be automatic startup
6) Restart the machine
Logon to the machine as someone else eg local administrator etc who
does not have the drive mapping to see if the file created. (ie was it
able to read the drive mapping)

I believe you won't be able to do it. You can try.

Mar 1 '06 #7

"Amir_Hasan" <ah****@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
| The cause of this is you have to remember that the Service has to be
| running under a certain context.
| Whenever any process is run on windows it is run within a Context. (A
| context being things like user credentials, current working directory
| etc)
|
| When the service starts the CWD will be system32 since it was invoked
| by the SCM eg c:\windows\system32
|
| You must change the working directory to the directory of where your
| executable in your OnStart or in your Main() of your service
| implementation you can do this by doing the following:
|
| protected override void OnStart(string[] args)
| {
|
| //EventLog.WriteEntry("Current working Directory Before
| Assignment:"+Environment.CurrentDirectory);
|
| Environment.CurrentDirectory =
| System.AppDomain.CurrentDomain.BaseDirectory;
|
| //EventLog.WriteEntry("Current working Directory
| is:"+Environment.CurrentDirectory);
|
| //rest of OnStart Code here...
|
|
| }
|
|
This makes no sense, sorry, the problem is that the service cannot be loaded
so certainly not be started, how would this solve the problem?
Willy.
Mar 1 '06 #8
No, drives can be mapped as long as the 'Workstation' service runs, and they
can be mapped in any logon session.

Willy.

"Arran Pearce" <ar*********@cellhire.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
| Are mapped drives not only mapped when a user logs in? Thus how would the
| service start when the computer was restarted and the mapped drives does
not
| exist?
|
|
| "djmob" <u19229@uwe> wrote in message news:5c9b5a53c89d9@uwe...
| >I am very new to .NET and Windows programming in general so please bare
| >with
| > me...
| >
| > I created a Windows service by deriving a class from ServiceBase and
| > installing it via the 'sc create' commands as follows:
| >
| > sc create <name> binPath= <path>
| >
| > Where path is the fully qualified path name. The new service appeared
in
| > the
| > Services GUI and I was able to start and stop it without problems. Next
I
| > recreated the service via the 'sc create' command where binPath used a
| > mapped
| > drive letter via the subst command i.e. 'subst x: c:\actual\path\name'
| > The
| > service is created but it does not start and I get the following error
| > message:
| >
| > Could not start the <name> service on Local Computer. Error 3: The
system
| > cannot find the path specified.
| >
| > Unfortunately it is a requirement of my project to be able to used a
| > mapped
| > driver letter. It appears that when the service is started it has no
| > knowledge of the drive mappings in my environment. After some research
it
| > also appears that using the subst command is discouraged in later
versions
| > of
| > Windows and the preferred method is to use net commands. I have not
been
| > entirely successful with the net commands and hope there is another
| > approach
| > to solving this problem. Any suggestions???
|
|
Mar 1 '06 #9
Yes that is correct. The problem is with the service being loaded from a
locally mapped drive. Currently the drive is mapped using the subst command
i.e. 'subst x: c:\pathToService'.

Willy Denoyette [MVP] wrote:
| The cause of this is you have to remember that the Service has to be
| running under a certain context.
[quoted text clipped - 24 lines]
|
| }

This makes no sense, sorry, the problem is that the service cannot be loaded
so certainly not be started, how would this solve the problem?

Willy.

Mar 1 '06 #10
When you say the "The SCM can not start a service that is located on
a remote server" you mean even if it is mapped to the local drive i.e. 'subst x: c:\path'.
The reason this is a requirement is that I am turning an existing application
into a service and it is important for us to preserve this drive mapping to
support our current development environment (the exact details of why this is
important is a little sketchy to me too). The only reason I brought up the
net commands is that I thought there was something inherent in the subst
command for it not to work with a service.

As I said before this is very new to me so I hope I am making some sense.

Willy Denoyette [MVP] wrote:|I am very new to .NET and Windows programming in general so please bare with
| me...
[quoted text clipped - 21 lines]
| entirely successful with the net commands and hope there is another approach
| to solving this problem. Any suggestions???

Service executables should be stored on local drives, what you are trying is
not a supported scenario. The SCM can not start a service that is located on
a remote server, the reason is that SCM (Service.exe) runs as SYSTEM
(localsystem) which is a local security account that has no network access
token. Note that it's possible for SYSTEM to access remote resources from
domain members running in a W2K Active Directory domain, provided that the
"server" starting the service is granted access to the remote share (the
server name is used to authenticate over the network in this case), but even
then there is in guarantee that the service will get loaded, for instance
what if the remote server is not up?
Honestly, I don't see any reason why you need to load a service from a
remote share, I'm I missing something?.

Willy.

Mar 1 '06 #11
Your right I was assuming the Service Implementation is within the same
assembly as the executable (the exe assembly with the Main.) If it is
not you have to make sure that Appdomain Private Path contains the
path of the assembly that is implementing the service (and any of its
dependencies) so that the CLR can find it. You will then have to use
Reflection to load it using Activator.CreateInstance

This can be done by appending private path to the appdomain in the
static Main e.g.
static void Main(string [] args)
{
AppDomain.CurrentDomain.AppendPrivatePath("service imp"); //note this
assumes the assembly with YourServiceAssembly.dll is in a subfolder
(with its private dependencies ) called serviceimp
System.ServiceProcess.ServiceBase[] ServicesToRun;
System.ServiceProcess.ServiceBase impl =
Activator.CreateInstance("YourServiceAssembly,Serv iceImpl.MyService");
// note the fully qualified class name is ServiceImpl.MyService where
ServiceImpl is the namespace
ServicesToRun = new System.ServiceProcess.ServiceBase[] { impl };
System.ServiceProcess.ServiceBase.Run(ServicesToRu n);
}

I hope this makes sense.

..

Mar 1 '06 #12
Sorry I am not addressing the underlying issue. Your basically stuffed.
Mapped and substited drives are in terms of a user who has logged in.
the SCM that is running up your service is running under the system
account and so does not know anything about the substituted X:. This is
even before the CLR has loaded or kicked in there is basically nothing
you can do. Please ignore my previous posts

Mar 1 '06 #13
The SCM knows x: is not a real local drive, so it asks the 'Workstation'
service (also called the redirector)to resolve the path, which is not
possible as per my previous explaination, and it's not what you are after
anyway.

What you need to do is:
1. Load your service from a local drive (loading from a remote drive would
not give access to that drive from within your service anyway) and run it as
a user account with access privileges to the remote share, that is as:
- a domain account or
- a shadow account, that is a local account with the same credentials (user
name and password) that exists on both servers.

2. Don't use drive letters to access the remote share, use UNC paths instead
(like : \\server\shared\filename ).

If you can't use UNC paths (I see no reason why not), you need to map the
drive from withing your service.
This can be done using:
1 - PInvoke "NetUserAdd" or "WNetAddConnection2, or
2 - a by invoking the utility "net.exe" using Process.Start from within your
service.
complete command looks something like:
net use x: \\servername\share /user:remoteservername\userName pwd
where: remoteservername is the name of the remote machine
userName is the name o a user on above with valid access privileges
to the share
pwd is it's password.

Willy.
"djmob" <u19229@uwe> wrote in message news:5c9c092af53d7@uwe...
| Yes that is correct. The problem is with the service being loaded from a
| locally mapped drive. Currently the drive is mapped using the subst
command
| i.e. 'subst x: c:\pathToService'.
|
| Willy Denoyette [MVP] wrote:
| >| The cause of this is you have to remember that the Service has to be
| >| running under a certain context.
| >[quoted text clipped - 24 lines]
| >|
| >| }
| >
| >This makes no sense, sorry, the problem is that the service cannot be
loaded
| >so certainly not be started, how would this solve the problem?
| >
| >Willy.
Mar 1 '06 #14
I think your right. Thanks for the help.

Amir_Hasan wrote:
Sorry I am not addressing the underlying issue. Your basically stuffed.
Mapped and substited drives are in terms of a user who has logged in.
the SCM that is running up your service is running under the system
account and so does not know anything about the substituted X:. This is
even before the CLR has loaded or kicked in there is basically nothing
you can do. Please ignore my previous posts

Mar 1 '06 #15
Sorry but, you got it wrong again, the problem can not be solved in the
service code, the problem is that the Service Control Manager (the SCM)
cannot load the exeutable from the path as defined by the configuration
utility.
The service path is configured like : x:\myservice.exe, but the SCM does not
know what x: is all about so he asks the Workstation service to resolve the
mapping, which is not possible either, because it's the result of "subst"
command done in the interactive users logon context. So, it's not a network
share redirection either, which is finaly what the OP is trying to sumulate
(wrongly).

Willy.

"Amir_Hasan" <ah****@gmail.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
| Your right I was assuming the Service Implementation is within the same
| assembly as the executable (the exe assembly with the Main.) If it is
| not you have to make sure that Appdomain Private Path contains the
| path of the assembly that is implementing the service (and any of its
| dependencies) so that the CLR can find it. You will then have to use
| Reflection to load it using Activator.CreateInstance
|
| This can be done by appending private path to the appdomain in the
| static Main e.g.
| static void Main(string [] args)
| {
| AppDomain.CurrentDomain.AppendPrivatePath("service imp"); //note this
| assumes the assembly with YourServiceAssembly.dll is in a subfolder
| (with its private dependencies ) called serviceimp
| System.ServiceProcess.ServiceBase[] ServicesToRun;
| System.ServiceProcess.ServiceBase impl =
| Activator.CreateInstance("YourServiceAssembly,Serv iceImpl.MyService");
| // note the fully qualified class name is ServiceImpl.MyService where
| ServiceImpl is the namespace
| ServicesToRun = new System.ServiceProcess.ServiceBase[] { impl };
| System.ServiceProcess.ServiceBase.Run(ServicesToRu n);
| }
|
| I hope this makes sense.
|
| .
|
Mar 1 '06 #16

"djmob" <u19229@uwe> wrote in message news:5c9c1a1dd1373@uwe...
| When you say the "The SCM can not start a service that is located on
| >a remote server" you mean even if it is mapped to the local drive i.e.
'subst x: c:\path'.
|
| The reason this is a requirement is that I am turning an existing
application
| into a service and it is important for us to preserve this drive mapping
to
| support our current development environment (the exact details of why this
is
| important is a little sketchy to me too). The only reason I brought up
the
| net commands is that I thought there was something inherent in the subst
| command for it not to work with a service.
|

Service are not regular application, this is something you need to consider
when turning regular applications into Services, something that is common
place since the introduction of .NET.
Everyone seems to need services these day's, which IMO, is terribly wrong in
most circumstances.

I have explained what you have to do in an other reply, but before you do,
make sure you know what services are all about, what they are meant to solve
and what their restrictions are. Failing to do your homework will bite you
sooner that later.
What you need to know to begin with is (non exhaustive!):
- Services are meant to run in a restricted security context, they need
(preferably) to run under service users credentials (localsystem, local
service or network service). Running them under domain credentials is a
security threat.
- The run in a secured desktop (in the background as the sage goes) that has
no access to the active (visible) desktop, the active desktop has no access
to the service(s) desktop(s) either. That means they should not have a UI,
nor should they assume they can access, the UI of a running programs, by
whatever means.
- They don't have access to the profile data of the currently logged on user
(interactive), nor should the assume the presence of such a user.
- They have access to the services hives for their private profile storage,
these are mapped as:
- HKU\.Default or S-1-5-18 for localsystem and all other accounts
running the service (local or domain)
- HKU \S-1-5-19 for Local Service
- HKU \S-1-5-20 for Network Service

None of these hives contain configuration data like printers and persistent
mapped network shares, application paths like MyDocuments, MyPictures etc;
paths to document folders etc...

They run their threads in an MTA, so, be sure your COM objects can live
safely in this context, inspect your COM servers requirements first.
They should not use any library that requires a UI to be present even not a
hidden window (think System.Drawing and managed/unmanaged DirectX).
Willy.


Mar 1 '06 #17
Thanks for the advice and help. Greatly appreciated.

Willy Denoyette [MVP] wrote:
I have explained what you have to do in an other reply, but before you do,
make sure you know what services are all about, what they are meant to solve
and what their restrictions are. Failing to do your homework will bite you
sooner that later.

Mar 1 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Fraser | last post by:
Hi We are trying to debug a problem with services created using py2exe. It seems that these problems have arisen after services were installed and removed a few times. OK, first the actual...
2
by: DB | last post by:
Hi All Having stared at this all morning and altered various things with no effect other then to exasperate the problem i'm wondering if anyone could take a look at the code below and see why on...
2
by: Greg | last post by:
Please note: I have cross posted this from Newsgroup: microsoft.public.dotnet.framework.aspnet.webservices with a few minor changes... I am having a simple problem setting up the security on my...
2
by: yqlu | last post by:
I hava developed a client in C# that is connected to a 3-party XML Web Services developed in Java based on the AXIS 1.1. Most methods call are successful except for one method named "findObjects"...
0
by: graciezzzzz | last post by:
Hi all, I create a Web Service project called 'eSelectService' and another Class Library project called 'MonerisVO', and a Windows Form project called 'maps' as well. The reason why I need...
1
by: leslie_tighe | last post by:
Hello, I have webservice created with Axis 1.2.1 and that I am trying to consuming in .NET (VB) using the Microsoft provided tools. While I am able to consume methods on the service that return...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
1
by: louis_la_brocante | last post by:
Dear all, I am having trouble generating a client proxy for a webservice whose methods return a "complex" type. The type is complex in that it is a class whose members are a mix of primitive...
3
by: Blasting Cap | last post by:
I'm using VS 2005, SQL 2005 reporting services. SQL reporting services is working, and I have it both on my local computer, as well as on a server. I've created a report in the SQL Business...
5
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
This code works fine in Windows Application. In Windows Application, I am able to zip the image files properly and it totally contains 900MB My problem is the same code which I used in my Windows...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.