473,322 Members | 1,703 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,322 software developers and data experts.

Cannot run a command process from a Windows Service

Hi,

This problem has been frustrating me for days and I hope you experts can
help me out. I am trying to run a command, which I would normally run from
the command line, from within my C# application. The command runs
successfully from a Windows Form but fails if the application is recasted as
a Windows service. The application is essentially a File Watcher that should
run the process when a file is dropped into a specific directory.

Here is the code:

private void FileMonitor_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
string ChangeType = e.ChangeType.ToString();
string renamed = e.FullPath;
System.Diagnostics.Process process1;
process1= new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;
string strCmdLine;
strCmdLine = "/K Dumper.exe \"" + renamed + "\"";
Thread.Sleep(4000);
IDictionary environmentVariables = Environment.GetEnvironmentVariables();
process1.StartInfo.EnvironmentVariables.Clear();
foreach (DictionaryEntry de in environmentVariables)
{
// EventLog.WriteEntry("Directory Monitor", " KeyValue: " + de.Key +
de.Value);
process1.StartInfo.EnvironmentVariables.Add((strin g)de.Key,
(string)de.Value);
}
process1.StartInfo.WorkingDirectory = "C:\\Temp";
process1.StartInfo.FileName = "CMD.exe";
process1.StartInfo.Arguments = strCmdLine;
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.RedirectStandardError = true;
process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process1.StartInfo.CreateNoWindow = true;
process1.Start();
StreamReader myStreamReader = process1.StandardOutput;
StreamReader myStreamError = process1.StandardError;
string myID = myStreamReader.ReadLine();
string stdError = myStreamError.ReadLine();
process1.Close();
EventLog.WriteEntry("Directory Monitor", "myID: " + myID);
EventLog.WriteEntry("Directory Monitor", "stdError: " + stdError);
}

The event log reports the following error:

stdError: 'Dumper.exe' is not recognized as an internal or external command

Configuring the service so that it can interact with the desktop doesn't
help.

Any ideas why my application can't find the command? Perhaps I have not set
up the environment variables correctly?

Many thanks in advance and happy holidays!
Nov 16 '05 #1
4 15461
Hi,

have you tried to call the dumper.exe with the full path? I believe
there are no ENV-Vars set for services... This might solve your problem.

Martin
Primo wrote:
Hi,

This problem has been frustrating me for days and I hope you experts can
help me out. I am trying to run a command, which I would normally run from
the command line, from within my C# application. The command runs
successfully from a Windows Form but fails if the application is recasted as
a Windows service. The application is essentially a File Watcher that should
run the process when a file is dropped into a specific directory.

Here is the code:

private void FileMonitor_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
string ChangeType = e.ChangeType.ToString();
string renamed = e.FullPath;
System.Diagnostics.Process process1;
process1= new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;
string strCmdLine;
strCmdLine = "/K Dumper.exe \"" + renamed + "\"";
Thread.Sleep(4000);
IDictionary environmentVariables = Environment.GetEnvironmentVariables();
process1.StartInfo.EnvironmentVariables.Clear();
foreach (DictionaryEntry de in environmentVariables)
{
// EventLog.WriteEntry("Directory Monitor", " KeyValue: " + de.Key +
de.Value);
process1.StartInfo.EnvironmentVariables.Add((strin g)de.Key,
(string)de.Value);
}
process1.StartInfo.WorkingDirectory = "C:\\Temp";
process1.StartInfo.FileName = "CMD.exe";
process1.StartInfo.Arguments = strCmdLine;
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.RedirectStandardError = true;
process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process1.StartInfo.CreateNoWindow = true;
process1.Start();
StreamReader myStreamReader = process1.StandardOutput;
StreamReader myStreamError = process1.StandardError;
string myID = myStreamReader.ReadLine();
string stdError = myStreamError.ReadLine();
process1.Close();
EventLog.WriteEntry("Directory Monitor", "myID: " + myID);
EventLog.WriteEntry("Directory Monitor", "stdError: " + stdError);
}

