简体   繁体   中英

Unable to run command prompt programmatically in Windows service

I have the following code which I'm using to run an executable via the command line programmatically:

var process = new Process();
var startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.WorkingDirectory = ConfigurationManager.BitCometDirectory;
startInfo.FileName = "cmd.exe";
var command = String.Format("/c bitcomet {0} -s --tray", path);
startInfo.Arguments = command;
process.StartInfo = startInfo;
var status = process.Start();

When I test this code via a console app, it works great.

The goal, however, is to integrate this code into a Windows service. When I do this and attach the VS debugger to the running service, the service executes without issue/error. BitComet.exe as well as BitCometService.exe (not sure what that is) also appear in Task Manager. The command is to open a torrent file and, unfortunately, that is not occurring as the download never begins. FWIW, status is true .

When I originally setup the Windows service, I had to create an installer and I set the Account (the account type under which the service will run) to LocalService so I wouldn't have to authenticate the service when installing. I've also tested it with Account set to User with the same result.

Any ideas how to resolve the issue?

Thanks in advance.

UPDATE #1: I added process.StandardOutput.ReadToEnd(); to my code to log errors, but that always returns an empty string. When I check the Application logs, however, I see the following Error:

Faulting application name: UPNP.exe, version: 1.0.8.11, time stamp: 0x4a9638a4
Faulting module name: UPNP.exe, version: 1.0.8.11, time stamp: 0x4a9638a4
Exception code: 0xc0000005
Fault offset: 0x00013cbf
Faulting process id: 0xe088
Faulting application start time: 0x01d0a1ecc058cc01
Faulting application path: C:\Program Files\BitComet\tools\UPNP.exe
Faulting module path: C:\Program Files\BitComet\tools\UPNP.exe
Report Id: fe9f1270-0ddf-11e5-b482-10604b472e44

I'm not sure what UPNP.exe is, but it would appear it's called by BitComet.exe which is what my code calls. Any idea on how to resolve this? I currently have the service set to log on as my user (rather than Local System or Local Service).

UPDATE #2

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
  <Provider Name="Application Error" /> 
  <EventID Qualifiers="0">1000</EventID> 
  <Level>2</Level> 
  <Task>100</Task> 
  <Keywords>0x80000000000000</Keywords> 
  <TimeCreated SystemTime="2015-06-08T14:13:24.000000000Z" /> 
  <EventRecordID>136152</EventRecordID> 
  <Channel>Application</Channel> 
  <Computer><<computer info>></Computer> 
  <Security /> 
  </System>
- <EventData>
  <Data>UPNP.exe</Data> 
  <Data>1.0.8.11</Data> 
  <Data>4a9638a4</Data> 
  <Data>UPNP.exe</Data> 
  <Data>1.0.8.11</Data> 
  <Data>4a9638a4</Data> 
  <Data>c0000005</Data> 
  <Data>00013cbf</Data> 
  <Data>1c5c</Data> 
  <Data>01d0a1f5482780b3</Data> 
  <Data>C:\Program Files\BitComet\tools\UPNP.exe</Data> 
  <Data>C:\Program Files\BitComet\tools\UPNP.exe</Data> 
  <Data>85d9245f-0de8-11e5-bb36-10604b472e44</Data> 
  </EventData>
  </Event>

Shot in the dark, but perhaps this is the issue/fix:

var command = String.Format("/c {0}\\bitcomet  -s --tray", path);

The idea here is to put the path value into the command fired by CMD. IF that's not where 'path' points, this won't solve the issue.

Trying to resolve the UPNP.exe error has turned into a black hole. While ideally I would have all of my torrents (manually and automatically initiated) in the same app, as an alternative, I thought I'd try to wire in a different torrent utility. I found aria2 which is a command-line torrent client. Within a few minutes, I was able to download a torrent via the service.

I'm not sure what the problem is with BitComet and its UPNP.exe file, or how to resolve it, but this is an alternative I'm going to go with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM