Solved

Warning using Win32::API

Posted on 2004-08-24
10
763 Views
Last Modified: 2009-12-16
i use the following code to use a dll - function:

use Win32::API;
$timer = Win32::API->new( "winmm.dll", "long timeGetTime()" );

but the module complains

>> Win32::API::parse_prototype: WARNING unknown output parameter type 'long' at C:/Perl/site/lib/Win32/API.pm line 273.

Why is this? "int" doesn´t work also.
To me it seems to be a bug in the module.
0
Comment
Question by:holli
  • 5
  • 4
10 Comments
 
LVL 8

Expert Comment

by:inq123
ID: 11885234
Hi holli,

This may not help, but does it work that you change it to LONG or INT?

Cheers!
0
 
LVL 1

Accepted Solution

by:
boazgOLD earned 500 total points
ID: 11886601
try using
Win32::API->Import( "winmm.dll", "long timeGetTime()" );
instead of
Win32::API->new( "winmm.dll", "long timeGetTime()" );

seems to get around it on my computer...

then all you have to do is call timeGetTime()

example

use Win32::API;
Win32::API->Import( "winmm.dll", "long timeGetTime()" );
print timeGetTime();

hope that helps...
0
 
LVL 6

Author Comment

by:holli
ID: 11889674
i forgot to say, that i use the module in a WSH/PerlScript - file (*.wsf)
all perl code in such files is implicitly eval()ed.
so the code:

use Win32::API;
Win32::API->Import( "winmm.dll", "I timeGetTime()" );
print timeGetTime();

works fine, but this:

eval
qq{
  use Win32::API;
  Win32::API->Import( "winmm.dll", "I timeGetTime()" );
  print timeGetTime();
};

does not (prints the same warning as mentioned above).

inq,
changing long to LONG has no effect.
0
 
LVL 1

Expert Comment

by:boazgOLD
ID: 11890718
it would seem that INIT sections are not called when in eval()
you have to call them yourself
there is an INIT section in Type.pm responsibe of loading the types and their meanings.

solution:
in Win32/API/Type.pm find the init section (there is only one), and copy it into a function, name it something like eval_init, and call it before using win32::API

use Win32::API;
Win32::API::Type::eval_init();
$timer = Win32::API->new( "winmm.dll", "long timeGetTime()" );

if you dont feel like finding the init and stuff, you can just copy mine:


sub eval_init
{
    my $section = 'nothing';
    foreach (<DATA>) {
        next if /^\s*#/ or /^\s*$/;
        chomp;
        if( /\[(.+)\]/) {
            $section = $1;
            next;
        }
        if($section eq 'TYPE') {
            my($name, $packing) = split(/\s+/);
            # DEBUG "(PM)Type::INIT: Known('$name') => '$packing'\n";
            $Known{$name} = $packing;
        } elsif($section eq 'PACKSIZE') {
            my($packing, $size) = split(/\s+/);
            # DEBUG "(PM)Type::INIT: PackSize('$packing') => '$size'\n";
            $PackSize{$packing} = $size;
        } elsif($section eq 'MODIFIER') {
            my($modifier, $mapto) = split(/\s+/, $_, 2);
            my %maps = ();
            foreach my $item (split(/\s+/, $mapto)) {
                my($k, $v) = split(/=/, $item);
                $maps{$k} = $v;
            }          
            # DEBUG "(PM)Type::INIT: Modifier('$modifier') => '%maps'\n";
            $Modifier{$modifier} = { %maps };
        } elsif($section eq 'POINTER') {
            my($pointer, $pointto) = split(/\s+/);
            # DEBUG "(PM)Type::INIT: Pointer('$pointer') => '$pointto'\n";
            $Pointer{$pointer} = $pointto;
        }
    }
}



stick that in type.pm and all should be well!

hope that helps
0
 
LVL 6

Author Comment

by:holli
ID: 11891227
uh, that sucks.
is it not possible to call the INIT manually? i just don`t want to change module code.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:boazgOLD
ID: 11892422
if you want to avoid changing the module code, you can add the code to the module at runtime:

$function = sub {

# INIT code here....
};
*Win32::API::Type::eval_init=$function;

can create a new module (Win32::API::EvalCompatible for example) whos is to add the code and execute it;

hope that helps


0
 
LVL 6

Author Comment

by:holli
ID: 11900361
why can´t i just Module::INIT()?
isn´t it supposed to be a function like any other?
It must have a entry in the Symbol table?
if so, it should be possible to get a ref to this symbol.
0
 
LVL 6

Author Comment

by:holli
ID: 11900628
i did a little investigation.

package t;

sub INIT
{
      print "INIT";
}

sub import
{
   my ($pkg, @args) = @_;
   print "package $pkg loaded, args ", join(':',@args), "\n";
   @foo::args = @args;
 
   my $init = $t::{INIT};
   my $foo  = $t::{foo};
   $init = \&$init;
   $foo = \&$foo;
   print "* $init *";
   print "* $foo *";
   &foo();
   &$init();
   print "end";
}

sub foo
{
      print "FOO";      
}

1;

______________


#t.pl
use lib qw (c:/ );
use t qw (x y);
print "main";


outcome:
package t loaded, args x:y
Undefined subroutine &t::INIT called at c://t.pm line 21.
BEGIN failed--compilation aborted at c:\t.pl line 2.
* CODE(0x186a230) ** CODE(0x186a170) *FOO


Why?
"CODE(0x186a230)" looks like a code-reference. why can´t i call it?

0
 
LVL 1

Expert Comment

by:boazgOLD
ID: 11901239
INIT is not a subroutene. INIT is a block of code. the only way init is diferent from just any code between curley brackets is in that is is executed as soon as compiled, thus being useful for running initialisation code. you can have multiple INIT blocks. none of them are subroutenes, and none of them can be called.
0
 
LVL 6

Author Comment

by:holli
ID: 11901358
bad news :-(
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now