The event log reports the following error:

stdError: 'Dumper.exe' is not recognized as an internal or external command

Configuring the service so that it can interact with the desktop doesn't
help.

Any ideas why my application can't find the command? Perhaps I have not set
up the environment variables correctly?

Many thanks in advance and happy holidays!

Nov 16 '05 #2

"Primo" <Pr***@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com...
Hi,

This problem has been frustrating me for days and I hope you experts can
help me out. I am trying to run a command, which I would normally run from
the command line, from within my C# application. The command runs
successfully from a Windows Form but fails if the application is recasted
as
a Windows service. The application is essentially a File Watcher that
should
run the process when a file is dropped into a specific directory.

Here is the code:

private void FileMonitor_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
string ChangeType = e.ChangeType.ToString();
string renamed = e.FullPath;
System.Diagnostics.Process process1;
process1= new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;
string strCmdLine;
strCmdLine = "/K Dumper.exe \"" + renamed + "\"";
Thread.Sleep(4000);
IDictionary environmentVariables =
Environment.GetEnvironmentVariables();
process1.StartInfo.EnvironmentVariables.Clear();
foreach (DictionaryEntry de in environmentVariables)
{
// EventLog.WriteEntry("Directory Monitor", " KeyValue: " + de.Key +
de.Value);
process1.StartInfo.EnvironmentVariables.Add((strin g)de.Key,
(string)de.Value);
}
process1.StartInfo.WorkingDirectory = "C:\\Temp";
process1.StartInfo.FileName = "CMD.exe";
process1.StartInfo.Arguments = strCmdLine;
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.RedirectStandardError = true;
process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process1.StartInfo.CreateNoWindow = true;
process1.Start();
StreamReader myStreamReader = process1.StandardOutput;
StreamReader myStreamError = process1.StandardError;
string myID = myStreamReader.ReadLine();
string stdError = myStreamError.ReadLine();
process1.Close();
EventLog.WriteEntry("Directory Monitor", "myID: " + myID);
EventLog.WriteEntry("Directory Monitor", "stdError: " + stdError);
}

The event log reports the following error:

stdError: 'Dumper.exe' is not recognized as an internal or external
command

Configuring the service so that it can interact with the desktop doesn't
help.

Any ideas why my application can't find the command? Perhaps I have not
set
up the environment variables correctly?

Many thanks in advance and happy holidays!


Windows services are designed to run as self contained background processes,
that is:
- they preferably run in a security context of a restricted service account
("localservice", "network service" or "local system")
Solution: run as a non service account.
- they are meant to run in an unattended non-interactive mode (don't assume
the presence of an interactive logon session),That means that certain
windows resources such as OLE embedded objects, and the clipboard are
unavailable.
Solution: Run with "Interact with desktop" privilege enabled.
- they have no access to the logon user profile nor do they have access to
HKCU (if any), That means that printers, mapped drives, or any other
networked resources may not be available.
Solution: explicitly load the user profile using the Win32 "LoadUserProfile"
API. Note that you need to create a logon session (call LogonUser) for the
user before you can load the user's profile.
- they have their own environment block associated with the process.
[This is exactly your problem; dumper.exe is not in the "path" environment.
So maybe you can move forward by specifying the full path.]
Solution: call Win32 "CreateEnvironmentBlock" API. Note that you need to
create a logon session (LogonUser) for the user and load the user's profile
(LoadUserProfile)before you can retrieve the user's environment.
- they are not meant to execute external processes that need to break-out of
this secured context.
Solution: Run in an interactive desktop/winstation, create a new logon
session, load user profile and environment before creating the child process
using Win32 "CreateProcessAs" API . (But ask yourself why you run this as a
service!).

Above is a non-restricted list of possible issues and "solutions", however,
I would not recommended these as they all result in a security breach,
therefore I suggest you to find an alternative for a windows service.

Willy.

