spgilmore

spgilmore

  • NA
  • 591
  • 0

Managing Windows Services from C#

Nov 24 2003 6:42 PM
(Newbie) Sorry if this is already answered, but the SEARCH doesn't seem to be working... I have the need to stop, start, pause, and query the state of Windows NT services from a remote machine. How can this be done? Please mention every method. I'll try them all and see what works best. I tried STW, but invariably get a hundred results on how to BUILD an NT service in C#. Thanks for any help in advance! Here's what I've tried: I wrote a Delphi class which will do everything I need and has been proven in battle. I decided to wrap it in a DLL and call it from C# using these declarations: [DllImport("services.dll")] private static extern int GetServiceState(string aservicename, string amachinename); [DllImport("services.dll")] private static extern int SetServiceState(string aservicename, string amachinename, uint astate); One of these calls causes a problem (I have to call them together). The function succeeds without error or exception. Heck, the results are right and the service state is actually set properly. But from them on, I get frequent but random access violations which are untrappable. I can DEBUG when they happen, but the C++Builder JIT debugger can't even find the execution address. Where it normally pulls up a CPU window, showing the current instruction and assembly source, It shows all zero-filled memory at absolute address 0x00000000 with no instructions. I also tried debugging with Delphi with the same result (no surprise), and Visual studio says that the stack is currently executing Application.Run(frmmain); Not much help. When I ran this program (and force a service to reset) about 30 times, I finally rebooted my machine to see if that would clean up the access violations. What happened was about 8 instances of the "Windows will now shut down this program (SEND REPORT TO MICROSOFT?) " dialogs popped up when the machine restarted. This may have been due to some threading issues that I had earlier, though.

Answers (1)