Run VCL Application as Service or as Application with Forms

Hi,

I have an existing VCL application based, comprising of serveral forms, timers and worker threads. Now I've been asked to make it possible to have the program be started/executed as a service as well (e.g. before the first user logs on, without the ability to show any forms of course).

The application is based on VCL, having TFoms, TThreads and TTimers. I have also once written a system service based on the TService base class, which worked OK.

Unfortunately as it's not possible to have the main form derive form TForm and TService at the same time I have no idea what it would take to make the trick possible -- to have the application run as usual when started from the command line and to have the same executable 'switch to service mode' when started as a service (with a command line parameter). I have successfully registered the application with the SCM and can detect in WinMain when it is started as a service, but I have no idea how to make it 
'behave' like a TService one time and as a TForm based application the other time.

On a side note: I have some code required for operation in the TTimers placed on my forms and some other functions of the main form that should be executed when the service mode is active as well.

I hope some of you can point me into the right direction.


-Udo
0
Udo
1/3/2011 8:20:32 AM
embarcadero.cppbuilder.using 1848 articles. 1 followers. Follow

5 Replies
2042 Views

Similar Articles

[PageSpeed] 43

<Udo Rudolph> wrote in message news:317630@forums.embarcadero.com...

> Unfortunately as it's not possible to have the main form derive
> form TForm and TService at the same time I have no idea what
> it would take to make the trick possible -- to have the application
> run as usual when started from the command line and to have the
> same executable 'switch to service mode' when started as a service
> (with a command line parameter).

The VCL does not natively support what you are asking for.  A TService 
project cannot run as an Application, and a TForm project cannot run as a 
Service (well, it is possible, but it requires quite a few Win32 API calls 
to accomplish).  To have a single executable that can run as both an 
Application and a Service, you are best off using a third-party framework 
that supports that, such as SvCom 
(http://www.aldyn-software.com/svcom.html).  Otherwise, make a separate 
executable for your Service.  If needed, move any shared logic into a DLL 
that both Application and Service can use.

> I have successfully registered the application with the SCM and can
> detect in WinMain when it is started as a service, but I have no idea
> how to make it 'behave' like a TService one time and as a TForm
> based application the other time.

Your WinMain() code would need to be like this:

{code:cpp}
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    if( FindCmdLineSwitch("scm") )
    {
        // service code here ...
    }
    else
    {
        // forms code here ...
    }

    return 0;
}
{code}

Please read MSDN's documentation, it tells you what API functions a service 
executable is required to call:

    http://msdn.microsoft.com/en-us/library/ms685141.aspx

-- 
Remy Lebeau (TeamB)
0
Remy
1/3/2011 9:07:24 AM
Remy,

thank you very much for your answer.

I found the 3rd party framework you mentioned when I did some reseach before posting my question here. Unfortunately the framework is not free and currently I cannot afford >100EUR (the program I am writing it for is free). So only the do-it-yourself solution seems to be available. Which might be better anyway, because that way I can learn and thats part of what programming is about (at least for me). ;)

I will read the MSDN part you mentioned and try my luck with that approach.

Is there any harm if I create my TForms, along with their TTimers and TThreads to have them do 'their stuff'? I do not expect any form to show, but I would like all/most of the logic/code to remain unchanged (because much of the progam logic is indeed in TTimers and TThreads that are created/started in FormCreate of the main TForm). In my first test there seemed to be a problem when I call Synchronize in the TService (it seemed to hang). Are there any other side effects I should be aware of or is even the
 creation of a form not recommended for a Service?


-Udo


> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> <Udo Rudolph> wrote in message news:317630@forums.embarcadero.com...
> 
> > Unfortunately as it's not possible to have the main form derive
> > form TForm and TService at the same time I have no idea what
> > it would take to make the trick possible -- to have the application
> > run as usual when started from the command line and to have the
> > same executable 'switch to service mode' when started as a service
> > (with a command line parameter).
> 
> The VCL does not natively support what you are asking for.  A TService 
> project cannot run as an Application, and a TForm project cannot run as a 
> Service (well, it is possible, but it requires quite a few Win32 API calls 
> to accomplish).  To have a single executable that can run as both an 
> Application and a Service, you are best off using a third-party framework 
> that supports that, such as SvCom 
> (http://www.aldyn-software.com/svcom.html).  Otherwise, make a separate 
> executable for your Service.  If needed, move any shared logic into a DLL 
> that both Application and Service can use.
> 
> > I have successfully registered the application with the SCM and can
> > detect in WinMain when it is started as a service, but I have no idea
> > how to make it 'behave' like a TService one time and as a TForm
> > based application the other time.
> 
> Your WinMain() code would need to be like this:
> 
> {code:cpp}
> WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
> {
>     if( FindCmdLineSwitch("scm") )
>     {
>         // service code here ...
>     }
>     else
>     {
>         // forms code here ...
>     }
> 
>     return 0;
> }
> {code}
> 
> Please read MSDN's documentation, it tells you what API functions a service 
> executable is required to call:
> 
>     http://msdn.microsoft.com/en-us/library/ms685141.aspx
> 
> -- 
> Remy Lebeau (TeamB)
0
Udo
1/3/2011 10:11:11 AM
<Udo Rudolph> wrote in message news:317640@forums.embarcadero.com...

> Is there any harm if I create my TForms, along with their TTimers and 
> TThreads
> to have them do 'their stuff'?

TThread is not a problem, but using TForm would be dangerous, and TTimer can 
be.  VCL UI components are not designed to be used outside of the main VCL 
thread.  If you follow MSDN's recommendations, then your main thread would 
be blocked by StartServiceCtrlDispatcher().  If you study SvcMgr.pas in the 
VCL's source code, it does not follow MSDN's recommendations, allowing the 
main thread to remain usable for non-service components.  But either way, 
services end up being run in their own worker threads, so it is not good to 
use TForm in either scenario.  On the other hand, if you properly separate 
your UI code from your backend logic, then you will not need to involve 
TForm anymore when running in service mode, so you would minimize the risks.

> I do not expect any form to show, but I would like all/most of the 
> logic/code to
> remain unchanged (because much of the progam logic is indeed in TTimers 
> and
> TThreads that are created/started in FormCreate of the main TForm).

Best to re-design your code to make it safer and less dependant on TForm.

> In my first test there seemed to be a problem when I call Synchronize in 
> the
> TService (it seemed to hang).

Then you were likely not doing something correctly in your project, because 
TService runs in a worker thread, allowing the main thread to process 
Synchronize() requests.

> is even the creation of a form not recommended for a Service?

It is not recommended.

-- 
Remy Lebeau (TeamB)
0
Remy
1/3/2011 10:27:08 AM
Ok, thanks again for taking the time to help.

I will see how far it's possible to separate the UI part from the logic (my main form's cpp file alone has now almost 10k lines of code -- most of it for updating the UI and drawing graphs, so they can be gotten rid of easily in the context of the service). I hope I will be able to disentagle the rest without introducing new bugs into the already working code. ;)


-Udo
0
Udo
1/3/2011 10:41:05 AM
You may want to look into this:

http://support.microsoft.com/kb/137890

> {quote:title=Udo Rudolph wrote:}{quote}
> Ok, thanks again for taking the time to help.
> 
> I will see how far it's possible to separate the UI part from the logic (my main form's cpp file alone has now almost 10k lines of code -- most of it for updating the UI and drawing graphs, so they can be gotten rid of easily in the context of the service). I hope I will be able to disentagle the rest without introducing new bugs into the already working code. ;)
> 
> 
> -Udo
0
Jaimi
1/4/2011 2:48:56 PM
Reply:

Similar Artilces:

Can connect to VCL Forms Application but not a Service Application
I have DataSnap VCL Forms App server that I can run and connect to on the same machine or separate machines as long as I open the port. When I create the same app as a Service Application (install with MyServiceApp.exe /install with "Service installed successfully" response) and I try to connect, I get "Socket Error # 10061 - Connection Refused" whether on the same machine or installed on a separate machine. Any ideas? Il Mon, 18 Nov 2013 11:09:16 -0800, Douglas Downs <> ha scritto: >I have DataSnap VCL Forms App server that I can run and connect to on the ...

