Solved

Debugging a Service in D5

Posted on 2000-02-24
11
260 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
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 1

Expert Comment

by:AttarSoftware
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:jonnyfive
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:jonnyfive
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I've tested it and it works. Do not forget to include windows and sysutils units into uses clause.
0
 

Author Comment

by:jonnyfive
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now