Link to home
Start Free TrialLog in
Avatar of Wayne Barron
Wayne BarronFlag for United States of America

asked on

Delphi - Stop and then Start a Service. (Delphi XE2 and XE10)

Hello All;

I am needing to Stop and then Start again, out Mail Server.
The Service is (example) mailSMTP.exe

How can I stop it, and then start it back up, all in one single call?
pretty much. I need this to happen once I click a button.

I have found several different OLD codes, that are not documented.
And have no idea how to use them.

Any information on this, for newer versions of Delphi.
Would be great.

Carrzkiss
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wayne Barron

ASKER

nice.
How would I call these functions?
I am assuming that I need 2 text edits on the form.
username
password
And a button.
Would I call it like so, under the button click?

Service_Stop();
Service_Start();
Also, which Jedi components would I add to the form?
Service_Stop('the_service_name_of_your_service');
Service_Start('the_service_name_of_your_service');

Open in new window


you can find the service name of a service on windows via starting services.msc
right click on the service and hit properties

the service name is at the top of that page
it's the JCL library, the one without components
it's just code

Jedi has 2 parts:
code > JCL
and components > JVCL
I have the JEDI library installed.
When I added the 2 functions in, it gives these errors.

undeclared identifier TJclSCManager
undeclared identifier TJclNtService
did you add this line ?

uses JclSvcCtrl

Open in new window

Thanks for that.
Running the project now.

This is what I have.
2 - edits (Username and Password)
2 - buttons (Stop and Start)
In each of the buttons, I have.

Service_Stop('sqlservr.exe');
Service_Start('sqlservr.exe');

When I click on the button to "Stop" the service. I get this error.

First chance exception at $777BA932. Exception class EOSError with message.
'System Error. Code: 2.
The System cannot find the file specified'.
on this line in the 'Stop"
svc := nil;
When I step over, I get this error right after.


First chance exception at $777BA932. Exception class EOSError with message
'System Error.  Code: 15100.
The resource loader failed to find MUI file'.
Process Project2.exe (14228)
try running your project as administrator

stopping and starting a service requires admin privileges
Ran Delphi as Admin, still same error.
>> The System cannot find the file specified'.

Ran the project.exe as Admin.
And it does nothing when I press the button.
The Service continues to run.
Any more idea's on this one Geert?
Hey, Geert.
Other than running as Admin.
Would being a Member of a Network Domain, have anything to do with this not working?
The program will be running on a windows 2016 server.
Right now, it is being developed on a Win10 system.
cannot find ...
wrong location of folder is very likely

lots of possibilities
company policies
local restrictions like uac (i switch this off on servers)
  ... google for "switch uac completely off" ... and apply the registry tweak too ... requires a reboot
-- company policies
My company, so. no policies in place to stop it there.
The user has complete control over the system and the server.

-- wrong location of folder is very likely
When running the code to start and stop.
I tried it this way.

Service_Stop('sqlservr.exe');

Open in new window


And this way.

Service_Stop('G:\Microsoft SQL Server\MSSQL11.CARRZ2005\MSSQL\Binn\sqlservr.exe');

Open in new window


And they both gave the same results.

I will take a look at the uac
turning the UAC completely off did not work.
I tried both paths.
With and without the address.

Tried the username, with and without the domain\user.
Geert.
Could you throw together a working project, and post the codes here, for me to test?
It is strange that you are able to get this to work, but I am not able too.

If you could throw something together, and make sure it works on your end.
And if I cannot get it to work on my end, then there is another deeper permissions issue that I will have to deal with.
Hello, Geert.
Any luck in throwing together a working example on this one?
I would really like to close this thread out but without a working example
It is in limbo.
my company ... so no policies ... lol
you are dealing with microsoft products : windows
last i checked, that was not your company ...

you are using microsoft in your company
that doesn't mean you don't have to follow microsoft policies.
for all we don't know ... it might be some CIA/FBI restriction which is causing the problem

i provided code which works.
if i would provide a project, it would be with the same code

did you try these approaches ?
https://technet.microsoft.com/en-us/library/dd421654.aspx
https://technet.microsoft.com/nl-be/library/ms190236(v=sql.105).aspx

if you can't get the command line approach to work, then it's no use to look at the delphi code
#1: You did not specify "company" as of, the software.
In most cases, when people talk about "company", they are referring to the company you work for.
NOT, the OS that you are using.

>> company policies
That is referring to the company you work for.

And the links you provided are for Command Prompts, not for doing what what were doing.
And I was using SQL Server as an example, not as the program that i wanted to start and stop.

It seems that it is most likely a DOMAIN issue, that I will have to check in on later on.

Thank you for your time in on this issue.
But please.
If you refer to "Windows" as the Company.
Please make sure that you specify that, as people are going to THINK that you are
Referring to the "Company" they work for.

I am going to close this out now.
Once again, thanks for your help.

Wayne
The sample you chose with Sql server is very specific ...
SQL Server is a database service.

Myself, I'm an oracle database administrator and before that a Delphi developer
For oracle I can choose to allow, or not, to stop and start the oracle service by specific users
what type of shutdown the database does on stopping the service
and what type of startup the database does on starting the service

If i would restart oracle service as you try to restart SqlServer service in this sample
then i know the oracle database (for the section of the company that i manage) will only shutdown correctly
but will not start ...

database services are sometimes very peculiar types of services in how they react
anti virus services can also have some very peculiar behavior

1 of my services (name = X) will only stop if another service (name = Y) is started first
otherwise it replies with access denied

when they knock on your door at night because some idiot switches off servers when feeling unhappy
you just need to take precautions ...

i still haven't found a way to protect against flooding, lack of electricity or machinegun fire
I completely understand that.
Maybe I should have been trying something else.

From my OP (First few lines)

I am needing to Stop and then Start again, out Mail Server.
The Service is (example) mailSMTP.exe

I will test it out and see if I can get it to work on our server.

Which one would I use?
The service Name
Service_Stop('MESMTPCS');

Open in new window

The Service Executable
Service_Stop('MESMTPC.EXE');

Open in new window

The Service Path to the Exacutable
Service_Stop('E:\Mail Enable\Bin64\MESMTPC.EXE');

Open in new window