Application as Service is an advanced system utility which lets you run any application as Windows 2000/XP/2003 Service at system boot. Specify application priority, run your program without the need
Application as Service is an advanced system utility which lets you run any application as Windows 2000/XP/2003 Service. You can easily configure Application as Service using its intuitive GUI interface or command line configurator directly from your software. Application as Service features very low CPU usage, custom environment variables, monitoring and detailed logging as well as many other features. You can use Application as Service software to: Launch any application as Windows 2000/XP/2003 service at system boot, before user logon; Automatically start applications before and after ser...

How to get Application EXE folder when Application is runned as a service?
Hi when trying to get Application.exe folder it returns an empty string or windows\system32 folder name instead of the current exe path, al also troed using GetModuleName API (as when working with DLLs) function but this results the same thing. fFullFileName:= GetModuleName(HInstance); - it works fine for service and ISAPI. > {quote:title=Francisco Armando Dueñas Rodriguez wrote:}{quote} > Hi when trying to get Application.exe folder it returns an empty string or windows\system32 folder name instead of the current exe path, al also troed using > GetModuleName API (as when...

How to Run my .exe file or Application in the Background using .NET Application like explorer.exe file is Running always in the Background...(you can see in task Manager..)
Hi , This is Sundar , i want to develope one small Application in windows or web ,that should send the mail continiously according to at what time scheduled in the application and that should Running in the Background always like windows service,SQL server ........ so , please help me ......................  what;s the problem if you develop an windows service and install it in your system.  http://www.15seconds.com/issue/021007.htmGive a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.  see, i have created the Windows services and wh...

Application to Application
Hi,  Curious on what different solutions there are for one application that is closing down to contact another application. I have the closing down applicaton sending something to the database and for the other application to monitor the database records, but I think that is a horrible solution. Another idea would be when the closing down application sends a message to the database for the database to then respond by sending a message to the other application, but that means the database would have to know where the other application is.  If possible, the security issue mak...

Login to server application using with local running application Credentials.
Hi, This is Ravichandra, I have a Requirement that i need login Directly to server application from local application which is installed in local system using with local application login details. I am calling the server application as follows. Response.Redirect(@"http://192.168.0.57/AE/Home.aspx?PortalUserName=" + drpUserName.SelectedItem.Text + "&PortalPwd=" + txtPassword.Text, true);I am passing the login values using URL query string. I am not able to get these querystring values in server application. I am getting Request.QueryString.Count is Zero. How...

run as a service, not run as an application
How to make a program (PowerPro) to run as a service than run as an application. Your kindest most honest response will be appreciated. ------ PIII / 1Ghz / 384mb ram / WinXP Pro Sp2 / AntiVir / Kerio 4.2.1 / Ad-aware / Spybot / a-squared / Spyware Doctor / Proxomitron / A4Proxy "John Abraham" <Nobodyknowsabcdef@unknown.com> wrote: > How to make a program (PowerPro) to run as a service than run as an > application. Use the MS utility srvany.exe, MS info here: How To Create a User-Defined Service http://support.microsoft.com/kb/q137890/ G...

Application Launcher NT Service not found to run the application as system User.
Fixed problem on posting below, and now getting the following error:- Could not launch application <Application Name>. Application Launcher NT Service not found to run the application as system User. Again, Netware 6.0 SP3, ZfD 4.01. Can someone please explain why this stupid error is now occurring? Thanks Lenny says... > Again, Netware 6.0 SP3, ZfD 4.01. > > Can someone please explain why this stupid error is now occurring? > looks like you didn't install nalntsrv and are using an application which is configured for unsecured / s...

how to use web service in windows form application ?
hello,All:            I am using  vs.net 2003,now I want to use web service in my win forms application .           can anyone give me a sample or web address  ? http://msdn2.microsoft.com/en-us/library/scf355x6(VS.80).aspx   this is wat u lokin for if yes >>> mark this post as anwser>> have a nice day...

Problem: Application Launcher NT Service was not found to run the application as system user.
Every time i try to launch an application through the application window on a workstation i get the following error message: Could not launch [application name] (id=number) Problem: Application Launcher NT Service was not found to run the application as system user. does anyone know why this happens and how to fix it? On Thu, 09 Sep 2004 23:13:38 GMT, bmcguirk@northbaybancorp.com wrote: > does anyone know why this happens and how to fix it? did you install the agent including the nalntsrv? -- Marcus Breiden Please change -- to - to mail me. The content of this mai...

Console Application to Forms Application
Howdy I know this sounds old, but I have a C++ builder application that runs very nicely as a console application, based on old C code that I ported to C++. This queries the RS232 port using a third party tool (Async32 by TMSSoftware). As it runs fine as a console app, is there a way to add a form or convert this project to a form based application? I could obviously change the console printf statements, etc., but I'd like to just get it to compile and run as a form based app. Thanks Doug try out the components managing RS232 Comports there http://www.deepsoftware.com &g...

Using a windows form application on a webpage/HTML/ASP.net web application (with some other controls).
hi there  I am a final year engineering student and doing a project to design a website which include some kind of controls to control the water level of a tank. but I got some problems with putting a windows form application (with the extra.dll files and some controls that are not supplied by VS2005) on a webpage by using VS 2005. in addtion maybe i also need to ask the client to download the  several .dll ( maybe 10)  files . can anyone please help?    Thanks a lot >< Jessica These links should help you out. http://aspnet.4guysfromrolla.co...

Using Windows Terminal Services to Run a Single Application
Using Group Policy and some scripting to publish a single application to Remote Desktop users. http://makeashorterlink.com/?A18235D39 ...

Run application program using RUN
I need to run a batch program. However, the batch file is located in a folder that contains spaces. So I included quotes around the complete path but still the program fails to run. Run('"c:\ghj gdsh jh\test.bat"') or Run("c:\ghj gdsh jh\test.bat") does not work! Is there another way or external API available to execute a batch file from withing PB? We use that all the time, and it works just fine: string ls_runStr ls_runStr = '\\servername\share name\folder name\program name.exe' Run( ls_runStr ) What's...

Web resources about - Run VCL Application as Service or as Application with Forms - embarcadero.cppbuilder.using

Application - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

The Bachelor 2016 application process can get explicit - Laurina Fleure
CHANNEL Ten is calling on singles to sign up for a chance to find love on The Bachelor , but willing applicants have to open a lot more than ...

Perth man’s Afghan wife slapped with 10-year visa application ban - PerthNow Search Search
A MAN’S three-year struggle to have his wife live with him in Australia has ended in heartache after his spouse was slapped with a 10-year ban. ...

Radius CEO Says Drug Application Delayed for `Work-Life Balance'
... Ward said. Radius fell 11 percent to $61.51 Tuesday after the company said in a statement that it was delaying submitting a new drug application ...

MBA: Mortgage Applications Decrease in Latest MBA Weekly Survey, Purchase Applications up 24% YoY
From the MBA: Mortgage Applications Decrease in Latest MBA Weekly Surve Mortgage applications decreased 3.2 percent from one week earlier, according ...

Elon Musk Requests Applications For Autopilot Software Engineers On Twitter
... and more quickly achieve “generalized full autonomy.” So, if you ever wanted to work for Tesla in such a [&hellip Elon Musk Requests Applications ...

Mortgage applications fall 3.2%
A definitive rise in mortgage interest rates over the last month is keeping borrowers at bay.

Help wanted: Obama's tech-training project now accepting applications
Organizers of innovative training programs can now apply for a federal grant to help prepare low-wage workers for more-lucrative tech jobs.

Number Of Daily Applications To French Military TRIPLES Overnight
Number Of Daily Applications To French Military TRIPLES Overnight

Google pushing virtual reality applications
Google has been pushing its R&D into virtual reality applications and is looking to enhance YouTube with the ability to provide virtual reality ...

Resources last updated: 11/29/2015 8:55:58 AM