Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
P is for Practical
 
PerlMonks  

Win32::API

by Guildenstern (Deacon)
on Sep 11, 2000 at 18:07 UTC ( #31876=modulereview: print w/ replies, xml ) Need Help??

Item Description: Allows use of many Win32 API calls

Review Synopsis: Great for using that one function that hasn't been CPAN'ed yet.

Every now and then I find a need to call a function that would be second nature if I were using Visual C++ in a Windows environment. I happened across Win32::API, and was very pleasantly surprised at what the module can do.
Win32::API allows functions to be imported from Windows DLL files. It also makes the job much easier by making the actual calling of the function incredibly simple. No more trying to do weird type conversions and stupid pointer tricks - just pack your variables and call the function. If the function doesn't take a structure as a parameter, it's even easier - just call it like a normal Perl sub.
I've had great success at using it, and while I haven't benchmarked any results it appears to be quite fast. Coding the function call is simple, also. It takes longer to research the function and its parameters than it does to write the call. Drawbacks: There are two drawbacks to Win32::API that I've noticed so far. One could be easily remedied, while the other is probably not something I should hold my breath for. Here they are:

  1. It would be nice if the documentation listed or gave a link to information about what size standard MS parameters are. It's kind of a pain to have to track down what the difference between a WORD and a DWORD is so you know what to pack
  2. It would be really nice if support for functions that have callbacks. One example I'm thinking of is placing icons in the system tray. To be able to respond to mouse clicks, the function call that places the icon there must specify a callback that gets executed on a mouse click. AFAIK, there is no way to do this yet in Perl, and it may be impossible at the present.

Bottom Line: Win32::API is a great way to build a Windows based Perl solution without having to resort to an MS programming solution because "there's no module that does X".

Comment on Win32::API
Select or Download Code
Re: Win32::API
by Sniper (Scribe) on Aug 13, 2001 at 00:16 UTC
    Win32::API::Prototype is a very good module to use with Win32::API.
    Just Copy/Paste the function declaration in msdn for example) and call the function with its functionname!!!
    the use of Win32::API is really simple then!
    see it at Win32::API::Prototype

    David "Sniper" Rigaudiere

      Hi, i want to use fuctions in my vc++ program that are declared in windows dll file. im beginner. can u suggest me that from where can i find the very basic information about that. i must be very simple. coz im totally new to this.
        i mean to say. using windows dll fucntions in vc++ programs. like GetSystemInfo() function of kernel32.dll how can i use this and many othre functions in my vc++. plz suggest me very simple articles or tutorials. thanks
Re: Win32::API
by dada (Chaplain) on Jul 15, 2002 at 09:57 UTC
    Guilderstern wrote:

    There are two drawbacks to Win32::API that I've noticed so far. One could be easily remedied, while the other is probably not something I should hold my breath for...

    hold your breath instead :-)
    I'm working on a new release of Win32::API which will make your life a lot easier by supporting not only named types (eg. WORD, DWORD et al.) but structures too.

    soon (I hope) you'll be able to say:

    use Win32::API 0.40; Win32::API::Struct->typedef 'POINT', qw( LONG x LONG y ); Win32::API->Import( 'user32', 'BOOL GetCursorPos(LPPOINT lpPoint)' ); my $point = Win32::API::Struct->new('POINT'); my $retval = GetCursorPos($point); print "Cursor is at $point->{x}, $point->{y}\n";
    ...and callbacks will be there. I'm still cleaning things up, but I bet you'll be surprised :-)

    oh, and BTW:

    One example I'm thinking of is placing icons in the system tray. To be able to respond to mouse clicks, the function call that places the icon there must specify a callback that gets executed on a mouse click. AFAIK, there is no way to do this yet in Perl, and it may be impossible at the present.

    well, there's Win32::GUI for this.

    cheers,
    Aldo

    __END__ $_=q,just perl,,s, , another ,,s,$, hacker,,print;
      as promised (and after a mere 6 months or so :-), I've released version 0.40 of Win32::API.

      it may not be on CPAN at the time I write, but here's the link to download it from my site:
      Win32-API-0.40.tar.gz

      the synopsis I gave in my previous message still applies :-)

      there is even support for callbacks, but it is very experimental: feedback is appreciated as always.

      cheers,
      Aldo

      King of Laziness, Wizard of Impatience, Lord of Hubris

        Wow man, you must not really be the King of Laziness, cause this is awesome! I really appreciate your work.
        Very nice and Very handy package.
        I'm having a problem, however... :-)

        In short, I'm trying to use Win32::API to call CreateProcess.

        All is well except for the LPSTARTUPINFO and LPPROCESS_INFORMATION parms I pass into the call. If I create these using pack(...) for the correct number of bytes, it works fine and I can get the PID back out of the PROCESS_INFORMATION object (my ultimate goal).

        If, however, I create these parms using the Win32::API::Struct->new( 'STARTUPINFO' ) approach, the call fails. I'm even setting the .cb field to the size of the structure. The error I get back (using Win32::GetLastError()) is "Invalid access to memory location". Here's the function call I'm making (same in both cases):

        <-----------snip------------> # version 1 - succeeds my $si = pack( "L17", ((0) x 17) ); $si = pack( "LL16", length( $si ), ((0) x 16 ) ); my $pi = pack( "L4", ((0) x 4 ) ); # version 2 - fails my $si = Win32::API::Struct->new( "STARTUPINFO" ); $si->{cb} = $si->sizeof(); my $pi = Win32::API::Struct->new( "PROCESS_INFORMATION" ); my $retval = CreateProcess( "c:\\winnt\\notepad.exe", 0, # unused - cmdline ptr, unused 0, # unused - proc attributes ptr 0, # unused - thread attributes ptr 0, # unused - inherit handles 0, # unused - creation flags 0, # unused - env pointer 0, # unused - current dir ptr $si, $pi ); <-----------snip------------>
        Should CreateProcess be called the same way in both cases? When a ->new('STARTUPINFO') is called, is the memory zeroed-out upon creation? Any idea why the pack version succeeds and the Struct verion fails?

        TIA!
        D. Smith

Back to Reviews

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: modulereview [id://31876]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2014-04-17 07:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (440 votes), past polls