Thanks Bill,

I tried to execute your program !

It fails to bring up file select dialog box !

Means at line

my $result = $GetOpenFileName->Call($struct);

The dialog box does not pop up and program fails
due to 0 return value means $result contans 0 !

Any idea what could be wrong ?

I think something in input parameter but I am not sure !

$GetOpenFileName contains valid hanlde after importing 
function from dll , still dialog box fails to pop up !

Please help !

WIth Best Regards,

V.S. Jangale

-----Original Message-----
From: $Bill Luebkert [mailto:[EMAIL PROTECTED]]
Sent: 2002-07-01 23:43
To: Jangale V-S
Subject: Re: FW: Common Dialog function


Jangale V-S wrote:
>>Dear All,
>>
>>Anybody has idea about using API function GetOpenFileNameA from
>>library comdlg32.dll using perl ??
>>
>>I want to use this function for selecting multiple files !!

This is only partially tested - feel free to modify (old API docs on back):

#!perl -w --

$| = 1;

use strict;
use Win32::API;
use vars qw(%A);

BEGIN { # get command line switches (normally in def module)
        for (my $ii = 0; $ii < @ARGV; ) {
                last if $ARGV[$ii] =~ /^--$/;
                if ($ARGV[$ii] =~ /^-{1,2}(.*)$/) {
                        splice @ARGV, $ii, 1;   # remove arg
                        my $tmp = $1;
                        if ($tmp =~ /^([\w]+)=\(([^\)]*)\)$/) {
                                $main::A{$1} = $2;
                        } elsif ($tmp =~ /^([\w]+)=(.*)$/) {
                                $main::A{$1} = $2;
                        } else {
                                $main::A{$1}++;
                        }
                } else { $ii++; }
        }
}

sub GetOpenFileName ($$);
sub dumpit ($;*);
my $debug = $A{d} || 0;

my $initdir = 'D:/docs';        # default start dir
my $filemask = '*.txt';         # default file mask

(my $prog = $0) =~ s|^.*[\\\/]||;
my $usage = <<EOD;

Usage: $prog [-d] [<start_dir> [<file_mask>]]
        -d              debug
        <start_dir>     initial directory for file selection (def: $initdir)
        <file_mask>     globbing mask for file selection (def: $filemask);

EOD

die $usage if $A{h} or $A{help};

# if command line args, get dir and mask

$initdir = $ARGV[0] if @ARGV;
$filemask = $ARGV[1] if @ARGV > 1;

# create popup for file selection

my $paths = GetOpenFileName ($initdir, $filemask);
if (not $paths) {
        print "Selection failed\n";
        exit;
}
if (@$paths > 1) {
        for (my $ii = 1; $ii < @$paths; $ii++) {
                print "Path: $paths->[0]\\$paths->[$ii]\n";
        }
} else {
        print "Path: $paths->[0]\n";
}

exit 0;

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-

