The key to your problem is finding out which HRESULT is meant by "-2147221008". You can use Win32::OLE::HRESULT() for that.

To make a long story short....

use strict; use Win32::OLE qw(HRESULT); use constant CO_E_NOTINITIALIZED => 0x800401F0; my $result = -2147221008; if (HRESULT(CO_E_NOTINITIALIZED) == $result) { print "Eureka! The problem is CO_E_NOTINITIALIZED\n"; }

So the answer is, you must call CoInitialize() first before calling CoInitializeSecurity()...

And yes, this appears to work fine:

use strict; use Data::Dumper; use Win32::API; print "Setting COM security\n"; # CoInitilizeSecurity defined : # http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com +/htm/cmf_a2c_8ayh.asp my $CoInit = Win32::API->new( "OLE32.DLL", "CoInitialize", # input prototypes # 1 P, # reserved "P", "N", # return ) or warn "Can't get DLL"; my $CoInitSec = Win32::API->new( "OLE32.DLL", "CoInitializeSecurity", # input prototypes # 1 P, # Access perms # 2 N, # num of els in asAuthSvr # 3 P, # array of auth services # 4 P, # reserved # 5 N, # default auth level # 6 N, # proxy impersonation level # 7 P, # sole auth list # 8 N, # add'l capabilities # 9 P, # reserved "PNPPNNPNP", "N", # return ) or warn "Can't get DLL"; my $NULL = 0; my $RPC_C_AUTHEN_NONE = 0; my $RPC_C_AUTHEN_LEVEL_NONE = 1; my $RPC_C_IMP_LEVEL_IMP = 3; my $EOAC_NONE = 0; my $result1 = $CoInit->Call( $NULL ); if ($result1 != 0){ warn "Oops: ", Dumper($result1), "\n"; } my $result2 = $CoInitSec->Call( $NULL, 0, $RPC_C_AUTHEN_NONE, $NULL, $RPC_C_AUTHEN_LEVEL_NONE, $RPC_C_IMP_LEVEL_IMP, $NULL, $EOAC_NONE, $NULL, ); if ($result2 != 0){ warn "Oops: ", Dumper($result2), "\n"; } print "done\n";

I should add, it's probably more correct to pass in "undef" for a null pointer instead of your $NULL, because otherwise Win32::API might be passing a real valid pointer to the perl SV containing an integer zero.


In reply to Re: Calling CoInitializeSecurity() from Win32::API by meetraz
in thread Calling CoInitializeSecurity() from Win32::API by jjohn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.