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

How to Debug a Windows Service

Hello all,

I wrote a Windows Service in VC.NET 2005
I want to debug the solution.
I tried as mentioned in the following link.

http://msdn.microsoft.com/library/de...plications.asp

But, when I started the service I got a message box saying that the
service is being stopped.

And also I would like to know whether the application code is to be
written in the OnStart() method or not.

One more problem that i faced while executing the Windows Service is,
if i try to pop-up a message box, i get "Stopping the Service" message
when i start the Service.

Thanks in advance.

Regards
Sunil Varma

Apr 11 '06 #1
7 3155
Did you write a service using .NET or win32?
I have always used win32. search www.codeproject.com for .NET examples.
I wrote a Windows Service in VC.NET 2005
I want to debug the solution.
I tried as mentioned in the following link.

http://msdn.microsoft.com/library/de...plications.asp

But, when I started the service I got a message box saying that the
service is being stopped.
The page you mention contains a link to 'debugging windows services' which
contains this explanation:

<snip>
Attaching to the service's process allows you to debug most but not all of
the service's code; for example, because the service has already been
started, you cannot debug the code in the service's OnStart method this way,
or the code in the Main method that is used to load the service.
</snip>

If all your code is in OnStart, you cannot debug it according to this.

What you could do is to is to print logging information to a text file
though that should at least give you some idea of what is happening inside
OnStart
And also I would like to know whether the application code is to be
written in the OnStart() method or not.
You can do it, but it is not mandatory. I have done it like that a couple of
times, but you don't have to. at least in win32 services.
One more problem that i faced while executing the Windows Service is,
if i try to pop-up a message box, i get "Stopping the Service" message
when i start the Service.


Is your service allowed to interact with the user interface? Otherwise you
will never see the dialog box. You should never put UI features in a service
anyway because it can easily lead to security problems. And even if you
provide UI features, they will be invisible if noone is logged in.

What I do when i need to debug service functionality is that I launch it in
the IDE, but with an argument that specifies it is running as a console
application instead of a service. that will instruct the application to
directly go to the function where the functionality of the program starts so
that the service stuff is skipped.

That ways you can debug all your code in the VS IDE. then , when you know it
works, you can start is as a service.

The platform SDK contains an example that does that exact same thing, but i
don't know if it is easy to do the same if you are using .NET to program a
service.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Apr 11 '06 #2

Thanks for the reply.

I created the service using VC.NET 2005.
If it is not mandatory to write the code of the application in
OnStart() method, then can you give me an idea of how to do it?

My service is not interacting with any user interface.
I just wrote
System::Windows::Forms::MessageBox::Show("Test");
in the OnStart() method.

I found a project in the CodeProject to create a Windows Service by
Anish C.V..
But i'm finding it difficult to create a service from that link.
Once i create the project i'm not able to install it as a service using
the "sc" command.

Please give me any link that gives full picture of how to create a
service and install the service.

Thanks & Regards
Sunil Varma

Bruno van Dooren wrote:
Did you write a service using .NET or win32?
I have always used win32. search www.codeproject.com for .NET examples.
I wrote a Windows Service in VC.NET 2005
I want to debug the solution.
I tried as mentioned in the following link.

http://msdn.microsoft.com/library/de...plications.asp

But, when I started the service I got a message box saying that the
service is being stopped.


The page you mention contains a link to 'debugging windows services' which
contains this explanation:

<snip>
Attaching to the service's process allows you to debug most but not all of
the service's code; for example, because the service has already been
started, you cannot debug the code in the service's OnStart method this way,
or the code in the Main method that is used to load the service.
</snip>

If all your code is in OnStart, you cannot debug it according to this.

What you could do is to is to print logging information to a text file
though that should at least give you some idea of what is happening inside
OnStart
And also I would like to know whether the application code is to be
written in the OnStart() method or not.


You can do it, but it is not mandatory. I have done it like that a couple of
times, but you don't have to. at least in win32 services.
One more problem that i faced while executing the Windows Service is,
if i try to pop-up a message box, i get "Stopping the Service" message
when i start the Service.


Is your service allowed to interact with the user interface? Otherwise you
will never see the dialog box. You should never put UI features in a service
anyway because it can easily lead to security problems. And even if you
provide UI features, they will be invisible if noone is logged in.

What I do when i need to debug service functionality is that I launch it in
the IDE, but with an argument that specifies it is running as a console
application instead of a service. that will instruct the application to
directly go to the function where the functionality of the program starts so
that the service stuff is skipped.

That ways you can debug all your code in the VS IDE. then , when you know it
works, you can start is as a service.

The platform SDK contains an example that does that exact same thing, but i
don't know if it is easy to do the same if you are using .NET to program a
service.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"


Apr 11 '06 #3
> I created the service using VC.NET 2005.
If it is not mandatory to write the code of the application in
OnStart() method, then can you give me an idea of how to do it?
The simplest way is to make a command line program that does what you want
to do in the service.
When you know that the command line app works (you can easily debug it), you
can simply launch it in the OnStart method of your service, using the Process
class.
My service is not interacting with any user interface.
I just wrote
System::Windows::Forms::MessageBox::Show("Test");
in the OnStart() method.
A message box is a user interface window.
I found a project in the CodeProject to create a Windows Service by
Anish C.V..
But i'm finding it difficult to create a service from that link.
Once i create the project i'm not able to install it as a service using
the "sc" command.