sub GetOpenFileName ($$) {
        my $dir = shift;
        my $file = shift;
        
        use constant OFN_ALLOWMULTISELECT => 0x00000200;
        use constant OFN_CREATEPROMPT => 0x00002000;
        use constant OFN_EXPLORER => 0x00080000;
        use constant OFN_HIDEREADONLY => 0x00000004;
        use constant OFN_NODEREFERENCELINKS => 0x00100000;
        use constant OFN_NOREADONLYRETURN => 0x00008000;
        use constant OFN_OVERWRITEPROMPT=> 0x00000002;

$dir =~ s|\/|\\|g;      # reverse slashes
my $GetActiveWindow = new Win32::API('user32', 'GetActiveWindow',
   ['V'], 'N') or die "GetActiveWindow:",
   Win32::FormatMessage(Win32::GetLastError());

my $GetOpenFileName = new Win32::API('comdlg32', 'GetOpenFileName', ['P'],
'N')
   or die "GetOpenFileName Call: ", Win32::FormatMessage
(Win32::GetLastError);

my $lStructSize = 76;
my $hwnd = $GetActiveWindow->Call();
my $hInstance = 0;
# modify this
my $lpstrFilter = pack 'A*', "All Files\0*.*\0Text Files\0*.TXT;*.BAK\0" .
   "Perl Files\0*.pl;*.pm;*.cgi\0\0";
# modify this
my $lpstrCustomFilter = pack 'A*', "Fubar Files\0*.TMP\0\0";
my $nFilterIndex = 1;   # base-1 index into lpStrFilter for default filter
my $lpstrFile = pack 'A*', $file . "\0" x (8192 - length $file);
my $lpstrFileTitle = pack 'A*', "\0" x 8192;
my $lpstrInitialDir = pack 'A*', "$dir\0\0";
my $lpstrTitle = pack 'A*', "Select Your File\0\0";
my $Flags = OFN_CREATEPROMPT | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT |
   OFN_EXPLORER | OFN_NODEREFERENCELINKS | OFN_NOREADONLYRETURN |
   OFN_OVERWRITEPROMPT;
my $nFileOffset = 0;
my $nFileExtension = 0;
my $lpstrDefExt = pack 'A*', "\0\0";
my $lCustData = 0;
my $lpfnHook = 0;
my $lpTemplateName = pack 'A*', "\0\0";

my @openfilename = (
        $lStructSize,           # 'lStructSize'         # DWORD (I)
        $hwnd,                  # 'hwndOwner'           # HWND (I)
        $hInstance,             # 'hInstance'           # HINSTANCE (I)
        $lpstrFilter,           # 'lpstrFilter'         # LPCTSTR (P)
        $lpstrCustomFilter,     # 'lpstrCustomFilter' 
# LPTSTR (P)
        length $lpstrCustomFilter, # 'nMaxCustFilter'   # DWORD (I)
        $nFilterIndex,          # 'nFilterIndex'        # DWORD (I)
        $lpstrFile,             # 'lpstrFile'           # LPTSTR (P)
        length $lpstrFile,      # 'nMaxFile'            # DWORD (I)
        $lpstrFileTitle,        # 'lpstrFileTitle' 
# LPTSTR (P)
        length $lpstrFileTitle, # 'nMaxFileTitle'       # DWORD (I)
        $lpstrInitialDir,       # 'lpstrInitialDir' 
# LPCTSTR (P)
        $lpstrTitle,            # 'lpstrTitle'          # LPCTSTR (P)
        $Flags,                 # 'Flags'               # DWORD (I)
        $nFileOffset,           # 'nFileOffset'         # WORD (I)
        $nFileExtension,        # 'nFileExtension' 
# WORD (I)
        $lpstrDefExt,           # 'lpstrDefExt'         # LPCTSTR (P)
        $lCustData,             # 'lCustData'           # DWORD (I)
        $lpfnHook,              # 'lpfnHook'            # LPOFNHOOKPROC (P)
        $lpTemplateName,        # 'lpTemplateName' 
# LPCTSTR (P)
);
my $struct = pack "LLLppLLpLpLppLLLpLpp", @openfilename;

print "\@openfilename:\n" if $debug;
dumpit (join ('', @openfilename), \*STDOUT) if $debug;
print "\$struct:\n" if $debug;
dumpit ($struct, \*STDOUT) if $debug;

my $result = $GetOpenFileName->Call($struct);
print "RES: $result\n" if $debug;
return undef if not $result;

print "\@openfilename:\n" if $debug;
dumpit (join ('', @openfilename), \*STDOUT) if $debug;
print "\$struct:\n" if $debug;
dumpit ($struct, \*STDOUT) if $debug;

# my $title = unpack 'A*', $openfilename[9];
# my @titles = split "\0", $title;
# $title =~ s/\0.*$//;
my $path = unpack 'A*', $openfilename[7];
my @paths = split "\0", $path;
#$path =~ s/\0.*$//;

return \@paths;

}

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-

# dump a variable in hex/ASCII (def to STDERR)

sub dumpit ($;*) {      # &dumpit (<var>[, *FH]);
        my $buf = shift;
        my $fh = *STDERR;
        $fh = shift if @_;

for (my $ii = 0; $ii < length ($buf); $ii += 16) {

        printf $fh "%04x  ", $ii;
        my $len = length ($buf) - $ii;
        $len = 16 if $len > 16;

        my $buffer = substr ($buf, $ii, 16);
        print $fh map { sprintf "%02x ", $_; } unpack 'C8', $buffer;
        print $fh ' ';
        print $fh map { sprintf "%02x ", $_; } unpack 'C*',
          substr ($buffer, 8, 8) if length $buffer > 8;
        print $fh '  ' . '   ' x (16 - $len);

        for (my $jj = 0; $jj < $len; $jj++) {

                my $ch = substr $buffer, $jj, 1;
                if ((ord $ch) >= 32 && (ord $ch) < 0x7f) {
                        print $fh $ch;
                } else {
                        print $fh '.';
                }
        }
        print $fh "\n";
}

}

__END__

typedef struct tagOFN { // ofn
     DWORD         lStructSize;
     HWND          hwndOwner;
     HINSTANCE     hInstance;
     LPCTSTR       lpstrFilter;
     LPTSTR        lpstrCustomFilter;
     DWORD         nMaxCustFilter;
     DWORD         nFilterIndex;
     LPTSTR        lpstrFile;
     DWORD         nMaxFile;
     LPTSTR        lpstrFileTitle;
     DWORD         nMaxFileTitle;
     LPCTSTR       lpstrInitialDir;
     LPCTSTR       lpstrTitle;
     DWORD         Flags;
     WORD          nFileOffset;
     WORD          nFileExtension;
     LPCTSTR       lpstrDefExt;
     DWORD         lCustData;
     LPOFNHOOKPROC lpfnHook;
     LPCTSTR       lpTemplateName;
} OPENFILENAME;

#define OFN_SHAREWARN            0
#define OFN_SHARENOWARN          1
#define OFN_SHAREFALLTHROUGH     2

#define OFN_ALLOWMULTISELECT         0x00000200
#define OFN_CREATEPROMPT             0x00002000
#define OFN_ENABLEHOOK               0x00000020
#define OFN_ENABLETEMPLATE           0x00000040
#define OFN_ENABLETEMPLATEHANDLE     0x00000080
#define OFN_EXPLORER                 0x00080000
#define OFN_EXTENSIONDIFFERENT       0x00000400
#define OFN_FILEMUSTEXIST            0x00001000
#define OFN_HIDEREADONLY             0x00000004
#define OFN_LONGNAMES                0x00200000
#define OFN_NOCHANGEDIR              0x00000008
#define OFN_NODEREFERENCELINKS       0x00100000
#define OFN_NOLONGNAMES              0x00040000
#define OFN_NONETWORKBUTTON          0x00020000
#define OFN_NOREADONLYRETURN         0x00008000
#define OFN_NOTESTFILECREATE         0x00010000
#define OFN_NOVALIDATE               0x00000100
#define OFN_OVERWRITEPROMPT          0x00000002
#define OFN_PATHMUSTEXIST            0x00000800
#define OFN_READONLY                 0x00000001
#define OFN_SHAREAWARE               0x00004000
#define OFN_SHOWHELP                 0x00000010

The OPENFILENAME structure contains information the operating system uses to
initialize the system-defined Open or Save As dialog box. After the user
closes
the dialog box, the system returns information about the user's selection in
this structure.

Members:
        lStructSize
                Specifies the length, in bytes, of the structure.

        hwndOwner
                Identifies the window that owns the dialog box. This member
can
                be any valid window handle, or it can be NULL if the dialog
box
                has no owner.

        hInstance
                Identifies a data block that contains a dialog box template
                specified by the lpTemplateName member (if the Flags member
is
                set to OFN_ENABLETEMPLATE).  Identifies the memory object
                containing a dialog box template (if Flags is set to
                OFN_ENABLETEMPLATEHANDLE). If neither OFN_ENABLETEMPLATE nor
                OFN_ENABLETEMPLATEHANDLE is set, this member is ignored.

        lpstrFilter
                Points to a buffer containing pairs of null-terminated
filter
                strings. The first string in each pair describes a filter
(for
                example, "Text Files"), and the second specifies the filter
                pattern (for example, "*.TXT"). Multiple filters can be
                specified for a single item by separating the filter pattern
                strings with a semicolon (for example, "*.TXT;*.DOC;*.BAK").
                The last string in the buffer must be terminated by two NULL
                characters. If this member is NULL, the dialog box will not
                display any filters. The filter strings are assumed to be in
                the proper order - the operating system does not change the
                order.

        lpstrCustomFilter
                Points to a buffer containing a pair of user-defined filter
                strings. The first string describes the filter, and the
second
                specifies the filter pattern (for example "WinWord,
*.docnn").
                The buffer is terminated by two NULL characters. The
operating
                system copies the strings to the buffer when the user closes
                the dialog box. The system uses the strings to initialize
the
                user-defined file filter the next time the dialog box is
                created. If this member is NULL, the dialog box lists but
does
                not save user-defined filter strings.

                Windows 95: If this member is NULL, the member is ignored.
                If this member is not NULL, the value of the nMaxCustFilter
                member is used to determine the length of the corresponding
                string.

        nMaxCustFilter
                Specifies the size, in bytes or characters, of the buffer
                identified by lpstrCustomFilter. This buffer should be at
least
                40 characters long. This member is ignored if
lpstrCustomFilter
                is NULL or points to a NULL string.

                Windows 95: The value of nMaxcustFilter is used whenever
                lpstrCustomFilter is defined.

        nFilterIndex
                Specifies an index into the buffer pointed to by
lpstrFilter.
                The system uses the index value to obtain a pair of strings
to
                use as the initial filter description and filter pattern for
                the dialog box. The first pair of strings has an index value
of
                1. When the user closes the dialog box, the system copies
the
                index of the selected filter strings into this location. If
                nFilterIndex is zero, the custom filter is used. If
                nFilterIndex is zero and lpstrCustomFilter is NULL, the
system
                uses the first filter in the buffer identified by
lpstrFilter.
                If all three members are zero or NULL, the system does not
use
                any filters and does not show any files in the file list
                control of the dialog box.

        lpstrFile
                Points to a buffer that contains a filename used to
initialize
                the File Name edit control. The first character of this
buffer
                must be NULL if initialization is not necessary. When the
                GetOpenFileName or GetSaveFileName function returns, this
                buffer contains the drive designator, path, filename, and
                extension of the selected file.
                If the buffer is too small, the dialog box procedure copies
the
                required size into this member.

        nMaxFile
                Windows NT: Specifies the size, in characters, of the buffer
                pointed to by lpstrFile. The GetOpenFileName and
                GetSaveFileName functions return FALSE if the buffer is too
                small to contain the file information. The buffer should be
at
                least 256 characters long. This member is ignored if
lpstrFile
                is NULL.

                Windows 95: Specifies the size in bytes or characters of the
                buffer pointed to by lpstrFile. The buffer should be at
least
                260 charcters long. this member is ignored if lpstrFile is
NULL.

        lpstrFileTitle
                Points to a buffer that receives the title of the selected
                file.  For Windows versions 3.0 and 3.1, this buffer
receives
                the filename and extension without path information. The
                application should use this string to display the file
title.
                If this member is NULL, the function does not copy the file
                title.

        nMaxFileTitle
                Specifies the maximum length of the string that can be
copied
                into the lpstrFileTitle buffer. This member is ignored if
                lpstrFileTitle is NULL.

        lpstrInitialDir
                Points to a string that specifies the initial file
directory. If this member is NULL, the system uses the current directory as
the initial directory.

        lpstrTitle
                Points to a string to be placed in the title bar of the
dialog box. If this member is NULL, the system uses the default title (that
is, "Save As" or "Open").

        Flags
                Specifies the dialog box creation flags. This member may be
a combination of the following flags:

                Flag    Meaning


                OFN_ALLOWMULTISELECT    

                        Specifies that the File Name list box allows
multiple selections. (If the dialog box is created by using a private
template, the definition of the File Name list box must contain the
LBS_EXTENDEDSEL value.)Specifies that the File Name list box allows multiple
selections. If the dialog box is created by using a private template, the
definition of the File Name list box must contain the LBS_EXTENDEDSEL
value.When this flag is set, the lpstrFile member points to a buffer
containing the path to the current directory and all filenames in the
selection. The first filename is separated from the path by a space. Each
subsequent filename is separated by one space from the preceding filename.
Note that for the new Explorer-style dialogs (dialogs that have the
OFN_EXPLORER flag set), a single null character, \0, separates the
filenames, and two null characters, \0\0, terminate the entire string.

                OFN_CREATEPROMPT        

                        Specifies that the dialog box function should ask
whether the user wants to create a file that does not currently exist. (This
flag automatically uses the OFN_PATHMUSTEXIST and OFN_FILEMUSTEXIST flags.)

                OFN_ENABLEHOOK  

                        Enables the hook function specified in the lpfnHook
member.

                OFN_ENABLETEMPLATE      

                        Causes the operating system to create the dialog box
by using the dialog box template identified by hInstance and lpTemplateName.

                OFN_ENABLETEMPLATEHANDLE        

                        Indicates that hInstance identifies a data block
that contains a preloaded dialog box template. The operating system ignores
lpTemplateName if this flag is specified.

                OFN_EXPLORER    

                        Creates an Open or Save As dialog box that uses
user-interface features similar to the Windows Explorer.

                OFN_EXTENSIONDIFFERENT  

                        Specifies that the user typed a filename extension
that differs from the extension specified by lpstrDefExt. The function does
not use this flag if lpstrDefExt is NULL.

                OFN_FILEMUSTEXIST       

                        Specifies that the user can type only names of
existing files in the File Name entry field. If this flag is specified and
the user enters an invalid name, the dialog box procedure displays a warning
in a message box. If this flag is specified, the OFN_PATHMUSTEXIST flag is
also used.

                OFN_HIDEREADONLY        

                        Hides the Read Only check box.

                OFN_LONGNAMES   

                        Causes the Open or Save As dialog box to display
long filenames. If this flag is not specified, the dialog box displays
filenames in 8.3 format. This value is ignored if OFN_EXPLORER is set.

                OFN_NOCHANGEDIR 

                        Restores the current directory to its original value
if the user changed the directory while searching for files.

                OFN_NODEREFERENCELINKS  

                        Directs the dialog box to return the path and
filename
                        of the selected shortcut (.LNK) file. If this value
is
                        not given, the dialog box returns the path and
filename
                        of the file referenced by the shortcut.

                OFN_NOLONGNAMES 

                        Specifies that long filenames are not displayed in
the
                        File Name list box. This value is ignored if
                        OFN_EXPLORER is set.

                OFN_NONETWORKBUTTON     

                        Hides and disables the Network button.

                OFN_NOREADONLYRETURN    

                        Specifies that the returned file does not have the
Read
                        Only check box checked and is not in a
write-protected
                        directory.

                OFN_NOTESTFILECREATE    

                        Specifies that the file is not created before the
                        dialog box is closed. This flag should be specified
if
                        the application saves the file on a create-nonmodify
                        network sharepoint. When an application specifies
this
                        flag, the library does not check for write
protection,
                        a full disk, an open drive door, or network
protection.
                        Applications using this flag must perform file
                        operations carefully, because a file cannot be
reopened
                        once it is closed.

                OFN_NOVALIDATE  

                        Specifies that the common dialog boxes allow invalid
                        characters in the returned filename. Typically, the
                        calling application uses a hook function that checks
                        the filename by using the FILEOKSTRING message. If
the
                        text box in the edit control is empty or contains
                        nothing but spaces, the lists of files and
directories
                        are updated. If the text box in the edit control
                        contains anything else, nFileOffset and
nFileExtension
                        are set to values generated by parsing the text. No
                        default extension is added to the text, nor is text
                        copied to the buffer specified by lpstrFileTitle.
                        If the value specified by nFileOffset is less than
                        zero, the filename is invalid. Otherwise, the
filename
                        is valid, and nFileExtension and nFileOffset can be
                        used as if the OFN_NOVALIDATE flag had not been
                        specified.

                OFN_OVERWRITEPROMPT     

                        Causes the Save As dialog box to generate a message
box
                        if the selected file already exists. The user must
                        confirm whether to overwrite the file.

                OFN_PATHMUSTEXIST       

                        Specifies that the user can type only valid paths
and
                        filenames. If this flag is used and the user types
an
                        invalid path and filename in the File Name entry
field,
                        the dialog box function displays a warning in a
message
                        box.

                OFN_READONLY    

                        Causes the Read Only check box to be checked
initially
                        when the dialog box is created. This flag indicates
the
                        state of the Read Only check box when the dialog box
is
                        closed.

                OFN_SHAREAWARE  

                        Specifies that if a call to the OpenFile function
fails
                        because of a network sharing violation, the error is
                        ignored and the dialog box returns the given
filename.
                        If this flag is not specified, the registered
message
                        for SHAREVISTRING is sent to the hook function with
a
                        pointer to a null-terminated string for the path and
                        filename in the lParam parameter.

                OFN_SHOWHELP    

                        Causes the dialog box to show the Help button. The
                        hwndOwner member must not be NULL if this option is
                        specified.
        nFileOffset
                Specifies a zero-based offset from the beginning of the path
to
                the filename in the string pointed to by lpstrFile. For
                example, if lpstrFile points to the following string,
                "c:\dir1\dir2\file.ext", this member contains the value 13.

        nFileExtension
                Specifies a zero-based offset from the beginning of the path
to
                the filename extension in the string pointed to by
lpstrFile.
                For example, if lpstrFile points to the following string,
                "c:\dir1\dir2\file.ext", this member contains the value 18.
If
                the user did not type an extension and lpstrDefExt is NULL,
                this member specifies an offset to the terminating null
                character. If the user typed "." as the last character in
the
                filename, this member specifies 0.

        lpstrDefExt
                Points to a buffer that contains the default extension.
                GetOpenFileName and GetSaveFileName append this extension to
                the filename if the user fails to type an extension. This
                string can be any length, but only the first three
characters
                are appended. The string should not contain a period (.). If
                this member is NULL and the user fails to type an extension,
no
                extension is appended.

        lCustData
                Specifies application-defined data that the operating system
                passes to the hook function identified by lpfnHook. The
system
                passes the data in the lParam parameter of the WM_INITDIALOG
                message.

        lpfnHook
                Points to a hook function that processes messages intended
for
                the dialog box. An application must specify the
OFN_ENABLEHOOK
                flag in the Flags member to enable the function; otherwise,
the
                operating system ignores this structure member. The hook
                function should return FALSE to pass a message to the
standard
                dialog box procedure or TRUE to discard the message.
                If the hook function processes WM_CTLCOLOR* messages (such
as
                WM_CTLCOLORDLG), the function must return a valid brush
handle
                for painting the background of the dialog box or the
                corresponding control.

        lpTemplateName
                Points to a null-terminated string that names the dialog box
                template to be substituted for the standard dialog box
                template. An application can use the MAKEINTRESOURCE macro
for
                numbered dialog box resources. This member is only used if
                Flags specifies the OFN_ENABLETEMPLATE flag; otherwise, this
                member is ignored.

-- 
   ,-/-  __      _  _         $Bill Luebkert   ICQ=162126130
  (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic
http://www.todbe.com/
_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to