Solved

Debugging a Service in D5

Posted on 2000-02-24
11
267 Views
Last Modified: 2010-04-04
Hi everyone...

I want to attach my programm to the running service process, but despite following the instructions in the D5 help, I still get an Access Denied. I can't use the "Start Program first and then the Service" method, since the error occurs, when the Service stops. By the way, I use NT4 Server, Service Pack 6a.

Jonny...
0
Comment
Question by:jonnyfive
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 1

Expert Comment

by:AttarSoftware
ID: 2554006
When I wrote a service, I had to write a debug function that wrote text to a logfile, and then I had to examine the logfile... :O(

These are the functions I used, I hope they help (even though they do not answer your question)...

PROCEDURE Debug ( s : string ) ;
VAR
  f : text ;
BEGIN
  Assign ( f , 'C:\log.log' ) ;
  {$i-} Append ( f ) ; IF ioResult <> 0 THEN Rewrite ( f ) ; {$i+}
  WriteLn ( f , s ) ;
  Close ( f ) ;
END ;

PROCEDURE DebugBuffer ( buffer : pointer ; len : longint ) ;
VAR
      p : pchar ;
      x , c : integer ;
      s1 , s2 : string ;
BEGIN
  p := buffer ;
  x := 0 ;
  FOR c := 1 TO len DO
  BEGIN
    IF x = 0 THEN
    BEGIN
      s1 := '' ;
      s2 := '' ;
    END ;
    s1 := s1 + Copy ( '0123456789ABCDEF' , ( Ord ( p ^ ) DIV 16 ) + 1 , 1 ) +
               Copy ( '0123456789ABCDEF' , ( Ord ( p ^ ) AND 15 ) + 1 , 1 ) +
               ' ' ;
    IF Ord ( p ^ ) < 32 THEN
      s2 := s2 + #0
    ELSE
      s2 := s2 + p ^ ;
    Inc ( x ) ;
    IF ( x = 16 ) THEN
    BEGIN
      Debug ( s1 + '  ' + s2 ) ;
      x := 0 ;
    END ;
    Inc ( p ) ;
  END ;
  IF ( x > 0 ) THEN
  BEGIN
    FOR c := x TO 15 DO
    BEGIN
      s1 := s1 + '   ' ;
      s2 := s2 + ' ' ;
    END ;
    Debug ( s1 + '  ' + s2 ) ;
  END ;
END ;

Good luck with other (more suitable) answers =O)

Tim.
0
 
LVL 1

Expert Comment

by:AttarSoftware
ID: 2554015
Obviously, you then have to scatter :

  Debug( 'Got here' ) ;

And

  DebugBuffer( ptr, ptrLen ) ;

sporadically throughout your code (I can also recommend putting them inside ifdefs:

  {$IFDEF DEBUG}Debug( 'Got here' ) ;{$ENDIF}

And then you can compile them in by setting up a conditional define DEBUG (in Project/Options), and then remove them all by removing the define...  Saved me hours...

Good luck again,

Tim.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2554029
I found this somewhere on dejanews:

------------------------------------
You can supply a parameter to set the run mode (-d for debugging, for instance, which runs the service in a normal context, that is, as a standard program and not a service).
------------------------------------
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jonnyfive
ID: 2554073
All these are ideas, but not quite what I want to do. Even if I run the service as a normal program, I'm not able to "STOP" it. I just can shut it down, which doesn't help, since my bug occures exactly then. To do this, I don't need to implement any switches, I simply start the program from within Delphi and then the Service imidiatly afterwards and I can debug the startup & running, but not the shutdown. The reason for this is, when Delphi takes over, the Service manager thinks, that the Service is no longer running (but it is) and can not shut it down.

Any other ideas?
Regards, Jonny...
0
 
LVL 4

Expert Comment

by:Radler
ID: 2554572
Hi jonny,

Really your user account has the rigth "Act like part of operational system" and "Debbug program" rigths ?

Well, I dont pass by this experience, but you can get a trial version of SoftIce do debug your service and if you get the symbols from M$ the Win32  API too.

Good luck, Radler.
0
 

Author Comment

by:jonnyfive
ID: 2556018
In the meantime I fixed my problem (it was actually a timing problem (Main App finished, before a child thread was finished). Anyway... I still would like to know how to attach to a Service. Yes, I gave myself all available rights in the Usermanager (yes, also "Act like part of operating system"). Any other ideas? Does anyone got this to work?

Jonny...
0
 
LVL 1

Expert Comment

by:aldyn
ID: 2565178
You will have to add some security breaking code to your service. It includes the following steps: build a valid security descriptor with DACL set to nil, then call SetKernelObjectSecurity and pass the handle of the current process to it, followed by DACL_SECURITY_INFORMATION and security descriptor itself. Place this code before Application.Initialize for example. Now the DACL is set to nil which means that everyone has all possible rights on the service process and, of course, you can attach to it and debug it.
0
 

Author Comment

by:jonnyfive
ID: 2585636
Hi Aldyn...

I didn't had a chance to try this out yet. Do you have any "example" code ready by any chance :-]

Thanks, Jonny...
0
 
LVL 1

Expert Comment

by:aldyn
ID: 2611785
Use something like this:

procedure BreakProcessSecurity; // (;-P)
var SD: PSECURITY_DESCRIPTOR;
begin
    GetMem(SD,SECURITY_DESCRIPTOR_MIN_LENGTH);
    if not InitializeSecurityDescriptor(SD,SECURITY_DESCRIPTOR_REVISION) then RaiseLastWin32Error;
    if not SetKernelObjectSecurity(GetCurrentProcess,DACL_SECURITY_INFORMATION,SD) then RaiseLastWin32Error;
    FreeMem(SD);
end;

begin
    BreakProcessSecutiry;
    Application.Initialize;
    ............



0
 
LVL 1

Accepted Solution

by:
aldyn earned 200 total points
ID: 2615206
I've tested it and it works. Do not forget to include windows and sysutils units into uses clause.
0
 

Author Comment

by:jonnyfive
ID: 2623880
Hi Aldyn...

sorry for letting you wait this long, I just hadn't the time to test it. But I did now and it really works... I'm impressed :-) Well done...

Regards, Jonny...
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question