service Application.StartupPath - visual-studio-2010

Is there an equivalent to Application.StartupPath when writing a service in VB2010? I would like to write some data to a simple log file in the directory where the service executable is installed at (ProgramFilesFolder Manufacturer ProductName) but since it is a service and not an application it bombs out.

Related

Running a GHC created windows executable as a service

I have compiled a program on Windows Server 2008 using GHC 7.6.3 32-bit. I'm attempting to run it via a service within windows on boot-up (and ideally keep it up). To do so I have created a service with the following command successfully
sc create stworker binPath= "C:\Users\vagrant\Desktop\worker.exe"
The problem I'm having is that when I attempt to start the service I receive the following error (see image below).
The executable runs fine when I double click it. So not sure why Windows wouldn't allow the service to be run.
I don't think this is an issue w/ GHC. I'm under the assumption that GHC compiles to native code and not MSIL.
So any ideas why I can't run my executable as a service?
As mentioned in the comments, you need to actually implement the Win32 service API for your program to behave as a Windows service, and you can do this using the Win32-services package.
There's also a wrapper package Win32-services-wrapper that I wrote that aims to provide some of the boilerplate and handle logging, so that defining a service looks like this:
main =
defineService $
Service {
serviceName = "Service",
-- Start the service given a debug handle to write to.
-- Make sure not to use stdout or stderr as they don't exist.
-- Any state needed by serviceStop can be returned here -
-- the Service type takes the type of the state as a type parameter
serviceStart = \debugHandle -> ...,
-- Stop the service given the service state returned by serviceStart
serviceStop = \serviceState -> ...
}
There's a real example of using it in darcsden.

Topshelf, NLog and File Permissions

I have a Windows service application that uses the TopShelf library and I'm installing it in AWS during the cfn-init using the handy command line features that you get with topshelf.
C:\handy_service\> HandyService.exe install start
This basically installs the service in the registry and then calls sc start, but it's quite useful because it checks the service name matches what you expect and it allows you to configure the user that service will run as using the nice fluent API.
The installer code also writes some diagnostic logs to NLog if the service is configured to use NLog in general.
The problem is this: the installer runs as the default local administrator account that the AMI starts with and the NLog file gets created by this user. When the service starts up as the Network Service user, it doesn't have permission to write to the NLog log file.
How can I get my service to write to the log file? I've thought about setting the permissions programmatically but it looks nasty and I'd have to determine the log file name as this is generated dynamically based on the ec2 instance id. Also, it's not entirely obvious at what point the log file is first created. The easiest hack that I might go with is having two NLog.configs and switching one out at the end of the install after the logger is flushed. But because there is some overlap in time between the service starting and the installer exiting, I expect I'll lose a few lines of logging here.
Any clean suggestions would be greatly appreciated!
In the end I went with setting the permissions on the logs folder at deploy time. It's actually pretty straightforward with icacls, only a couple of lines in rake for instance, assuming you know where your logs folder is going to be:
sh %{icacls "#{logs_dir}" /grant "#{username}":(OI)(W)}
Not calling UseNLog() in the service config would also be a simple option, any install-time errors would go in the Windows event log in that case.

Not able to access network drive from VB.NET Windows Service

I have a Windows service developed in VB.NET. This Windows service picks a file every night at 8 PM from copies a file from my C:\ftpDocs to Y:\FtpDocs folder.
Y: is a mapped drive which is \\sourceServer\Output files. When I run the same code from a VB.NET Windows application instead of a Windows service it is working absolutely fine. But from the service it is throwing access denied error accessing \\sourceServer\Output.
It seems the Windows service runs from C:\windows\system32. For this reason I tried changing the current directory to C:\ftpService (This is the folder where my application is).
To access the mapped drive I provide a userid and password which is not my Windows userid and password. Do you think this is the reason why it is not able to access it from the Windows service?
If yes, how is it working from Windows application? This issue is not going away for the past one month now.
What drives are currently mapped is maintained per user -- it'd be a big no-no for me to be able to access files on a share on which you have credentials just because we're both logged on at the same time.
Your service will need to map the share itself using saved credentials of some kind (you could hard code them, if you like, though that's not terribly secure and represents a maintainability burden besides). A good example of how to do this is here -- though, I haven't used this code, I've just read the article.
Typically a Windows service runs under an id whose credentials are not authorized to access files on the network. Try running your windows service under the domain account which can access the network files. Make sure that this account has access to both the network and local folders/files that it will be reading and writing.
Also, you'll want to use the UNC path, not a mapped drive. The mapped drive won't be mounted for the service.

service doesn't behave the same as command line

I am running on a Windows Server 2003. This is my problem:
I wrote a Perl script to automate the copy of some files from my Server machine to some network drives. I am using xcopy to copy the files. My problem is the permissions.
If I run the script from the command line, it works, all the copies are successful.
If I try to run the script using a service all the copies fail. This service is a program that I wrote that takes the script and runs it. In the background all it is doing is to call the C function 'system' and it runs the same program that I can run from the command line.
I have tried many variations of this to figure out what is wrong with it but I can't see why the service would not run the same way I run it from the command line.
I set up the service to run as the same user I am using from the command line.
I also tried to map the network drives as the user that has writing permission but the result is the same. Manually the script works, from the service, it doesn't.
Any suggestion is appreciated.
Thanks
Tony
The service may be running as the system and not have access to the network drives. In the Service settings, change the service to run under your account (or an account with the relevant permissions/mappings).
When the service runs, it uses whatever credentials you specify in the Services manager of Windows. The default, LOCAL SERVICE, probably does not have permission to access the resources to be copied.
Create a new user account with the minimum set of permissions needed to perform the copy and configure your service to run under that account.
I did figure out the issue (I think), and that matches what I later found in another post:
https://serverfault.com/questions/4623/windows-can-i-map-a-network-drive-for-a-service-account
<...Persistent drive mappings are only restored during an interactive login, which the service does not use. I believe the only way to get a service to use a network drive is for that service to map the drive itself or alternatively for it to us a UNC path instead of a mapped drive.>
What I did was mapping the drive using the service and that seems to work. It turns out, if I map the drive and save credentials, then I can access later the drive without having to map it again. I don't know why this approach seems to work though.
-Thanks everybody for your help.
Tony

VB.net Windows Service with File Watcher

I have a windows service that runs at 5 PM everyday. Once it starts it will use a method to watcher that monitors a folder and if any new file is created it will upload it to Mainframes.
My questions is once the service is started for how long will the filewatcher be effective?? When will the service stop watching the folder.
What happens if someone creates a file at 7PM will it still upload to the mainframe??
Thanks
I assume by "service" you mean a Windows Service (caps) as opposed to a "service" (scheduled executable). Of course this depends on the code in the service but ostensibly as long as the service is running, the filewatcher should monitor the folder.

Resources