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

I have functions inside "some.dll" with the following 'C' specifications:

int ace_init(void);

int ace_term(void);

int ace_init_addr(one parameter); ADDR_HANDLE* ah; Output: address handle

int ace_set_file(three parameters);
ADDR_HANDLE ah; Input: address handle
int file_ID; Input: file identifier
char* pathname; Input: pathname



So far I have:
use Win32::API; my $debug = 1; ## Needed Files my $ADDRLN = 'c:\pw\acelib\addrln.dct'; my $LASTLN = 'c:\pw\acelib\lastln.dct'; my $PWCAS = 'c:\pw\acelib\pwcas.dct'; my $FIRMLN = 'c:\pw\acelib\firmln.dct'; my $CITY07 = 'h:\city07.dir'; my $REVZIP4 = 'h:\revzip4.dir'; my $ZCF07 = 'h:\zcf07.dir'; my $ZIP4US = 'h:\zip4us.dir'; my $ACE3553 = 'c:\pw\acelib\ace3553.frm'; my $ACENDI = 'c:\pw\acelib\acendi.frm'; my $ACELIBKEY = 'c:\pw\acelib\acelib.key'; my $ace_dll = 'c:\pw\acelib\ace32.dll'; ## ACE32 Functions my $ace_init = new Win32::API("$ace_dll", "ace_init", [V], I); my $ace_term = new Win32::API("$ace_dll", "ace_term", [V], I); my $ace_init_addr = new Win32::API("$ace_dll", "ace_init_addr", [P], I +); my $ace_term_addr = new Win32::API("$ace_dll", "ace_term_addr", [P], I +); my $ace_open = new Win32::API("$ace_dll", "ace_open", [P], I); my $ace_close = new Win32::API("$ace_dll", "ace_close", [P], I); my $ace_set_file = new Win32::API("$ace_dll", "ace_set_file", [P, I, P +], I); my $ah = pack('L', 0); my $addrln_path = pack('Z', "$ADDRLN"); my $res; ## ACE Initialization debug("ACE Initialization"); if (($res = $ace_init->Call()) != 0) { die("Unable to initialize ACE: Result -> $res\n"); } ## ACE Address Handle Initialization debug("ACE Address Handle Initialization"); if (($res = $ace_init_addr->Call(\$ah)) != 0) { $ace_term->Call(); die("Unable to initialize Address Handle: Result -> $res\n"); } ## Set addrln.dct File debug("Set addrln.dct File"); if (($res = $ace_set_file->Call($ah, 5, $addrln_path)) != 0) { $ace_term_addr->Call($ah); $ace_term->Call(); die("Unable to set $ADDRLN: Result -> $res\n"); } print $ace_term_addr->Call($ah) . "\n"; print $ace_term->Call() . "\n"; exit(0); ## ## Subroutines and Functions ## ## Debug sub debug { if ($debug) { my ($msg) = @_; print "$msg\n"; } }

Am I using this right (I keep getting an error on $ace_set_file), if not what is wrong?

Thanks for any help

Replies are listed 'Best First'.
(tye)Re: Win32::API help
by tye (Sage) on Dec 10, 2001 at 23:30 UTC

    You can't use \$ah with Win32::API.

    The "P" type for parameters means that the address of the string in the variable is passed in. If you pass in \$ah for that argument, then you'll probably be passing in the address of a string like "SCALAR(0x1c7d220)".

    Just pass in $ah and the C code will get a pointer to the string (which you correctly initialized to a 4-byte buffer) and can stuff the pointer into that buffer. Now, for ace_set_file, you want to pass the C pointer that got stuff into that string. This means that you can't use "P" for the first argument to ace_set_file, because it can only be used to pass the pointer to a Perl string and that pointer does not point to a Perl string.

    Instead, use the argument type of "N" (for ace_set_file's first argument) and do:     $ah= unpack("L",$ah); after the call to ace_init_addr.

    Finally, I suggest you use strict which means you need to change code like [P, I, P] to something more like ["P","I","P"] or [qw(P I P)], despite the bad examples in the Win32::API documentation.

            - tye (but my friends call me "Tye")