By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

How to Debug a Windows Service

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a

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

P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
> > > 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 discussion thread is closed

Replies have been disabled for this discussion.