Please give me any link that gives full picture of how to create a
service and install the service.


http://msdn.microsoft.com/library/de...pplication.asp

The trick to finding examples is to include C# in your search There are much
more C# and VB.NET articles than C++/CLI articles.
Is is often easier to find an article on something in C#. Then you only have
to translate the code to C++/CLI which is relatively easy.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"

Apr 11 '06 #4
Sunil Varma wrote:
Thanks for the reply.

I created the service using VC.NET 2005.
If it is not mandatory to write the code of the application in
OnStart() method, then can you give me an idea of how to do it?

My service is not interacting with any user interface.
I just wrote
System::Windows::Forms::MessageBox::Show("Test");
A MessageBox *IS* a user interface! If you want to see this box, allow the
service to interact with desktop (ONLY for debugging purposes! Never use
this feature on a production service!)
in the OnStart() method.

The OnStart method is supposed to terminate in a short amount of time. If
you do a blocking call such as MessageBox within it, you're on the wrong
path. You'd better use a log file to trace the execution of your service.

An alternative if you really need to debug the starting of the service is to
use the "Image File execution option / debugger" feature of Windows to have
the debugger attached as soon as the process start. See
http://msdn2.microsoft.com/en-us/library/a329t4ed.aspx

Arnaud
MVP - VC
Apr 11 '06 #5
in the OnStart() method.

The OnStart method is supposed to terminate in a short amount of time.


Good to know.

That is a major difference with win32 service programs, because it is
harmless to use the OnStart function as your service body that runs until the
service is stopped.
In fact the service example in the platform SDK works like this.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"

Apr 12 '06 #6

Bruno van Dooren a écrit :
in the OnStart() method.

The OnStart method is supposed to terminate in a short amount of time.


Good to know.

That is a major difference with win32 service programs, because it is
harmless to use the OnStart function as your service body that runs untilthe
service is stopped.
In fact the service example in the platform SDK works like this.


No, it the same thing. To be more precise, when the SCM calls the
ServiceMain function, the service must register it's handler immediatly
with RegisterServiceCtrlHandlerEx. From this point on, the service has
1 s to initialize itself. If it needs a longer time, it needs to notify
the SCM with SetServiceStatus(SERVICE_START_PENDING).

The same mechanism apply to .NET, it is just wrapped by the BCL, in the
System.ServiceProcess.ServiceBase class. There is a good example in the
documentation of this class.

Arnaud
MVP - VC

Apr 13 '06 #7
> > > The OnStart method is supposed to terminate in a short amount of time.

That is a major difference with win32 service programs, because it is
harmless to use the OnStart function as your service body that runs until the
service is stopped.
In fact the service example in the platform SDK works like this.


No, it the same thing. To be more precise, when the SCM calls the
ServiceMain function, the service must register it's handler immediatly
with RegisterServiceCtrlHandlerEx. From this point on, the service has
1 s to initialize itself. If it needs a longer time, it needs to notify
the SCM with SetServiceStatus(SERVICE_START_PENDING).

The same mechanism apply to .NET, it is just wrapped by the BCL, in the
System.ServiceProcess.ServiceBase class. There is a good example in the
documentation of this class.


if a process is running in its own process, the process will be started when
the service has to start, which is why you can use service_main to run your
main service code.

If the startup takes a long time to complete, you need to call
SetServiceStatus(SERVICE_START_PENDING) like you say. When you are officially
started, you have to call SetServiceStatus(SERVICE_STARTED), but there is no
requirement for returning from the service_main.

The example in the platform SDK does exactly the same.

Note that my services were all running in their own process.
If services share a process the situation is may be different, but
Otherwise you can put your body in the service_main function.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Apr 13 '06 #8

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

Similar topics

0
by: INGSOC | last post by:
Using remote debugging, I can attach to a windows service and run it in debug mode in VS.Net 2003. The problem is this service uses two supporting dlls. On the remote service, the dlls have...
5
by: SiD` | last post by:
when starting a windows service writte in vb.net, a messagebox appears: cannot start service from the command line or a debugger. A windows service must first be installed using installutil.exe ...
3
by: | last post by:
Since I need to dotfuscate my exe file anyway, does it make any difference if I use Debug or Release versions. Would a Debug version be easier to decompile/study/reverse engineer than a Release...
4
by: Rob R. Ainscough | last post by:
I've created several DLL's that I use in other projects. I "Add Reference" to these other projects pointing to my DLL. Probelm is, I can't seem to be able to step into my DLL code (from these...
5
by: TulasiKumar | last post by:
hi all, how to debug windows service in c#.net thanks in advance Regards, Tulasi
2
by: Elioth | last post by:
Hi, How I Debug a Windows Services in VB 2005? Thanks for all help. Elioth
1
by: thomas | last post by:
Hello all, I have a C# windows application project and a web service project, all part of the same solution. My windows application references and uses web service. Everything works as expected...
11
by: ThunderMusic | last post by:
Hi, I have a windows service that only loads a CSV file and import it's data using SqlBulkCopy in a newly created Sql Server 2005 table using 25000 rows batches. If I build the service in debug...
5
by: Jeff | last post by:
..NET 2.0 I'm working on 3 projects in VS2005: - a windows application project - dll library project - windows service This works like this: The window form call some methods in the dll...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.