Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / April 2006

Tip: Looking for answers? Try searching our database.

How to Debug a Windows Service

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sunil Varma - 11 Apr 2006 11:17 GMT
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/default.asp?url=/library/en-us/vbcon/html/vbco
nintroductiontontserviceapplications.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
Bruno van Dooren - 11 Apr 2006 11:52 GMT
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.
[quoted text clipped - 4 lines]
> 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.

Signature

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

Sunil Varma - 11 Apr 2006 12:31 GMT
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

> Did you write a service using .NET or win32?
> I have always used win32. search www.codeproject.com for .NET examples.
[quoted text clipped - 58 lines]
>     bruno_nos_pam_van_dooren@hotmail.com
>     Remove only "_nos_pam"
Bruno van Dooren - 11 Apr 2006 13:05 GMT
> 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..
[quoted text clipped - 4 lines]
> Please give me any link that gives full picture of how to create a
> service and install the service.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwl
kwalkthroughcreatingwindowsserviceapplication.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.

Signature

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

Arnaud Debaene - 11 Apr 2006 21:57 GMT
> Thanks for the reply.
>
[quoted text clipped - 5 lines]
> 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
Bruno van Dooren - 12 Apr 2006 09:44 GMT
> > 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.

Signature

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

adebaene@club-internet.fr - 13 Apr 2006 08:55 GMT
Bruno van Dooren a écrit :

> > > in the OnStart() method.
> > The OnStart method is supposed to terminate in a short amount of time.
[quoted text clipped - 5 lines]
> 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
Bruno van Dooren - 13 Apr 2006 11:09 GMT
> > > The OnStart method is supposed to terminate in a short amount of time.
> >
[quoted text clipped - 12 lines]
> 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.

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.