Delphi OLE automation inside a windows service

Hello: I have a simple question.

I did a program (a service) with a timer: every 10 minutes, this service
goes to a database, take records and sends informative mail to customers
using Outlook with OLE automation.

This program, as standalone program, works fine.

But this program, converted as service, call Outlook and the service stop.

Any ideas?

The service code:

procedure TDistriMail.ServiceExecute(Sender: TService);   // the OnExecute
service
begin
  Timer1.Enabled := True;

  while not Terminated do
     ServiceThread.ProcessRequests(True);// wait for termination

  Timer1.Enabled := False;
end;

procedure TDistriMail.Timer1Timer(Sender: TObject);   // the OnTimer
component call a procedure every 10 minutes
begin
    ComponeMensajes;
end;

Procedure TDistriMail.ComponeMensajes;

var
   bandejas, carpetas, carpeta, mensaje: olevariant;
   i: integer;
  texto: tstringlist;

begin
    // this is a example code

    Outlookapplication.Connect;

    bandejas := Outlookapplication.GetNameSpace('MAPI');
    bandejas.Logon(EmptyParam, EmptyParam, False, True);
    carpetas := bandejas.GetDefaultFolder( 4 );  //4 es salida, 6 es entrada

          texto := tstringlist.create;
          texto.LoadFromFile('p.txt');

          mensaje := Outlookapplication.createitem(0);  // create a message

          mensaje.to := 'ejemplo@ejemplo.com';
          texto := tstringlist.create;

          texto.LoadFromFile('e:\ensayo\p.txt');   // get some text

          for i := 0 to texto.count - 1 do
              mensaje.body := mensaje.body + texto[i] + #13#10;   // format
the text

          mensaje.subject := 'Mail ' + TimeToStr( Time );   //

          mensaje.send;

    texto.free;
    mensaje := null;

    bandejas.Logoff;
    Outlookapplication.Quit;
    Outlookapplication.Disconnect;
end;

Thank you, fellas.

Pablo Romero
Cordoba, Argentina
0
Pablo
9/25/2008 8:09:50 PM
embarcadero.delphi.ole 598 articles. 1 followers. Follow

3 Replies
1696 Views

Similar Articles

[PageSpeed] 11
Get it on Google Play
Get it on Apple App Store

"Pablo Fabian Romero" <info@pabloromero.com.ar> wrote in message 
news:21165@forums.codegear.com...

> The service code:

You are not calling CoInitialize/Ex() and CoUninitialize() in your OnExecute 
event.  TService runs in its own thread, not the main thread, so you have to 
initialize the COM library manually before you can use any COM objects in 
that thread context.

    procedure TDistriMail.ServiceExecute(Sender: TService);   // the 
OnExecute
    begin
        CoInitialize(nil); // <-- add this
        try
            Timer1.Enabled := True;

            while not Terminated do
                ServiceThread.ProcessRequests(True);// wait for termination

            Timer1.Enabled := False;
        finally
            CoUninitialize; // <-- add this
        end;
    end;

With that said, you shouldn't really be using the OnExecute event at all. 
You are just duplicating what TService already does internally for you when 
no OnExecute handler is assigned.  Use the OnStart and OnStop/Shutdown 
events instead:

    procedure TDistriMail.ServiceStart(Sender: TService; var Started: 
Boolean);
    begin
        CoInitialize(nil);
        Timer1.Enabled := True;
        Started := True;
    end;

    procedure TDistriMail.ServiceStart(Sender: TService; var Started: 
Boolean);
    begin
        ServiceShutdown(Sender);
        Stopped := True;
    end;

    procedure TDistriMail.ServiceShutdown(Sender: TService);
    begin
        Timer1.Enabled := False;
        CoUninitialize;
    end;

-- 
Remy Lebeau (TeamB)
0
Remy
9/25/2008 8:27:42 PM
Ok, My friend. I assume that

Watch

>     procedure TDistriMail.ServiceStart(Sender: TService; var Started:
<------ # 1
> Boolean);
>     begin
>         CoInitialize(nil);
>         Timer1.Enabled := True;
>         Started := True;
>     end;
>
>     procedure TDistriMail.ServiceStart(Sender: TService; var Started:
<--- This would be ServiceSTOP?
> Boolean);
>     begin
>         ServiceShutdown(Sender);
>         Stopped := True;
>     end;
>
>     procedure TDistriMail.ServiceShutdown(Sender: TService);
>     begin
>         Timer1.Enabled := False;
>         CoUninitialize;
>     end;

I did not put here the hole code that Delphi generate for the service, just
the OnExecute part. Using what you adviced, The rest of the code remains the
same, but the OnStart, OnStop and OnShutdown events. I have to erase the
OnExecute part. Ok, I will test.

Thank you again, my friend, God Bless You.