Nov 16 '05 #3
Hi Martin,

I've decided to use the full path and it solves my problem.

Thanks,

Omer

"mphanke" wrote:
Hi,

have you tried to call the dumper.exe with the full path? I believe
there are no ENV-Vars set for services... This might solve your problem.

Martin
Primo wrote:
Hi,

This problem has been frustrating me for days and I hope you experts can
help me out. I am trying to run a command, which I would normally run from
the command line, from within my C# application. The command runs
successfully from a Windows Form but fails if the application is recasted as
a Windows service. The application is essentially a File Watcher that should
run the process when a file is dropped into a specific directory.

Here is the code:

private void FileMonitor_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
string ChangeType = e.ChangeType.ToString();
string renamed = e.FullPath;
System.Diagnostics.Process process1;
process1= new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;
string strCmdLine;
strCmdLine = "/K Dumper.exe \"" + renamed + "\"";
Thread.Sleep(4000);
IDictionary environmentVariables = Environment.GetEnvironmentVariables();
process1.StartInfo.EnvironmentVariables.Clear();
foreach (DictionaryEntry de in environmentVariables)
{
// EventLog.WriteEntry("Directory Monitor", " KeyValue: " + de.Key +
de.Value);
process1.StartInfo.EnvironmentVariables.Add((strin g)de.Key,
(string)de.Value);
}
process1.StartInfo.WorkingDirectory = "C:\\Temp";
process1.StartInfo.FileName = "CMD.exe";
process1.StartInfo.Arguments = strCmdLine;
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.RedirectStandardError = true;
process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process1.StartInfo.CreateNoWindow = true;
process1.Start();
StreamReader myStreamReader = process1.StandardOutput;
StreamReader myStreamError = process1.StandardError;
string myID = myStreamReader.ReadLine();
string stdError = myStreamError.ReadLine();
process1.Close();
EventLog.WriteEntry("Directory Monitor", "myID: " + myID);
EventLog.WriteEntry("Directory Monitor", "stdError: " + stdError);
}

The event log reports the following error:

stdError: 'Dumper.exe' is not recognized as an internal or external command

Configuring the service so that it can interact with the desktop doesn't
help.

Any ideas why my application can't find the command? Perhaps I have not set
up the environment variables correctly?

Many thanks in advance and happy holidays!

Nov 16 '05 #4
Hi Willy,

Following on from your reply and recommendation I've provided the full path
to the process to avoid the logon session. My application now works but I
will consider an alternative to the Windows service.

Thanks for you first class reply. It was extremely helpful.

Regards

"Willy Denoyette [MVP]" wrote:

"Primo" <Pr***@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com...
Hi,

This problem has been frustrating me for days and I hope you experts can
help me out. I am trying to run a command, which I would normally run from
the command line, from within my C# application. The command runs
successfully from a Windows Form but fails if the application is recasted
as
a Windows service. The application is essentially a File Watcher that
should
run the process when a file is dropped into a specific directory.

Here is the code:

private void FileMonitor_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
string ChangeType = e.ChangeType.ToString();
string renamed = e.FullPath;
System.Diagnostics.Process process1;
process1= new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;
string strCmdLine;
strCmdLine = "/K Dumper.exe \"" + renamed + "\"";
Thread.Sleep(4000);
IDictionary environmentVariables =
Environment.GetEnvironmentVariables();
process1.StartInfo.EnvironmentVariables.Clear();
foreach (DictionaryEntry de in environmentVariables)
{
// EventLog.WriteEntry("Directory Monitor", " KeyValue: " + de.Key +
de.Value);
process1.StartInfo.EnvironmentVariables.Add((strin g)de.Key,
(string)de.Value);
}
process1.StartInfo.WorkingDirectory = "C:\\Temp";
process1.StartInfo.FileName = "CMD.exe";
process1.StartInfo.Arguments = strCmdLine;
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.RedirectStandardError = true;
process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process1.StartInfo.CreateNoWindow = true;
process1.Start();
StreamReader myStreamReader = process1.StandardOutput;
StreamReader myStreamError = process1.StandardError;
string myID = myStreamReader.ReadLine();
string stdError = myStreamError.ReadLine();
process1.Close();
EventLog.WriteEntry("Directory Monitor", "myID: " + myID);
EventLog.WriteEntry("Directory Monitor", "stdError: " + stdError);
}

