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

Windows Services

Hi all,

I created a Minimal Windows Service Program in Visual Studio.NET VC# using
the Wizard...i just added the MessageBox Display in OnStart() function and my
service is installed properly...but when i started the Service it display
that "Local Service started and stopped , Serivces should have Log Support
and Alerts Isssued"..

what is this error??...am running Windows XP Home Edition,

i have given the AccountType as "LocalService",i tried with everything
excpet with User...i no need to run as User...

i would be happy if anyone could help me sort out this ...

with regards,
C.C.Chakkaradeep
Nov 16 '05 #1
12 4824
The errors is that you should not show a message box from a service.
Services are meant to run in the background without any user interaction
requirements.

Willy.

"Chakkaradeep" <Ch**********@discussions.microsoft.com> wrote in message
news:12**********************************@microsof t.com...
Hi all,

I created a Minimal Windows Service Program in Visual Studio.NET VC# using
the Wizard...i just added the MessageBox Display in OnStart() function and
my
service is installed properly...but when i started the Service it display
that "Local Service started and stopped , Serivces should have Log Support
and Alerts Isssued"..

what is this error??...am running Windows XP Home Edition,

i have given the AccountType as "LocalService",i tried with everything
excpet with User...i no need to run as User...

i would be happy if anyone could help me sort out this ...

with regards,
C.C.Chakkaradeep

Nov 16 '05 #2
Hi Willy,

so then how to check it out whether it is running or not..and is Socket
Communication possible in Services.....that is,..my service should act as a
Multithreaded Server monitoring a Port Number and if any informations come to
the port it should accept the sockets and exeute something so that the
Service has completed it's job...

am a newbie to to this Service...

with regards,
C.C.Chakkaradeep

"Willy Denoyette [MVP]" wrote:
The errors is that you should not show a message box from a service.
Services are meant to run in the background without any user interaction
requirements.

Willy.

"Chakkaradeep" <Ch**********@discussions.microsoft.com> wrote in message
news:12**********************************@microsof t.com...
Hi all,

I created a Minimal Windows Service Program in Visual Studio.NET VC# using
the Wizard...i just added the MessageBox Display in OnStart() function and
my
service is installed properly...but when i started the Service it display
that "Local Service started and stopped , Serivces should have Log Support
and Alerts Isssued"..

what is this error??...am running Windows XP Home Edition,

i have given the AccountType as "LocalService",i tried with everything
excpet with User...i no need to run as User...

i would be happy if anyone could help me sort out this ...

with regards,
C.C.Chakkaradeep


Nov 16 '05 #3
Socket communications are no issue. But you should be aware that services
are (should be) running in a restricted security context:
- They should not assume the presence of an interactive desktop nor an
interactive user (logon session).
- For security reasons, they should run with the least privileges possible
(run as NetworkService or LocalService).
Running as LocalSystem gives the service high privileges on the local system
but no access to remote system resources, it's the only account that enables
a service to interact with the desktop (to enable this - run services applet
and check/enable this property).
While it's possible to enable this option for quick and dirty debugging, I
would suggest you use one of the other facilities offered by .NET for
debugging and tracing (write to log file or eventlog, output to debugger
window etc.).

Willy.

"Chakkaradeep" <Ch**********@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.com...
Hi Willy,

so then how to check it out whether it is running or not..and is Socket
Communication possible in Services.....that is,..my service should act as
a
Multithreaded Server monitoring a Port Number and if any informations come
to
the port it should accept the sockets and exeute something so that the
Service has completed it's job...

am a newbie to to this Service...

with regards,
C.C.Chakkaradeep

"Willy Denoyette [MVP]" wrote:
The errors is that you should not show a message box from a service.
Services are meant to run in the background without any user interaction
requirements.

Willy.

"Chakkaradeep" <Ch**********@discussions.microsoft.com> wrote in message
news:12**********************************@microsof t.com...
> Hi all,
>
> I created a Minimal Windows Service Program in Visual Studio.NET VC#
> using
> the Wizard...i just added the MessageBox Display in OnStart() function
> and
> my
> service is installed properly...but when i started the Service it
> display
> that "Local Service started and stopped , Serivces should have Log
> Support
> and Alerts Isssued"..
>
> what is this error??...am running Windows XP Home Edition,
>
> i have given the AccountType as "LocalService",i tried with everything
> excpet with User...i no need to run as User...
>
> i would be happy if anyone could help me sort out this ...
>
> with regards,
> C.C.Chakkaradeep


Nov 16 '05 #4
Hi Willy,

so u mean to say that i can accept a socket and receive messages but i cant
send messages back to the client??

with regards,
C.C.Chakkaradeep

"Willy Denoyette [MVP]" wrote:
Socket communications are no issue. But you should be aware that services
are (should be) running in a restricted security context:
- They should not assume the presence of an interactive desktop nor an
interactive user (logon session).
- For security reasons, they should run with the least privileges possible
(run as NetworkService or LocalService).
Running as LocalSystem gives the service high privileges on the local system
but no access to remote system resources, it's the only account that enables
a service to interact with the desktop (to enable this - run services applet
and check/enable this property).
While it's possible to enable this option for quick and dirty debugging, I
would suggest you use one of the other facilities offered by .NET for
debugging and tracing (write to log file or eventlog, output to debugger
window etc.).

Willy.

"Chakkaradeep" <Ch**********@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.com...
Hi Willy,

so then how to check it out whether it is running or not..and is Socket
Communication possible in Services.....that is,..my service should act as
a
Multithreaded Server monitoring a Port Number and if any informations come
to
the port it should accept the sockets and exeute something so that the
Service has completed it's job...

am a newbie to to this Service...

with regards,
C.C.Chakkaradeep

"Willy Denoyette [MVP]" wrote:
The errors is that you should not show a message box from a service.
Services are meant to run in the background without any user interaction
requirements.

Willy.

"Chakkaradeep" <Ch**********@discussions.microsoft.com> wrote in message
news:12**********************************@microsof t.com...
> Hi all,
>
> I created a Minimal Windows Service Program in Visual Studio.NET VC#
> using
> the Wizard...i just added the MessageBox Display in OnStart() function
> and
> my
> service is installed properly...but when i started the Service it
> display
> that "Local Service started and stopped , Serivces should have Log
> Support
> and Alerts Isssued"..
>
> what is this error??...am running Windows XP Home Edition,
>
> i have given the AccountType as "LocalService",i tried with everything
> excpet with User...i no need to run as User...
>
> i would be happy if anyone could help me sort out this ...
>
> with regards,
> C.C.Chakkaradeep


Nov 16 '05 #5

"Chakkaradeep" <Ch**********@discussions.microsoft.com> wrote in message
news:1E**********************************@microsof t.com...
Hi Willy,

so u mean to say that i can accept a socket and receive messages but i
cant
send messages back to the client??


No, I said Sockets are no problem.
Willy.
Nov 16 '05 #6
hi,

so then Sockets are not issue...hmm...nice to hear...so i can just create
Sockets using TcpListener and accept TcpClients,rite?.....and how to check
for OnStart() function...by storing some informations in files???

with regards,
C.C.Chakkaradeep

"Willy Denoyette [MVP]" wrote:

"Chakkaradeep" <Ch**********@discussions.microsoft.com> wrote in message
news:1E**********************************@microsof t.com...
Hi Willy,

so u mean to say that i can accept a socket and receive messages but i
cant
send messages back to the client??


No, I said Sockets are no problem.
Willy.

Nov 16 '05 #7


"Chakkaradeep" <Ch**********@discussions.microsoft.com> wrote in message
news:DF**********************************@microsof t.com...
hi,

so then Sockets are not issue...hmm...nice to hear...so i can just create
Sockets using TcpListener and accept TcpClients,rite?.....and how to check
for OnStart() function...by storing some informations in files???


Just write a message to the eventlog.

Willy.
Nov 16 '05 #8
hi willy ,

here is my code for Service...

/*********Service OnStart( ) function starts here**********************/
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
System.Net.IPAddress localAdd=System.Net.IPAddress.Parse("127.0.0.1");
const int portNumber = 3000;
TcpListener tcpListener = new TcpListener(localAdd,portNumber);
tcpListener.Start();
//try
{
//Accept the pending client connection and return a TcpClient initialized
for communication.
TcpClient tcpClient = tcpListener.AcceptTcpClient();
NetworkStream networkStream =
tcpClient.GetStream();
string responseString = "You have successfully connected to me";

Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString);
networkStream.Write(sendBytes, 0, sendBytes.Length);

