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
krypto2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vadim_tiCommented:
could you  post segment of your code raised exception?
0
Wim ten BrinkSelf-employed developerCommented:
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...
0
krypto2000Author Commented:
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(TfileCopyConfig, fileCopyConfig);
  //  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
//--------------------------------------------------------------------//


//-------------------------- unit_applet.pas -----------------------//
...
implementation

uses unit_main;

{$R *.DFM}

procedure TfileCopyConfig.AppletModuleActivate(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,ssStopped);

function getServiceState(serviceName:string): TServiceState;
var svc,
    svcManager : SC_HANDLE;
    status     : TServiceStatus;
begin
   svcManager := OpenSCManager(nil,nil,GENERIC_READ);
   try
     svc := OpenService(svcManager,PChar(serviceName),SERVICE_QUERY_STATUS);
     if svc <> 0 then
       try
         if QueryServiceStatus(svc,status) then
           case status.dwCurrentState of
                SERVICE_RUNNING : result := ssRunning;
                SERVICE_PAUSED  : result := ssPaused;
                SERVICE_STOPPED : result := ssStopped;
                end;
       finally
         CloseServiceHandle(svc);
       end;
   finally
     CloseServiceHandle(svcManager);
   end;
end;

procedure TfrmMain.btnGetClick(Sender: TObject);
begin
   currentServiceState := getServiceState('myServiceName');  <-- access violation error !
end;
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Wim ten BrinkSelf-employed developerCommented:
You don't get the access violation inside the getServiceState() function? Weird, but this means that function is okay...

It could be that the form cannot assign the result to currentServiceState for some reason.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krypto2000Author Commented:
do you have tried to make an applet with this function ??
0
vadim_tiCommented:
try a little change

procedure TfrmMain.btnGetClick(Sender: TObject);
begin
   ShowMessage(IntToStr(getServiceState('myServiceName'));
end;

in your code all is looking fine
i only could not fine

currentServiceState

declaration.

may be it is in form1 , what was not created?
0
krypto2000Author Commented:
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.AppletModuleActivate(Sender: TObject; Data: Integer);
begin
   with TFrmMain.Create(nil) do
      try
        ShowModal
      finally
        Free
      end;
end;



True :

procedure TfileCopyConfig.AppletModuleActivate(Sender: TObject; Data: Integer);
begin
   Application.CreateForm(TFrmMain, 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 !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.