Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Win32::OLE (Outlook) as a windows service

by Limbic~Region (Chancellor)
on Oct 16, 2008 at 03:35 UTC ( [id://717391]=perlquestion: print w/replies, xml ) Need Help??

Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:

All,
This is a follow up to Turn a perl script into a Win32 Service. It does not matter if I use SrvAny or Win32::Daemon, I can not get my application to run as a service using ActiveState Perl 5.10 and Vista.

I have had some success running an application just not my application. It seems that the culprit is Win32::OLE and Outlook.

use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; my $outlook = Win32::OLE->new('Outlook.Application') or die Win32::OLE +->LastError; my $namespace = $outlook->GetNamespace("MAPI"); my $folder = $namespace->GetDefaultFolder(olFolderInbox) or die Win32 +::OLE->LastError; # <--- dead my $items = $folder->Items; print STDERR "Folder: ", $folder->Name, "\n"; print STDERR "Total entries: ",$items->Count, "\n"; __DATA__ The error message is something about "METHOD/PROPERTYGET getdefaultfol +der"

I haven't yet tried ActiveState's PerlSvc (part of their PDK) but I have put in a call to the sales department. I am at a complete loss and am hoping some wise monk knows what I am missing. If anyone can get the script above to work on AS 5.10 on Vista as a service (using any tool at all), I would be very appreciative.

cross posted

Cheers - L~R

Replies are listed 'Best First'.
Re: Win32::OLE (Outlook) as a windows service
by NetWallah (Canon) on Oct 16, 2008 at 04:02 UTC
Re: Win32::OLE (Outlook) as a windows service
by pKai (Priest) on Oct 16, 2008 at 09:27 UTC

    In Re: How do I convert VBA script to Perl? - Using Win32::OLE I showed a code sketch I previously used in a Win32 service (with AS PerlSvc) to access an Exchange server inbox.

    Some random remarks:

    • That code uses the MAPI API from CDO directly, rather then instantiating Outlook, because you get CDO with installing Outlook anyway, and it was forbidden to have Outlook on a server I wanted to run that on, so I broke the spirit of the law while abiding to it by installing Outlook, and immediately uninstalling it again (with leaving the "shared component" CDO intact.)
    • To have a successful MAPI-Login I had to use Win32::OLE qw(EVENTS);, because otherwise the used threading model was not suitable (NB: I don't know what that actually means. It was just some necessary cargo cult programming to get it working ;-))
    • There are more gotchas accessing items in MAPI folders (see my comments in the code)
    • This ran on W2K3. I have no idea, if Vista makes the whole thing even more challenging.

    I hope that any of this may be of any help for you.

Re: Win32::OLE (Outlook) as a windows service
by roboticus (Chancellor) on Oct 17, 2008 at 13:33 UTC
    L~R:

    Just a guess: A service doesn't run under your account/credentials, so I suspect that the system account running the service doesn't have an Outlook account, and thus, no default folder to retrieve. If that's the case, perhaps there's a login API you could use after creating the OLE object but before accessing the default folder.

    ...bucket o' bolts
      roboticus,
      I came to a similar idea based on a question posed by BrowserUk who asked if I was sure if the account the service was running as had sufficient privilages. While I was sure that it did, I wasn't sure it would attempt to access the correct mailbox. I haven't tried it yet, but I do have some updates to share.

      The ActiveState PDK includes a tool called PerlSvc that does seem to work. It creates an "exe" and the service wraps around that. The price tag is steep ($195 or $295 for the pro edition). What I am unsure of is if it will work with Outlook when I am not logged in. The code tries to attach to an existing Outlook process before attempting to start a new instance. When I got what I believe to be a successful test run, I was already logged in with Outlook open so it really wasn't a complete test. I am using a 21 day trial for now while I test out a few other ideas.

      While I haven't been able to get it to work, example 5 in the Win32::Daemon docs looks to be the simplest complete example that would make converting an existing application fairly trivial. I have gotten example 1 to work (minus Outlook) so it does look very promising assuming I can work out my Outlook issues.

      If I find that the only stumbling block preventing my application to work as a service is interfacing Outlook (assuming I can't resolve those issues), then I will leave it up to the person in charge to decide on a solution. (run it as a scheduled task and not as a service, drop Outlook and move to POP/SMTP modules, pay for the PDK assuming a complete test works, fire me, etc).

      Cheers - L~R

        Limbic~Region:

        Here in the salt mines, our security is tied down pretty tightly, so applications attempting to access an Outlook session from a different process invoke a dialog box for the user to key in their password to give the application access to Outlook. Perhaps you're running into something like that? You might be able to test it by RDPing into your computer with a different account and trying to access the Outlook session you already have running. (IIRC, another way you can see if the service account is opening a dialog box is by looking at the Event Viewer | Applications page.)

        Other than that, I can't think of anything.

        (I'm quite selfishly hoping you'll be successful, as I tried to get an automatic process to load EMail from Outlook into a database so I could search / archive / categorize / graph it more easily. I got access, but the aforementioned security problem stonewalled me. I'll try to think on it some more and/or dig up my OutlookDB project and see if I can come up with something.)

        ...roboticus

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://717391]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found