# Win32-service: error 5: access denied

**URL:** <https://www.ruby-forum.com/t/win32-service-error-5-access-denied/109072>\
**Category:** Ruby\
**Created:** [July 11, 2007, 10:20am UTC](https://www.ruby-forum.com/t/win32-service-error-5-access-denied/109072 "2007-07-11T10:20:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [July 11, 2007, 10:20am UTC](https://www.ruby-forum.com/t/win32-service-error-5-access-denied/109072/1 "2007-07-11T10:20:34Z")

</div>

hello altogether!

I have written an win32 service like explained in O’Reilys “Ruby  
Cookbook” on page 750.

when i try to start the service is get the following error:  
Zugriff verweigert (Win32::ServiceError)  
or when i try to start the service manually:  
Fehler 5: Zugriff verweigert  
(in english: error 5: access denied)

does anyone know how to fix this?

thnx! have a nice day!  
Michael

---

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [July 12, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/win32-service-error-5-access-denied/109072/2 "2007-07-12T16:42:53Z")

</div>

does nobody have any information?

thnx!

---

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [July 12, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/win32-service-error-5-access-denied/109072/3 "2007-07-12T16:55:33Z")

</div>

On 7/11/07, Michael D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> does anyone know how to fix this?

What system do you have? XP/W2k/vista?

Is the service already registered? (Can you see it in services?)  
Can you start it with net start ?  
Are you admin? Uberadmin in vista?  
Can you start/stop other services?

Could you post the code you are trying to start?  
Could you post the actual command (what did you do when you tried to  
start the service)?

What do you do in your service?  
You cannot use stdin, stdout, stderr; You don’t see mapped/subst-ed  
disks, You might not have rights to some files - you are most probably  
running under SYSTEM user.

(Now I see this looks like a confession recipe 😉

J.

---

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [July 12, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/win32-service-error-5-access-denied/109072/4 "2007-07-12T17:00:22Z")

</div>

Hi Jano!

thnx for post!!

I use winXP, sp2.  
Yeah service is registered. in “services” the status is “manual”  
Yes im Admin an yes i can start other Services.

so here is my code:

* * *

require ‘rubygems’  
require ‘win32/service’  
include Win32

SERVICE\_NAME = “cProjectsOutlookEventsListener3”  
SERVICE\_DISPLAYNAME = “cProjectsOutlookEventsListener3”

if ARGV[0] == “register”  
#startet den service  
svc = Service.new  
svc.create\_service{ |s|  
s.service\_name = SERVICE\_NAME  
s.display\_name = SERVICE\_DISPLAYNAME  
s.binary\_path\_name = 'c:\ruby\bin ’ + File.expand\_path($0)  
s.dependencies = []  
}  
svc.close  
puts "Service registriert: "+SERVICE\_DISPLAYNAME

elsif ARGV[0] == “start”  
Service.start(SERVICE\_NAME)  
puts “Service gestartet”

elsif ARGV[0] == “delete”  
#stop service  
if Service.status(SERVICE\_NAME).current\_state == “running”  
Service.stop(SERVICE\_NAME)  
end  
Service.delete(SERVICE\_NAME)  
puts "Service gelÃ¶scht: " + SERVICE\_NAME

else

# schreibe parameterinfo

puts “Use: ruby outlook\_rubysvc.rb [option]”  
puts " options:"  
puts " register - Registriert und starte den Service"  
puts " delete - Stoppt und lÃ¶scht den Service"  
exit  
end

# hier ist der eigentliche Service-code

class Daemon  
def service\_init  
# Wartezeit bis der Service richtig initialisiert worden ist  
sleep 10  
end

def service\_main  
fileCount = 0  
watchForFile = “C:\findme.txt”  
while state == RUNNING  
sleep 5  
if File.exists? watchForFile  
fileCount += 1  
File.rename watchForFile, watchForFile + “.” + fileCount.to\_s  
end  
end  
end

d = Daemon.new  
d.mainloop  
end

* * *

thnx! Michael  
Jano S. wrote:

> On 7/11/07, Michael D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > does anyone know how to fix this?
> 
> What system do you have? XP/W2k/vista?
> 
> Is the service already registered? (Can you see it in services?)  
> Can you start it with net start ?  
> Are you admin? Uberadmin in vista?  
> Can you start/stop other services?
> 
> Could you post the code you are trying to start?  
> Could you post the actual command (what did you do when you tried to  
> start the service)?
> 
> What do you do in your service?  
> You cannot use stdin, stdout, stderr; You don’t see mapped/subst-ed  
> disks, You might not have rights to some files - you are most probably  
> running under SYSTEM user.
> 
> (Now I see this looks like a confession recipe 😉
> 
> J.

---

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [July 12, 2007, 6:02pm UTC](https://www.ruby-forum.com/t/win32-service-error-5-access-denied/109072/5 "2007-07-12T18:02:39Z")

</div>

On 7/12/07, Michael D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘rubygems’  
> require ‘win32/service’  
> include Win32
> 
> SERVICE\_NAME = “cProjectsOutlookEventsListener3”  
> SERVICE\_DISPLAYNAME = “cProjectsOutlookEventsListener3”

Offtopic: You can write the following as

case ARGV[0]  
when “register”  
…  
when “start”  
…  
else  
…  
end

> if ARGV[0] == “register”  
> #startet den service  
> svc = Service.new  
> …
> 
> # hier ist der eigentliche Service-code
> 
> class Daemon  
> def service\_init  
> # Wartezeit bis der Service richtig initialisiert worden ist  
> sleep 10  
> end

Try this: (the only possible place where it can raise “access denied”  
exception is when you rename the file (I suppose you can’t do that  
while it’s open). If it does not help, create a log file and store the  
exception stack trace to it (i.e. enclose everything in begin /  
rescue.

> def service\_main

- 

```
fileCount = 0

```

- 

```
fileCount = 0

```

> ```
> watchForFile = "C:\findme.txt"
> while state == RUNNING
> sleep 5
> if File.exists? watchForFile
> 
> ```

- 

```
    fileCount += 1

```

- 

```
    File.rename watchForFile, watchForFile + "." + fileCount.to_s

```

- 

```
   begin

```

- 

```
     File.rename watchForFile, watchForFile + "." + fileCount.to_s

```

- 

```
     fileCount += 1

```

- 

```
    rescue

```

- 

```
    end

```

> ```
> end
> end
> 
> ```
> 
> end

Of course, I assume the program works when run standalone, from the  
cmd.exe.

Access permissions of NTFS might be in the game as well. You may try  
playing with runas /user:SYSTEM, too.

J.
