Board index » cppbuilder » SendMessage() to the service app

SendMessage() to the service app


2004-07-26 09:41:11 PM
cppbuilder9
Hi,
Is it possible to send a custom message via SendMessage()
mechanism from the 'Desktop App' to the 'Service App', which
both resides on the same PC?
I need this just to inform the 'Service App', for example,
to reload the configuration because it's changed by the
'Desktop App'.
I know that I can use sockets for this but I think it's too
robust solution (defining IP and port) for sending just one
RELOAD command.
Best regards,
Vladimir Stefanovic
 
 

Re:SendMessage() to the service app

"Vladimir Stefanovic" < XXXX@XXXXX.COM >wrote in message
Quote
Is it possible to send a custom message via SendMessage()
mechanism from the 'Desktop App' to the 'Service App',
which both resides on the same PC?
No. Services do not use windows let alone window messages. Look at the
OpenService() and ControlService() function in the Win32 API instead.
ControlService() can send custom SCM messages to a service. Then, the in
the service itself, you can override the virtual DoCustomControl() method in
order to respond to custom messages.
Gambit
 

Re:SendMessage() to the service app

Thanks,
I have now enough 'material' to start...
Quote
>Is it possible to send a custom message via SendMessage()
>mechanism from the 'Desktop App' to the 'Service App',
>which both resides on the same PC?

No. Services do not use windows let alone window messages. Look at the
OpenService() and ControlService() function in the Win32 API instead.
ControlService() can send custom SCM messages to a service. Then, the in
the service itself, you can override the virtual DoCustomControl() method
in
order to respond to custom messages.


Gambit


 

{smallsort}

Re:SendMessage() to the service app

Remy,
Following your directions I succeded to control the service
and to send custom messages to it. This rough code works
fine if someone is interested (I also included starting, stopping,
pausing and continuing the service).
What about closing the service handles CloseServiceHandle()?
I added closing but it is not mentioned explicitely that it's
important but it seems prety normal to close the handles.
<CONFIG APP>
void __fastcall TForm1::SharedButtonClick(TObject *Sender)
{
int ButtonTag = ( (TButton*)Sender )->Tag;
SC_HANDLE H1 = OpenSCManager ( NULL, NULL, SC_MANAGER_ALL_ACCESS );
DWORD DesiredAccess;
if ( ButtonTag == 0 ) DesiredAccess = SERVICE_USER_DEFINED_CONTROL;
else DesiredAccess = SERVICE_ALL_ACCESS;
SC_HANDLE H2 = OpenService ( H1, "TestService", DesiredAccess );
bool R;
SERVICE_STATUS sstatus;
switch ( ButtonTag )
{
case 0: R = ControlService ( H2, 200, &sstatus ); break;
case 1: R = ControlService ( H2, SERVICE_CONTROL_STOP, &sstatus );
break;
case 2: R = ControlService ( H2, SERVICE_CONTROL_PAUSE, &sstatus );
break;
case 3: R = ControlService ( H2, SERVICE_CONTROL_CONTINUE, &sstatus );
break;
case 4: R = StartService ( H2, NULL, NULL ); break;
}
CloseServiceHandle ( H2 );
CloseServiceHandle ( H1 );
}
<SERVICE>
bool __fastcall TTestService::DoCustomControl(unsigned CtrlCode)
{
// Some result checking code ...
TIniFile* pif = new TIniFile ( "C:\\service.ini" );
if ( CtrlCode == 200 ) pif->WriteString ( "Test", "Value",
ntToStr( CtrlCode ) );
else pif->WriteString ( "Test", "Value", "Not 200!" );
delete pif;
return ( true );
}
 

Re:SendMessage() to the service app

"Vladimir Stefanovic" < XXXX@XXXXX.COM >wrote in message
Quote
What about closing the service handles CloseServiceHandle()?
I added closing but it is not mentioned explicitely that it's
important but it seems prety normal to close the handles.
Yes, you have to close the handles.
Gambit