in reply to Using Window System Path Variable in Perl Script

As an alternative to the environment variable, there's a Win32 API call you can use: GetWindowsDirectory, and there's also its sibling GetSystemDirectory. You can call these by using the Win32::API module.
# Old Skool calls use Win32::API; my $getwindir = new Win32::API(kernel32 => GetWindowsDirectoryA => [qw +(P N)], "N") or die "Can't locate function: $!"; # ... or use 'PN' for the 3rd parameter, if Win32::API is recent enoug +h (version?) my $buffer = "\0" x 300; my $length = $getwindir->Call($buffer, length $buffer); print substr $buffer, 0, $length;
The following crashes for me, if I don't delete $func->{proto}... Surely that must be a bug? Otherwise, I must be misunderstanding something really badly...
# New Skool calls ($Win32::API::VERSION >= 0.40) use Win32::API; my $getwindir = new Win32::API(kernel32 => 'LONG GetWindowsDirectoryA( + LPSTR buffer, LONG maxlength )') or die "Can't locate function: $!"; # Do this, or bear the consequences: delete $getwindir->{proto}; my $buffer = "\0" x 200; my $length = $getwindir->Call($buffer, length $buffer); print substr $buffer, 0, $length;
And of course, you can replace "GetWindowsDirectory"/"GetWindowsDirectoryA" everywhere with "GetSystemDirectory"/"GetSystemDirectoryA", to get the Windows/System subdirectory

Replies are listed 'Best First'.
Re: Re: Using Window System Path Variable in Perl Script
by devgoddess (Acolyte) on Mar 15, 2004 at 02:22 UTC
    Hey, that's pretty cool, too. Thanks for turning me on to the Win32::API module. I really didn't know it existed. (Either that, or I probably just never noticed it before.) I'm coding exclusively in Windows 2K Server, and my scripts run exclusively on Windows machines, so this will be a good module to know and study.

    As for my purposes with this script, I went ahead and used the $ENV{windir} variable. It took one line of code to accomplish what I wanted. Tight is good. ;)

    Thanks a lot for the reply. I love the way I'm constantly picking up new info and techniques around here.

    Dev Goddess
    Developer / Analyst / Criminal Mastermind

    "Size doesn't matter. It's all about speed and performance."