The event log reports the following error:

stdError: 'Dumper.exe' is not recognized as an internal or external
command

Configuring the service so that it can interact with the desktop doesn't
help.

Any ideas why my application can't find the command? Perhaps I have not
set
up the environment variables correctly?

Many thanks in advance and happy holidays!


Windows services are designed to run as self contained background processes,
that is:
- they preferably run in a security context of a restricted service account
("localservice", "network service" or "local system")
Solution: run as a non service account.
- they are meant to run in an unattended non-interactive mode (don't assume
the presence of an interactive logon session),That means that certain
windows resources such as OLE embedded objects, and the clipboard are
unavailable.
Solution: Run with "Interact with desktop" privilege enabled.
- they have no access to the logon user profile nor do they have access to
HKCU (if any), That means that printers, mapped drives, or any other
networked resources may not be available.
Solution: explicitly load the user profile using the Win32 "LoadUserProfile"
API. Note that you need to create a logon session (call LogonUser) for the
user before you can load the user's profile.
- they have their own environment block associated with the process.
[This is exactly your problem; dumper.exe is not in the "path" environment.
So maybe you can move forward by specifying the full path.]
Solution: call Win32 "CreateEnvironmentBlock" API. Note that you need to
create a logon session (LogonUser) for the user and load the user's profile
(LoadUserProfile)before you can retrieve the user's environment.
- they are not meant to execute external processes that need to break-out of
this secured context.
Solution: Run in an interactive desktop/winstation, create a new logon
session, load user profile and environment before creating the child process
using Win32 "CreateProcessAs" API . (But ask yourself why you run this as a
service!).

Above is a non-restricted list of possible issues and "solutions", however,
I would not recommended these as they all result in a security breach,
therefore I suggest you to find an alternative for a windows service.

Willy.

Nov 16 '05 #5

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

Similar topics

8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
3
by: Joe | last post by:
Dear Friends, How to run the C# Console application as services? I have a console application.I want run this application as background services.User don't want see the command propmt. If anyone...
4
by: Chris | last post by:
I posted this in the C# language group, then thought it might be more appropriate in this group. I would not cross-post except I want the answer so badly. I built small C# Web and Web Service...
3
by: Amjad | last post by:
Hi, I just wrote a test Windows Service that creates a text file on startup (please see my code below). The file is never created. Protected Overrides Sub OnStart(ByVal args() As String) Dim...
5
by: clsmith66 | last post by:
I've been asked to find out if a project is possible, but I'm not having much luck finding the information I need, I hope some one can help. I need to see if I can build a windows service on the...
4
by: kkt49 | last post by:
# vim: et sw=4 ts=8 sts from wxPython.wx import * import sys, os, time import pywintypes import win32serviceutil import win32service import win32event import win32process
7
by: =?Utf-8?B?Vmlua2k=?= | last post by:
public void sendKeysTest() { Process myProcess = Process.Start(@"C:\winnt\system32\cmd.exe"); SetForegroundWindow(myProcess.Handle); if (myProcess.Responding) SendKeys.SendWait("{ENTER}");...
40
by: =?Utf-8?B?Um9iZXJ0IEUuIEZsYWhlcnR5?= | last post by:
What is the C# command to wait for a specified period of time? I am writing a windows service that will process a file once it has beed created or changed. I'm using the fileSystemWatcher to...
0
by: Almund | last post by:
Hi, Since a while back PosgreSQL have started to hang making interaction with it impossible. The process still remains in the taskmanager aswell as another process with the same name (which i guess...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.