
RhetTbull
stranger
Nov 18, 2001, 10:43 AM
Post #5 of 8
(1309 views)
|
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.
|