tcpClient.Close();
tcpListener.Stop();

}
}
/*********Service OnStart( ) function ends here**********************/

I tried with evry Account....when i start the service,it is taking much time
and then finally tells that the servie cannot be installed becoz it is out of
time.....what is my error???

The client progrm is here...

/*********Client Program Starts here**********************/
static void Main(string[] args)
{
TcpClient tcpClient = new TcpClient();
try {
tcpClient.Connect("localhost", 3000);
NetworkStream networkStream = tcpClient.GetStream();

if(networkStream.CanWrite && networkStream.CanRead){
// Does a simple write.
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there");
networkStream.Write(sendBytes, 0, sendBytes.Length);

// Reads the NetworkStream into a byte buffer.
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
networkStream.Read(bytes, 0, (int) tcpClient.ReceiveBufferSize);

// Returns the data received from the host to the console.
string returndata = Encoding.ASCII.GetString(bytes);
Console.WriteLine("This is what the host returned to you: " +
returndata);

}
else if (!networkStream.CanRead)
{
Console.WriteLine("You can not write data to this stream");
tcpClient.Close();
}
else if (!networkStream.CanWrite)
{
Console.WriteLine("You can not read data from this stream");
tcpClient.Close();
}
}
catch (Exception e )
{
Console.WriteLine(e.ToString());
}
}
/*********Client Program ends here**********************/

The Server and Client Samples were taken from MSDN only.....

what would be the problem...

with regards,
C.C.Chakkaradeep

Nov 16 '05 #9
"Chakkaradeep" <Ch**********@discussions.microsoft.com> ha scritto nel
messaggio news:F5**********************************@microsof t.com...
hi willy ,

here is my code for Service...

/*********Service OnStart( ) function starts here**********************/
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
System.Net.IPAddress localAdd=System.Net.IPAddress.Parse("127.0.0.1");
const int portNumber = 3000;
TcpListener tcpListener = new TcpListener(localAdd,portNumber);
tcpListener.Start();
//try
{
//Accept the pending client connection and return a TcpClient initialized
for communication.
TcpClient tcpClient = tcpListener.AcceptTcpClient();
NetworkStream networkStream =
tcpClient.GetStream();
string responseString = "You have successfully connected to me";

Byte[] sendBytes =
Encoding.ASCII.GetBytes(responseString);
networkStream.Write(sendBytes, 0, sendBytes.Length);

tcpClient.Close();
tcpListener.Stop();

}
}
/*********Service OnStart( ) function ends here**********************/

I tried with evry Account....when i start the service,it is taking much
time
and then finally tells that the servie cannot be installed becoz it is out
of
time.....what is my error???


This code is correct, but it can't work; the SCM calls the OnStart()
function and expects it to return, or it will report the timout you're
experiencing; so you can do only initializations there, not your main
processing (i.e. waiting for connections). You'll have to run the service on
a different thread than the one who calls OnStart. You have two choiches
here: spawn the worker thread manually and have it run the same code you
tried to run inside OnStart(), or use async sockets..

Massimo

Nov 16 '05 #10


"Chakkaradeep" <Ch**********@discussions.microsoft.com> wrote in message
news:F5**********************************@microsof t.com...
hi willy ,
The Server and Client Samples were taken from MSDN only.....

what would be the problem...

with regards,
C.C.Chakkaradeep

You should not do anything that takes longer than 30 seconds in your OnStart
method. What you should do in your OnStart is spawn a background threads and
run your code on that thread. When your thread has started you simply return
from OnStart.

Willy.
Nov 16 '05 #11
Hi,

at last i did write a Service which acts a Multithreaded Server...thanks for
ur help Willy and others.....

i did these steps...

In OnStart() spawned a thread to Start Listening over a port,when a socket
is received...accept it and spawn another thread so that i can be executed in
it's own area...thus there wil be 2 threads created in my program .....
a)One thread for Listening,done only once
b)For each client , a thread will be spawned

With regards,
C.C.Chakkaradeep.

"Willy Denoyette [MVP]" wrote:


"Chakkaradeep" <Ch**********@discussions.microsoft.com> wrote in message
news:F5**********************************@microsof t.com...
hi willy ,


The Server and Client Samples were taken from MSDN only.....

what would be the problem...

with regards,
C.C.Chakkaradeep

You should not do anything that takes longer than 30 seconds in your OnStart
method. What you should do in your OnStart is spawn a background threads and
run your code on that thread. When your thread has started you simply return
from OnStart.

Willy.

Nov 16 '05 #12
Yes, there is.

Add following class to the cs file that contains the Installer class (class
derived from System.Configuration.Install.Installer).
Change the class ServiceInstaller into ServiceInstallerEx and create an
instance of ServiceInstallerEx instead of ServiceInstaller .
Finally add - using System.Management;

class ServiceInstallerEx : System.ServiceProcess.ServiceInstaller
{
public ServiceInstallerEx() : base()
{
// Set eventhandler to call UpdateServiceConfig when install commited
base.Committed += new InstallEventHandler( this.UpdateServiceConfig );
}
// Eventhandler, called after service installer committed.
// This method uses System.Management classes to change service config.
// Note that enabling "DesktopInteract" will fail if Service account is not
LocalSystem !!
void UpdateServiceConfig(object sender, InstallEventArgs e)
{
int ret;
ManagementBaseObject inParams = null;
ManagementObject srvc = new ManagementObject("Win32_Service=" + "\"" +
this.ServiceName + "\"");
inParams = srvc.GetMethodParameters("Change");
inParams["DesktopInteract"] = true; // Enable interactive mode (Interact
With Desktop)
ManagementBaseObject outParams = srvc.InvokeMethod("Change", inParams,
null);
if((ret = Convert.ToInt32(outParams.Properties["ReturnValue"].Value)) !=
0)
Console.WriteLine("Failed to set option: {0}", ret);
else
Console.WriteLine("Option set");
}
}

Willy.
"Chakkaradeep" <Ch**********@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
Hi,

at last i did write a Service which acts a Multithreaded Server...thanks
for
ur help Willy and others.....

i did these steps...

In OnStart() spawned a thread to Start Listening over a port,when a socket
is received...accept it and spawn another thread so that i can be executed
in
it's own area...thus there wil be 2 threads created in my program .....
a)One thread for Listening,done only once
b)For each client , a thread will be spawned

With regards,
C.C.Chakkaradeep.

"Willy Denoyette [MVP]" wrote:


"Chakkaradeep" <Ch**********@discussions.microsoft.com> wrote in message
news:F5**********************************@microsof t.com...
> hi willy ,

>
> The Server and Client Samples were taken from MSDN only.....
>
> what would be the problem...
>
> with regards,
> C.C.Chakkaradeep
>

You should not do anything that takes longer than 30 seconds in your
OnStart
method. What you should do in your OnStart is spawn a background threads
and
run your code on that thread. When your thread has started you simply
return
from OnStart.

Willy.

Nov 16 '05 #13

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

Similar topics

0
by: Stefan Hinz | last post by:
Degan, jumping in to try and solve some problems that look pretty obvious to me ... > #options for default service (mysqld2) > (mysqld2) It should be , not (mysqld2).
8
by: Bill Sonia | last post by:
I've written a Windows Service to send e-mails on events like OnStart, OnStop, OnShutDown using System.Web.Mail. It works for everything but OnShutdown. My guess is that for OnShutDown, once my...
3
by: illegal.prime | last post by:
Hi all, I have a service that needs to start a regular windows application. I'm running the service as ServiceAccount.LocalSystem. But, when it starts the process (using Process.Start) the GUI...
5
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
This code works fine in Windows Application. In Windows Application, I am able to zip the image files properly and it totally contains 900MB My problem is the same code which I used in my Windows...
2
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
Hi, I have 2 applications running, one Windows application project and the other windows services project. I want to call my Windows application in my windows services. I want to run them as...
1
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
Hi, I have 2 applications running, one Windows application project and the other windows services project. I want to call my Windows application in my windows services. I want to run them as...
5
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name?...
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: Overview NOTE- This complete article on "Windows Autorun FAQs" applies theoretically to all Windows NT-based OSes till Windows Vista (and probably Vista's successors too)....
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: List of autostart locations Linked from the Original article- "Windows Autorun FAQs: Description". Que: Can you list all the autostart locations for windows? Ans: Here is...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.