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
Solved

Debugging a Service in D5

Posted on 2000-02-24
11
265 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 161
how to center only a line in richedit? 4 61
Help on project with Soap 10 57
Delphi...Split view - idea? 1 89
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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