Pablo Romero
Cordoba, Argentina
0
Pablo
9/26/2008 10:57:33 AM
"Pablo Fabian Romero" <info@pabloromero.com.ar> wrote in message 
news:21481@forums.codegear.com...

>>     procedure TDistriMail.ServiceStart(Sender: TService; var Started:
> <--- This would be ServiceSTOP?

Yes.

-- 
Remy Lebeau (TeamB)
0
Remy
9/26/2008 6:14:22 PM
Reply:

Similar Artilces:

OLE Automation with Delphi and MS Word
Hi, I'm working with Delphi for a while now, but it's the first time that I need to export in Word some data taken from a sqlite database. With SQLite there are no problems, everything works as it should. With Word, also because of the lack of good part of documentation, I'm currently having troubles, in particular, in the moment when i go to split a cell of a table, depending on the number of occurrences found with a SQLite query (1 occurrence = do nothing, 2 occurrences = split the cell into two rows, 15 occurrences = split the cell into 15 rows and so on). When i create ...

OLE Automation with Delphi and MS Word [Edit]
Hi, I'm working with Delphi for a while now, but it's the first time that I need to export in Word some data taken from a sqlite database. With SQLite there are no problems, everything works as it should. With Word, also because of the lack of good part of documentation, I'm currently having troubles, in particular, in the moment when i go to split a cell of a table, depending on the number of occurrences found with a SQLite query (1 occurrence = do nothing, 2 occurrences = split the cell into two rows, 15 occurrences = split the cell into 15 rows and so on). When i create ...

WordPerfect: OLE automation through OLE control
OK, I connected to WordPerfect from PB6.5 by using OLEObject like: OLEObject ole_obj ole_obj= CREATE OLEobject li_rc=ole_obj.ConnectToNewObject ("WordPerfect.PerfectScript") ........ Now it accepts automation commands. But my goal is to start OLE automation through Object property of OLE control. Something like: ole_1.Activate(Offsite!) ole_obj=ole_1.Object.WordPerfect.PerfectScript ..... And that does not work though it does with Word. Does somebody know if it is possible at all to make OLE automation through OLE control and how to do that? Thanks ...

OLE Word97 and ole objects vs ole controls
--------------50D299EA9D99A20C83E801AB Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit I am new to OLE automation. I'm trying to connect to a word97 document and filling forms fields with information. I'm trying to use the following code. I'm getting the following error in my log file: DateTime: 10/27/99 18:32:07 Title: System Error Message: Error Number 36. Error text = Name not found accessing external object property object. Window/Menu/Object = w_ole_test. Error Object/Control = cb_okay. Script = clicked. Line in Script = 22. ...

OLE Excel with Delphi 7
I am having troubles opening an Excel file with Delphi 7. I think I really need some Delphi specific examples or some help files. I am trying to follow examples in this forum and also seeing there is limited information in the help files. For example I try the following -- which seems to be working for others in the forum, I am assuming with later versions of Delphi Xapp1.Workbooks.open('...filename...'); //where file name is a valid name This gives me an error "Not enough actual parameters", One example I see has me try this: Xapp1.Workbooks.Open(&#...

Ole written in Delphi and Powerbuilder
Hi, We are using powerbuilder 9.0 build 5507, Due to some very very special reason, My company is not going to upgrade with a newer version, for example, powerbuilder 9.0.1 build 7275 I have choosen Delphi to write some very useful Ole component which can works fine under Powerbuilder 9.0.1 build 7275, But they failed working in 9.0 build 5507. Since my company is not going to upgrade our powerbuilder with a newer version. So , I need to know is it possible to connect the powerbuilder 9.0 build 5507 with a ole server written in Delphi ? What should ...

OLE Word97 and ole objects vs ole controls
--------------50D299EA9D99A20C83E801AB Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit I am new to OLE automation. I'm trying to connect to a word97 document and filling forms fields with information. I'm trying to use the following code. I'm getting the following error in my log file: DateTime: 10/27/99 18:32:07 Title: System Error Message: Error Number 36. Error text = Name not found accessing external object property object. Window/Menu/Object = w_ole_test. Error Object/Control = cb_okay. Script = clicked. Line in Script = 22. ...

Delphi and Delphi for .Net
It seems that Delphi for .Net is slower than Delphi Win32 native applicaiton. I would like to know is it true all .Net application is slower than Win32 native applicaiton or it is Delphi for .Net only. Your information is great appreciated, Inung On 2011-06-21 18:20:17 +0100, Inung Huang said: > It seems that Delphi for .Net is slower than Delphi Win32 native applicaiton. > I would like to know is it true all .Net application is slower than > Win32 native applicaiton or it is Delphi for .Net only. If you are only running the code in the application once then, yes, yo...

Any where to offer services as Delphi/Delphi for PHP freelance developer?
Hi, I'm a freelance Software Engineer. One of many that in current difficult days, is pushing for self-employment... offering services as Business Analyst and Developer, using Delphi, Delphi for PHP, MySQL, PostgreSQL. Any where in this forum or other you might know where to offer these services to the world? Thank you, Guillermo Guillermo, The Delphi for PHP forum does have For Hire and Wanted For Hire sections if you haven't seen those yet: http://forums.delphi-php.net/ Thanks. -Phil > {quote:title=Guillermo Najar wrote:}{quote} > Hi, > > I&#...

Show an Application inside a window (OLE?)?
hi, Is it possible to show an application inside a window? (i.e. as OLE-Control) I wan't to open the app with RUN TIA, gzim Only if it is an OCX (Active X) control. Excel is a good example. You can't just place any old application in there. Using Run will just launch the application outside of your application. > hi, > > Is it possible to show an application inside a window? > (i.e. as OLE-Control) I wan't to open the app with RUN > > TIA, > gzim > > ...

Windows Service
I have two services - A and B I have to start Service B, on OnStart method of Service A. How to do this? Thanks    Well your service A would have a overriden method called on_start. Here you will need to use the servicecontroller and iterate through all services on the local machine to identify Service B. This can be a unique service name. Once you have found Service B you can start it.You however need to understand about permissions, depending on what context Service A is running under, it may or may not have enough permissions to start another service. ...

Problem upgrading web service client from Delphi 7 to Delphi XE
In trying to upgrade our web service client to Delphi XE we have now get a web service error of Invalid Format. When we examine the SOAP request we see the following differences from Delphi 7 vs Delphi XE In Delphi 7 we get <SomeStuff xmlns="" xsi:nil="true" /> In Delphi XE we get <SomeStuff xsi:nil="true" /> We have narrowed the failure down to these differences in the client request. Is there anyway to get Delphi XE to generate a SOAP request with the xmlns attribute set to blank as it used to be? Hello, > In trying to u...

Delphi
Dear All, Sorry to ask 2 questions in 24 hours! (I have just come back to using Delphi after a break and I am finding the 2009 version fanstastic! - Just installing 2010 also!) Does anyone know how to call windows to hide/show the Wireless Networking Tray icon in Windows XP from Delphi? Many Thanks Mark <Mark Williams> wrote in message news:178978@forums.codegear.com... > Does anyone know how to call windows to hide/show the > Wireless Networking Tray icon in Windows XP from Delphi? System Tray icons can only be shown/hidden by the apps that call Shell_Notify...

Is there a specific Windows OLE config required to support PB OLE controls
Hi there, I have a PB 8.0.1 app that stores external documents as LONG fields in an Oracle 7 database. The app retrieves the data, assigns it to an OLE control then activates the control. On 4 out of 12 PCs the line 'ole_1.dataobject = ....' crashes with a bad runtime function error. My guess is either the data is being corrupted somehow by SQL*Net or the Windows OLE configuration is different on the offending PCs. The client IT people say the PC config are the same but i cannot believe that. Does anyone have any suggestions as to what aspect of the PC config I should l...

Web resources about - Delphi OLE automation inside a windows service - embarcadero.delphi.ole

Automation - Wikipedia, the free encyclopedia
For a hierarchical presentation of automation topics, see Outline of automation . For other uses, see Automation (disambiguation) . Automation ...

Automation World - Covering the field of industrial automation
AutomationWorld.com delivers content for the Automation Buying team, which includes Information Technology and Networking; Operations and Engineering; ...

Facebook Ads API Partner AdStage: Campaign Automation, $6.25M Series A Funding
AdStage , a Facebook ads API partner, announced Wednesday the launch of campaign automation — as well as a $6.25 million round of Series A funding. ...

Mind blown! Facebook’s f8 makes a mobile move into marketing automation
... Thus, you’ll need to build a mobile app (if you’re enterprise) or use an app-building platform (such as GroSocial). The marketing automation ...

Automation Heroes (@automationhero) on Twitter
Sign in Sign up To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor ...

Automation World
Automation World is your link to the why and how of manufacturing automation. The purpose of this group is to connect manufacturing engineers,... ...

Samsung SmartThings Home Monitoring Kit offers easy automation
Home automation and monitoring is getting easier than ever. The days when you had to hire a contractor to wire up your home for security or to ...

Factory Automation Will Speed Forward with A.I., Says Bernstein
... tech-industrial companies such as Corning (GLW) and AU Optronics (AUO), this afternoon offered up a thinks piece on robotics and factory automation, ...

Is home automation in your future or are you using it now?
Home automation gets touted by some as the future, but it's really the now. A seemingly endless list of products are on the market and they allow ...

3 Growing Digital Trends for 2016: Automation, Personalization, Gamification.
1. Marketing Automation What is it? Marketing automation is a category of technology that allows companies to streamline, automate, and measure ...

Resources last updated: 1/13/2016 6:27:15 PM