[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Control panel applet and services start / pause / stop

Posted on 2004-10-22
7
Medium Priority
?
559 Views
Last Modified: 2010-10-05
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
0
Comment
Question by:krypto2000
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12379243
could you  post segment of your code raised exception?
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12379282
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
 

Author Comment

by:krypto2000
ID: 12379491
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 240 total points
ID: 12379696
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
 

Author Comment

by:krypto2000
ID: 12379743
do you have tried to make an applet with this function ??
0
 
LVL 6

Assisted Solution

by:vadim_ti
vadim_ti earned 360 total points
ID: 12379885
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
 

Author Comment

by:krypto2000
ID: 12380220
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline
Suggested Courses

834 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