473,692 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Cha nged(object sender,
System.IO.FileS ystemEventArgs e)
{
string ChangeType = e.ChangeType.To String();
string renamed = e.FullPath;
System.Diagnost ics.Process process1;
process1= new System.Diagnost ics.Process();
process1.Enable RaisingEvents = false;
string strCmdLine;
strCmdLine = "/K Dumper.exe \"" + renamed + "\"";
Thread.Sleep(40 00);
IDictionary environmentVari ables = Environment.Get EnvironmentVari ables();
process1.StartI nfo.Environment Variables.Clear ();
foreach (DictionaryEntr y de in environmentVari ables)
{
// EventLog.WriteE ntry("Directory Monitor", " KeyValue: " + de.Key +
de.Value);
process1.StartI nfo.Environment Variables.Add(( string)de.Key,
(string)de.Valu e);
}
process1.StartI nfo.WorkingDire ctory = "C:\\Temp";
process1.StartI nfo.FileName = "CMD.exe";
process1.StartI nfo.Arguments = strCmdLine;
process1.StartI nfo.UseShellExe cute = false;
process1.StartI nfo.RedirectSta ndardOutput = true;
process1.StartI nfo.RedirectSta ndardError = true;
process1.StartI nfo.WindowStyle = ProcessWindowSt yle.Hidden;
process1.StartI nfo.CreateNoWin dow = true;
process1.Start( );
StreamReader myStreamReader = process1.Standa rdOutput;
StreamReader myStreamError = process1.Standa rdError;
string myID = myStreamReader. ReadLine();
string stdError = myStreamError.R eadLine();
process1.Close( );
EventLog.WriteE ntry("Directory Monitor", "myID: " + myID);
EventLog.WriteE ntry("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 15535
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_Cha nged(object sender,
System.IO.FileS ystemEventArgs e)
{
string ChangeType = e.ChangeType.To String();
string renamed = e.FullPath;
System.Diagnost ics.Process process1;
process1= new System.Diagnost ics.Process();
process1.Enable RaisingEvents = false;
string strCmdLine;
strCmdLine = "/K Dumper.exe \"" + renamed + "\"";
Thread.Sleep(40 00);
IDictionary environmentVari ables = Environment.Get EnvironmentVari ables();
process1.StartI nfo.Environment Variables.Clear ();
foreach (DictionaryEntr y de in environmentVari ables)
{
// EventLog.WriteE ntry("Directory Monitor", " KeyValue: " + de.Key +
de.Value);
process1.StartI nfo.Environment Variables.Add(( string)de.Key,
(string)de.Valu e);
}
process1.StartI nfo.WorkingDire ctory = "C:\\Temp";
process1.StartI nfo.FileName = "CMD.exe";
process1.StartI nfo.Arguments = strCmdLine;
process1.StartI nfo.UseShellExe cute = false;
process1.StartI nfo.RedirectSta ndardOutput = true;
process1.StartI nfo.RedirectSta ndardError = true;
process1.StartI nfo.WindowStyle = ProcessWindowSt yle.Hidden;
process1.StartI nfo.CreateNoWin dow = true;
process1.Start( );
StreamReader myStreamReader = process1.Standa rdOutput;
StreamReader myStreamError = process1.Standa rdError;
string myID = myStreamReader. ReadLine();
string stdError = myStreamError.R eadLine();
process1.Close( );
EventLog.WriteE ntry("Directory Monitor", "myID: " + myID);
EventLog.WriteE ntry("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***@discussi ons.microsoft.c om> wrote in message
news:54******** *************** ***********@mic rosoft.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_Cha nged(object sender,
System.IO.FileS ystemEventArgs e)
{
string ChangeType = e.ChangeType.To String();
string renamed = e.FullPath;
System.Diagnost ics.Process process1;
process1= new System.Diagnost ics.Process();
process1.Enable RaisingEvents = false;
string strCmdLine;
strCmdLine = "/K Dumper.exe \"" + renamed + "\"";
Thread.Sleep(40 00);
IDictionary environmentVari ables =
Environment.Get EnvironmentVari ables();
process1.StartI nfo.Environment Variables.Clear ();
foreach (DictionaryEntr y de in environmentVari ables)
{
// EventLog.WriteE ntry("Directory Monitor", " KeyValue: " + de.Key +
de.Value);
process1.StartI nfo.Environment Variables.Add(( string)de.Key,
(string)de.Valu e);
}
process1.StartI nfo.WorkingDire ctory = "C:\\Temp";
process1.StartI nfo.FileName = "CMD.exe";
process1.StartI nfo.Arguments = strCmdLine;
process1.StartI nfo.UseShellExe cute = false;
process1.StartI nfo.RedirectSta ndardOutput = true;
process1.StartI nfo.RedirectSta ndardError = true;
process1.StartI nfo.WindowStyle = ProcessWindowSt yle.Hidden;
process1.StartI nfo.CreateNoWin dow = true;
process1.Start( );
StreamReader myStreamReader = process1.Standa rdOutput;
StreamReader myStreamError = process1.Standa rdError;
string myID = myStreamReader. ReadLine();
string stdError = myStreamError.R eadLine();
process1.Close( );
EventLog.WriteE ntry("Directory Monitor", "myID: " + myID);
EventLog.WriteE ntry("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 "LoadUserProfil e"
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 "CreateEnvironm entBlock" API. Note that you need to
create a logon session (LogonUser) for the user and load the user's profile
(LoadUserProfil e)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 "CreateProcessA s" 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_Cha nged(object sender,
System.IO.FileS ystemEventArgs e)
{
string ChangeType = e.ChangeType.To String();
string renamed = e.FullPath;
System.Diagnost ics.Process process1;
process1= new System.Diagnost ics.Process();
process1.Enable RaisingEvents = false;
string strCmdLine;
strCmdLine = "/K Dumper.exe \"" + renamed + "\"";
Thread.Sleep(40 00);
IDictionary environmentVari ables = Environment.Get EnvironmentVari ables();
process1.StartI nfo.Environment Variables.Clear ();
foreach (DictionaryEntr y de in environmentVari ables)
{
// EventLog.WriteE ntry("Directory Monitor", " KeyValue: " + de.Key +
de.Value);
process1.StartI nfo.Environment Variables.Add(( string)de.Key,
(string)de.Valu e);
}
process1.StartI nfo.WorkingDire ctory = "C:\\Temp";
process1.StartI nfo.FileName = "CMD.exe";
process1.StartI nfo.Arguments = strCmdLine;
process1.StartI nfo.UseShellExe cute = false;
process1.StartI nfo.RedirectSta ndardOutput = true;
process1.StartI nfo.RedirectSta ndardError = true;
process1.StartI nfo.WindowStyle = ProcessWindowSt yle.Hidden;
process1.StartI nfo.CreateNoWin dow = true;
process1.Start( );
StreamReader myStreamReader = process1.Standa rdOutput;
StreamReader myStreamError = process1.Standa rdError;
string myID = myStreamReader. ReadLine();
string stdError = myStreamError.R eadLine();
process1.Close( );
EventLog.WriteE ntry("Directory Monitor", "myID: " + myID);
EventLog.WriteE ntry("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***@discussi ons.microsoft.c om> wrote in message
news:54******** *************** ***********@mic rosoft.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_Cha nged(object sender,
System.IO.FileS ystemEventArgs e)
{
string ChangeType = e.ChangeType.To String();
string renamed = e.FullPath;
System.Diagnost ics.Process process1;
process1= new System.Diagnost ics.Process();
process1.Enable RaisingEvents = false;
string strCmdLine;
strCmdLine = "/K Dumper.exe \"" + renamed + "\"";
Thread.Sleep(40 00);
IDictionary environmentVari ables =
Environment.Get EnvironmentVari ables();
process1.StartI nfo.Environment Variables.Clear ();
foreach (DictionaryEntr y de in environmentVari ables)
{
// EventLog.WriteE ntry("Directory Monitor", " KeyValue: " + de.Key +
de.Value);
process1.StartI nfo.Environment Variables.Add(( string)de.Key,
(string)de.Valu e);
}
process1.StartI nfo.WorkingDire ctory = "C:\\Temp";
process1.StartI nfo.FileName = "CMD.exe";
process1.StartI nfo.Arguments = strCmdLine;
process1.StartI nfo.UseShellExe cute = false;
process1.StartI nfo.RedirectSta ndardOutput = true;
process1.StartI nfo.RedirectSta ndardError = true;
process1.StartI nfo.WindowStyle = ProcessWindowSt yle.Hidden;
process1.StartI nfo.CreateNoWin dow = true;
process1.Start( );
StreamReader myStreamReader = process1.Standa rdOutput;
StreamReader myStreamError = process1.Standa rdError;
string myID = myStreamReader. ReadLine();
string stdError = myStreamError.R eadLine();
process1.Close( );
EventLog.WriteE ntry("Directory Monitor", "myID: " + myID);
EventLog.WriteE ntry("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 "LoadUserProfil e"
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 "CreateEnvironm entBlock" API. Note that you need to
create a logon session (LogonUser) for the user and load the user's profile
(LoadUserProfil e)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 "CreateProcessA s" 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
5473
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 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
3
37978
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 knows please let me know. Thanks, Joe
4
7871
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 applications in a training class last week. The applications worked in the class, but when I tried to run them again over the weekend, they both bombed. Instead of getting my Web page, or the Web Service page, I get a page full of error text...
3
14924
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 swLog As StreamWriter = File.CreateText("C:\myLog.txt") swLog.WriteLine("My Windows Service has just started.") swLog.Close() : swLog.Flush() End Sub
5
3405
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 fly customized to a specific client. Is it possible to run the VS command line tool through another application (VB.NET)? I'm thinking I can run the tools as a process and pass the necessary parameters to the process, but I'm not sure how to go...
4
3177
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
8636
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}"); else
40
29334
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 detect when a specific file has been created or changed. That works fine to a point. The event does fire when the file is being created or changed but I then blow up when I attempt to open the file because the creation or changing has not...
0
3962
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 is the service trying to restart but failing). The process that remains I cannot kill however i try, I get a message 'The operation could not be completed. Access is denied.' from taskmanager even though I'm in administrator mode. I've tried to...
0
8604
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9083
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8800
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6459
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5819
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4324
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1957
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.