473,582 Members | 3,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3184
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************* *********@hotma il.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::Messag eBox::Show("Tes t");
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************* *********@hotma il.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::Messag eBox::Show("Tes t");
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************* *********@hotma il.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::Messag eBox::Show("Tes t");
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************* *********@hotma il.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 RegisterService CtrlHandlerEx. 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 SetServiceStatu s(SERVICE_START _PENDING).

The same mechanism apply to .NET, it is just wrapped by the BCL, in the
System.ServiceP rocess.ServiceB ase 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 RegisterService CtrlHandlerEx. 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 SetServiceStatu s(SERVICE_START _PENDING).

The same mechanism apply to .NET, it is just wrapped by the BCL, in the
System.ServiceP rocess.ServiceB ase 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
SetServiceStatu s(SERVICE_START _PENDING) like you say. When you are officially
started, you have to call SetServiceStatu s(SERVICE_START ED), 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************* *********@hotma il.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
1481
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 been registered in the GAC. They are also in the service's directory, along with their associated *.dbg symbols. Yet, any breakpoint in the dlls is...
5
13412
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 and then startede with the serverexpl, windows services or the NET START command. tryed also installing with installutil: it says there is no...
3
2187
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 version, both having been dotfuscated by the standard dotfuscator of visual studio 2003 ? Tia
4
10589
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 other projects) -- I've tried adding the source code project (that was used to build the DLL) into my current solution, but I still can't seem to step...
5
13325
by: TulasiKumar | last post by:
hi all, how to debug windows service in c#.net thanks in advance Regards, Tulasi
2
1966
by: Elioth | last post by:
Hi, How I Debug a Windows Services in VB 2005? Thanks for all help. Elioth
1
2223
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 except for the fact that I cannot debug web service. When my client windows application calls web service method the debugger does not step in,...
11
3135
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 mode and run it, I get descent performances. If I build it in release mode, I would expect it to at least stay as fast and maybe be faster, but I...
5
2481
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 library project (.net remoting) which again calls the actual objects running in the windows service...
0
7886
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...
0
8312
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7920
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...
0
8183
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6569
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5366
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...
0
3809
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...
1
1413
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
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...

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.