CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Can I call Win32 API's??

 



yapp
User

Nov 11, 2001, 4:03 AM

Post #1 of 8 (2104 views)
Can I call Win32 API's?? Can't Post

Can I call windows 32 API's from perl??

Can I also declare them?

Does that mean I need a C compiler, or can I import C++ header files??

Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com


mhx
Enthusiast

Nov 11, 2001, 4:19 AM

Post #2 of 8 (2103 views)
Re: Can I call Win32 API's?? [In reply to] Can't Post


In Reply To
Can I call windows 32 API's from perl??

Sure. There are (at least) two ways:

1. A lot of API calls can be reached with the Win32:: modules, but I'm sure you knew that already.

2. Write your own XS wrapper for the API calls you need. This of course requires you to have a C compiler, most preferably VisualC++.


In Reply To
Can I also declare them?

What do you mean?


In Reply To
Does that mean I need a C compiler, or can I import C++ header files??

If you write your own XS code, you need a C compiler. Where do you want to import C++ header files? BTW, the Windows API isn't C++.

Hope this helps a little.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



yapp
User

Nov 11, 2001, 10:50 PM

Post #3 of 8 (2099 views)
Re: Can I call Win32 API's?? [In reply to] Can't Post

Well, IF I can import the .h files from C, I can use the API's in it. (those funny #declare lines).

In Reply To
What do you mean? - can I also declare them?

About the declaring. I'm used to declare the API's in Visual Basic. Since VB doesn't have .h files, I read through the MSDN, or .h files, and convert those statements into VB, like this: (every win32 knows these API's in C)

Code
' this is Visual Basic code 
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Declare Sub Sleep lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function GetWindowRect lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long

Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com


mhx
Enthusiast

Nov 11, 2001, 11:58 PM

Post #4 of 8 (2097 views)
Re: Can I call Win32 API's?? [In reply to] Can't Post

You don't have to do that if you're writing an XS extension in C, a simple

Code
#include <windows.h>

is sufficient and has always served me well. Wink

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



RhetTbull
stranger

Nov 18, 2001, 10:43 AM

Post #5 of 8 (2086 views)
Re: Can I call Win32 API's?? [In reply to] Can't Post

Yes, you can. You don't need to import the header files if the API lives in a DLL and you know the prototype declaration. You need the Win32::API module (you can install this via ActiveState's ppm). Here's an example of using Win32::API to import and use an API routine (GetShortPathName which gets the MSDOS'ified version of a Windows file name)
#---start of script---
use strict;
use warnings;
use Win32::API;

#import the function
#GetShortPathName is in kernel32.dll, it takes two pointers to strings and a DWORD and returns a DWORD
my $GetShortPathName = new Win32::API("kernel32","GetShortPathName",[ qw(P P N) ],'N');
die "could not import GetShortPathName" if not defined $GetShortPathName;

#the name we want to convert
my $longname = 'C:\Program Files\Microsoft Office\Office\OFREAD9.TXT';

#create a string big enough to hold the shortname
my $shortname = " " x 80;

#retval will be length of the DOSified short name
my $retval = $GetShortPathName->Call($longname,$shortname,80);
print "retval = $retval\n";

#trim the trailing blanks
#recall that $shortname = " " x 80
#and retval is the length of the name returned by GetShortPathName
$shortname = substr($shortname,0,$retval);

print "longname = \n\"$longname\", \nshortname = \n\"$shortname\"";
#---end of script---

On my system, this outputs:
retval = 39
longname =
"C:\Program Files\Microsoft Office\Office\OFREAD9.TXT",
shortname =
"C:\PROGRA~1\MICROS~2\Office\OFREAD9.TXT"

One more example, using SystemParametersInfo to change the wallpaper:

#---start of script---
use strict;
use warnings;
use Win32::API;

my $SystemParametersInfo = new Win32::API("user32","SystemParametersInfo",[ qw(I I P I) ],'I');

if (not defined $SystemParametersInfo ) {
die "could not import SystemParametersInfo";
}
my $wallpaper = "c:\\winnt\\Blue Lace 16.bmp";
my $action = 20; #reference http://support.microsoft.com/support/kb/articles/Q97/1/42.ASP to see which constant to use
my $saveWinINI = 0; #save this in the user profile?
my $param = 0; #dependent on what $action is

#change the wallpaper
my $retval = $SystemParametersInfo->Call($action,$param,$wallpaper,$saveWinINI);
print "SystemParametersInfo returned $retval\n";
#---end of script---

Good luck! This method works great for importing predefined APIs that live in DLLs. If you need more complex interfacing then look into XS or Swig.






yapp
User

Nov 19, 2001, 1:46 AM

Post #6 of 8 (2080 views)
Re: Can I call Win32 API's?? [In reply to] Can't Post

Whoa.

That's pretty cool.

I asume that the constants in the qw(P P ...) are the same the ones pack() and unpack() use?



-- i am getting lazy here --
I can just ask it, and not try to understand everything from perldoc, which is difficult sometimes.

- What are the constants?

- Some time ago I tried making a binary file in perl. Although I used the network-long (N or n), by file didn't get compatible for all operating systems. The file was read in very different in Win32 and Linux (due endian differences)

Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com


RhetTbull
stranger

Nov 20, 2001, 6:35 AM

Post #7 of 8 (2072 views)
Re: Can I call Win32 API's?? [In reply to] Can't Post

>I asume that the constants in the qw(P P ...) are the same the ones pack() and unpack() use?

No. These constants are specific to Win32::API and are used to tell Win32::API what your function prototype looks like. You should really read the Win32::API docs before trying to use it. Here's an excerpt that tells what the constants are:

"I": value is an integer
"N": value is a number (long)
"F": value is a floating point number (float)
"D": value is a double precision number (double)
"P": value is a pointer (to a string, structure, etc...)

If the function uses a BOOL or bool, I've had good luck specifyng it as an integer (I).

As for your question about cross-platform use of pack and network byte order, you need to make sure you unpack the data using the same byte order you packed with. Network byte order is only transparent when sending things over the network (e.g. TCP/IP), hence it's name. When the OS reads a file off disk it has no idea what byte order the data is in so it uses the native byte order. Here's an example using pack that was tested on both a little endian and big endian machine:

#!/usr/bin/perl -w
use strict;

#create some data
my @data = qw(123456789 42 314 217 987654);
my $bin = pack('N*',@data);

open FH,"+>test.bin" or die "open: $!";
binmode FH; #set file to binary mode
print FH $bin;

undef $bin; #clear the $bin variable
seek FH, 0, 0; #rewind the file pointer
undef $/; #enable slurp mode to read entire file
$bin = <FH>; #slurp the file into $bin

my @newdata = unpack('N*',$bin);
print "data = @data\nnewdata = @newdata\n";

PS -- if you want to learn even more perl in a great forum come visit PerlMonks at http://perlmonks.org




yapp
User

Nov 20, 2001, 7:31 AM

Post #8 of 8 (2070 views)
Re: Can I call Win32 API's?? [In reply to] Can't Post


In Reply To
If the function uses a BOOL or bool, I've had good luck specifyng it as an integer (I).

Great!

Anyway, I've been using a lot of Win32 API's in my Visual Basic programs. I always needed to convert the declarations. Most of the types declared in C are 'synonyms' for the real type. A HWND, HDC, LPARAM, BOOL, WPARAM, DWORD are usually the same (4 bytes) integer.

The big difference is the kind of data you store in it. Most of the time, that doesn't matter.

Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives