Solved

Debugging a Service in D5

Posted on 2000-02-24
11
262 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
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

15 Experts available now in Live!

Get 1:1 Help Now