Droid41 has asked for the wisdom of the Perl Monks concerning the following question:

I've been perusing the documentation for Win32api::File and I'm not seeing how to open and read from directory handles.

I've tried opening a file handle (using createFile) and successfully read from it. I thought that, perhaps, a dirhandle could be created using createFile as well, but no luck. Pointing to a directory, rather than a file, just gets me an Accesss Denied error.

So, how does one open and read directories using Win32api::File?

Replies are listed 'Best First'.
Re: Win32api::File and Directories
by ikegami (Patriarch) on Jun 15, 2005 at 20:11 UTC
      Can't use the built in's because I'm trying end run around the fact that they're not Unicode aware. For the program I'm trying to write, I need to be able to move and copy files even if the filenames and directories have characters that readdir can't cope with.

        Maybe this will help

        #! perl -slw use strict; use Win32::API::Prototype; ApiLink( 'Kernel32', q[ HANDLE FindFirstFile( LPCTSTR lpFileName, LPWIN32_FIND_DATA lpF +FData )] ) or die $^E; ApiLink( 'Kernel32', q[ BOOL FindNextFile( HANDLE hFindFile, LPWIN32_FIND_DATA lpFFData + ) )] ) or die $^E; ApiLink( 'Kernel32', q[ HANDLE FindClose( HANDLE hFindDile )] ) or die $^E; =pod typedef struct _WIN32_FIND_DATA { DWORD dwFileAttributes; # 4 FILETIME ftCreationTime; # 8 FILETIME ftLastAccessTime; # 8 FILETIME ftLastWriteTime; # 8 DWORD nFileSizeHigh; # 4 DWORD nFileSizeLow; # 4 DWORD dwReserved0; # 4 DWORD dwReserved1; # 4 TCHAR cFileName[MAX_PATH]; # 260 TCHAR cAlternateFileName[14]; # 14 } WIN32_FIND_DATA, # 328 *PWIN32_FIND_DATA; =cut my $FFData = chr(0) x 328; my $hFF = FindFirstFile( '.\*', $FFData ) or die $^E; do { print unpack 'x44Z260', $FFData; } while FindNextFile( $hFF, $FFData ); FindClose( $hFF );

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

        Well, you could create a interface to FindFirstFileW or FindFirstFileExW, FindNextFileW and FindClose using Win32::API.

        Update: It seems that BrowserUK has already implemented that to which I was refering, except he implemented the non-unicode version of the calls.

Re: Win32api::File and Directories
by BrowserUk (Patriarch) on Jun 15, 2005 at 20:09 UTC
    So, how does one open and read directories using Win32api::File?

    You don't. See perl's built-in functions opendir,readddir,closedir


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.