Thank Brian. 
You are right. Packing was no need, and it was wrong.
The function call is working by directly using 0x80000000 as input.

My problem is now how to dereference a pointer returned by a Win32::API
call. I got an invalid "handle" from following code.

In the programmer guide for "ftd2xx.dll", the function "FT_open" and
"FT_close" are defined as
int FT_Open(int Device, PVOID *handle)
int FT_Close(PVOID handle)

FT_Open opens a USB device, and then returns a pointer to a variable
where "handle" will be stored. The "handle" will be used to access the
device.
In the following code sample, the FT_Open works successfully, and does
return a "handle", but I do not know how to manipulate the "handle" for
next FT_close call. I got "Invalid handle" error in this sample (the
"handle" is a "DWORD" type). How to get "$handle" to be the value
referred by the pointer instead of a random memory address in Perl?

Thanks for helps.

Eugene


#### code snippet #####
use strict;
use Win32::API;
use Carp;
use warnings;

my $FT_Open  = Win32::API->new("ftd2xx", "FT_Open", "NP", "I");
my $FT_Close = Win32::API->new("ftd2xx", "FT_Close", "P", "I");
my $DeviceNumber = 0; #only one device
my $pHandle = "\x00"x4; #Preallocate 4 bytes
$status = $FT_Open->Call($DeviceNumber,$pHandle);

my $handle = unpack ("L",$pHandle); #Dereference handle, how?

$status = $FT_Close->Call($handle);
print "\nFT_Close returned value: $status";


====================================================
RE: How to passing a hex number in Win32::API call
Brian Raven
Tue, 27 Oct 2009 03:17:30 -0700

I think that you problem is the 'pack'. Why do you think you need it?.
Just use 0x80000000 in your function call, or if you want to define it
as a constant, 'use constant FT_LIST_NUMBER_ONLY => 0x80000000;' is more
nearly equivalent to the #define you quote, and will add to your codes
readability. For example, you dould write it something like this:

use strict;
use warnings;
use Win32::API;

use constant FT_LIST_NUMBER_ONLY => 0x80000000;

my $function = new Win32::API("ftd2xx", "FT_ListDevices", "PPN", "N");
die "Can't import API FT_ListDevices: $!\n" unless defined $function;
my $DevBuffer = " " x 80;
my $status=$function->Call($DevBuffer, 0 ,FT_LIST_NUMBER_ONLY);
print "status: $status\n";
print "DeviceNumber: $DevBuffer";

HTH

-- 
Brian Raven 
This e-mail may contain confidential and/or privileged information. If
you are 
not the intended recipient or have received this e-mail in error, please
advise 
the sender immediately by reply e-mail and delete this message and any 
attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in
this 
e-mail is strictly forbidden.

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

-----Original Message-----
From: perl-win32-users-boun...@listserv.activestate.com
[mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of
Su, Yu (Eugene)
Sent: Monday, October 26, 2009 5:30 PM
To: perl-win32-users@listserv.activestate.com
Subject: RE: How to passing a hex number in Win32::API call

Hi,

I have a DLL from FTDI (ftd2xx.dll www.ftdichip.com) for a USB device. I
try to write a simple perl script to list a number of (FTD) USB devices
connected to a PC. My code below got an error on invalid parameter;

Argument "M-\0\0\0\0\" isn't numeric in subroutine entry at demo.pl line
# 

I guess I did not pass a hex data in the function call properly. Can
someone help me?

Thanks.

Eugene
 
######## code snippet #######

use strict;
use Win32::API;
use Carp;
use warnings;

# from FTD2xx programmer guide: 
# FT_ListDevices(PVOID numDevs,PVOID NULL,DWORD FT_LIST_NUMBER_ONLY)
# FT_LIST_NUMBER_ONLY is defined in FTD2xx.h as 
#define FT_LIST_NUMBER_ONLY = 0x80000000
#
my $function = new Win32::API("ftd2xx", "FT_ListDevices", "PPN", "N");
if (not defined $function) {
   die "Can't import API FT_ListDevices: $!\n";
}
my $DevBuffer = " " x 80;
my $FT_LIST_NUMBER_ONLY=pack('N', 0x80000000);
my $status=$function->Call($DevBuffer,0,$FT_LIST_NUMBER_ONLY);
print "status: $status\n";
print "DeviceNumber: $DevBuffer";
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to