krypto2000
asked on
Control panel applet and services start / pause / stop
Hello,
I'm sorry in advance for my english...
I've wrote a little service in delphi... it work greate !
Now I want to config it from control panel...
I've wrote a little program that change value into registry, and i'va added tree buttons for start / pause / stop the service
The application work perfectly, but when I transform it into a control panel applet, all function I call in the unit WinSvc raise an Access violation error :-(
I think that it is in connection with the handle but I don't know the solution...
I'm using Delphi7 Ent., no third party components.
Thanks
I'm sorry in advance for my english...
I've wrote a little service in delphi... it work greate !
Now I want to config it from control panel...
I've wrote a little program that change value into registry, and i'va added tree buttons for start / pause / stop the service
The application work perfectly, but when I transform it into a control panel applet, all function I call in the unit WinSvc raise an Access violation error :-(
I think that it is in connection with the handle but I don't know the solution...
I'm using Delphi7 Ent., no third party components.
Thanks
could you post segment of your code raised exception?
You're trying to convert the service to a control panel application? Bad idea!!!
Oh, wait. You're converting another application to a control applet. Well, it might be useful if you show some code snippets of how and where you call these services, although I suspect the problem arises because you're mixing the TApplication object from the Forms unit with the TApplication object from the CtlPanel unit. In a control application, Forms.Application is undefined! Don't use it! Not in your form, not in your code. Always refer to CtlPanel.Application instead by adding "CtlPanel." in front of the "Application" and by adding CtlPanel to your uses clause of your form...
Oh, wait. You're converting another application to a control applet. Well, it might be useful if you show some code snippets of how and where you call these services, although I suspect the problem arises because you're mixing the TApplication object from the Forms unit with the TApplication object from the CtlPanel unit. In a control application, Forms.Application is undefined! Don't use it! Not in your form, not in your code. Always refer to CtlPanel.Application instead by adding "CtlPanel." in front of the "Application" and by adding CtlPanel to your uses clause of your form...
ASKER
That i've make is only to create a new applet application, then I add a new form that i create when the
applet is activated. In this form, I cut and paste all the form description and the unit from my application.
I don't know if it is really the best way...
There is my code...
//------------------------ - Applet.dpr -------------------------- --------//
library Applet;
uses
CtlPanel,
unit_applet in 'units\unit_applet.pas' {fileCopyConfig: TAppletModule},
unit_main in 'units\unit_main.pas' {frmMain};
exports CPlApplet;
{$R *.RES}
{$E cpl}
begin
Application.Initialize;
Application.CreateForm(Tfi leCopyConf ig, fileCopyConfig);
// Application.CreateForm(TFo rm1, Form1);
Application.Run;
end.
//------------------------ ---------- ---------- ---------- ---------- ----//
//------------------------ -- unit_applet.pas -----------------------//
...
implementation
uses unit_main;
{$R *.DFM}
procedure TfileCopyConfig.AppletModu leActivate (Sender: TObject;
Data: Integer);
begin
with TFrmMain.Create(nil) do
try
ShowModal
finally
Free
end;
end;
//------------------------ ---------- ---------- ---------- ---------- ---------- -//
this is to show my Form
and there is the segment of code that raise an error
//------------------------ ----- unit_main.pas -------------------------- --//
uses WinSvc,SvcMgr;
type
TServiceState = (ssRunning,ssPaused,ssStop ped);
function getServiceState(serviceNam e:string): TServiceState;
var svc,
svcManager : SC_HANDLE;
status : TServiceStatus;
begin
svcManager := OpenSCManager(nil,nil,GENE RIC_READ);
try
svc := OpenService(svcManager,PCh ar(service Name),SERV ICE_QUERY_ STATUS);
if svc <> 0 then
try
if QueryServiceStatus(svc,sta tus) then
case status.dwCurrentState of
SERVICE_RUNNING : result := ssRunning;
SERVICE_PAUSED : result := ssPaused;
SERVICE_STOPPED : result := ssStopped;
end;
finally
CloseServiceHandle(svc);
end;
finally
CloseServiceHandle(svcMana ger);
end;
end;
procedure TfrmMain.btnGetClick(Sende r: TObject);
begin
currentServiceState := getServiceState('myService Name'); <-- access violation error !
end;
applet is activated. In this form, I cut and paste all the form description and the unit from my application.
I don't know if it is really the best way...
There is my code...
//------------------------
library Applet;
uses
CtlPanel,
unit_applet in 'units\unit_applet.pas' {fileCopyConfig: TAppletModule},
unit_main in 'units\unit_main.pas' {frmMain};
exports CPlApplet;
{$R *.RES}
{$E cpl}
begin
Application.Initialize;
Application.CreateForm(Tfi
// Application.CreateForm(TFo
Application.Run;
end.
//------------------------
//------------------------
...
implementation
uses unit_main;
{$R *.DFM}
procedure TfileCopyConfig.AppletModu
Data: Integer);
begin
with TFrmMain.Create(nil) do
try
ShowModal
finally
Free
end;
end;
//------------------------
this is to show my Form
and there is the segment of code that raise an error
//------------------------
uses WinSvc,SvcMgr;
type
TServiceState = (ssRunning,ssPaused,ssStop
function getServiceState(serviceNam
var svc,
svcManager : SC_HANDLE;
status : TServiceStatus;
begin
svcManager := OpenSCManager(nil,nil,GENE
try
svc := OpenService(svcManager,PCh
if svc <> 0 then
try
if QueryServiceStatus(svc,sta
case status.dwCurrentState of
SERVICE_RUNNING : result := ssRunning;
SERVICE_PAUSED : result := ssPaused;
SERVICE_STOPPED : result := ssStopped;
end;
finally
CloseServiceHandle(svc);
end;
finally
CloseServiceHandle(svcMana
end;
end;
procedure TfrmMain.btnGetClick(Sende
begin
currentServiceState := getServiceState('myService
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
do you have tried to make an applet with this function ??
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Okay i've the solution !!!
Maybe is my custom type declaration it don't like, maybe not, I don't know, because i've deleted all reference to my custom type
and i'm using the delphi library type : TServiceStatus.
So after this that always don't work...
Now i'm thinking, thinking : for what acces violation ? memory is not allowed for my application ??!?
After this I remember i have retired the declaration : Application.CreateForm for executing only when the user dblClick on the applet
The principle is right but not the realisation.
False :
procedure TfileCopyConfig.AppletModu leActivate (Sender: TObject; Data: Integer);
begin
with TFrmMain.Create(nil) do
try
ShowModal
finally
Free
end;
end;
True :
procedure TfileCopyConfig.AppletModu leActivate (Sender: TObject; Data: Integer);
begin
Application.CreateForm(TFr mMain, FrmMain);
with frmMain do
try
ShowModal
finally
Free
end;
end;
and now it work fine !!!
thank you everybody ! i have to split the point because you have me help together...
many thanks !
Maybe is my custom type declaration it don't like, maybe not, I don't know, because i've deleted all reference to my custom type
and i'm using the delphi library type : TServiceStatus.
So after this that always don't work...
Now i'm thinking, thinking : for what acces violation ? memory is not allowed for my application ??!?
After this I remember i have retired the declaration : Application.CreateForm for executing only when the user dblClick on the applet
The principle is right but not the realisation.
False :
procedure TfileCopyConfig.AppletModu
begin
with TFrmMain.Create(nil) do
try
ShowModal
finally
Free
end;
end;
True :
procedure TfileCopyConfig.AppletModu
begin
Application.CreateForm(TFr
with frmMain do
try
ShowModal
finally
Free
end;
end;
and now it work fine !!!
thank you everybody ! i have to split the point because you have me help together...
many thanks !