Click here to get back home

Problem with Win32::API and pointers

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Problem with Win32::API and pointers jrgoody 05-12-2006
Get Chitika Premium
Posted by jrgoody on May 12, 2006, 10:02 am
Please log in for more thread options


Hi,

I'm working on a project and trying to use the Win32::API to call a
DLL from Perl, but I have some problems. I'm using the last version
of the API (0.41).

I'll describe my problem. I hope someone can help me. The DLL I've
been trying to call from Perl is the FTD2XX.dll from FTDI, a Library to
communicate with a chip connected to the computer via USB. The thing is
really simple, this is my code:

#CODE
STARTS******************************************************************************
use Win32::API;

Win32::API::Type->typedef( 'FT_HANDLE', 'PVOID' );
Win32::API::Type->typedef( 'FT_STATUS', 'ULONG' );

#FT_Open
Win32::API->Import( 'FTD2XX', 'FT_STATUS FT_Open(int
deviceNumber,FT_HANDLE *pHandle)' );

#FT_Close
Win32::API->Import( 'FTD2XX', 'FT_STATUS FT_Close(FT_HANDLE ftHandle)'
);


#Open port
FT_Open(0,$ft_handle);

#Close port
$ft_status = FT_Close($$ft_handle);
#CODE
ENDS***********************************************************************************

This is how the functions and variables are defined in the .h file:

...
typedef PVOID FT_HANDLE;
typedef ULONG FT_STATUS;
...

FTD2XX_API
FT_STATUS WINAPI FT_Open(
        int deviceNumber,
        FT_HANDLE *pHandle
        );

...
FTD2XX_API
FT_STATUS WINAPI FT_Close(
        FT_HANDLE ftHandle
        );
...


The function FT_Open should return FT_STATUS (this works) but also a
pointer *pHandle to the Handle, which is to be used for the forthcoming
operations to the port, like in the function FT_Close.

But when I run the program, this is what I receive:

"Perl Command Line Interpreter has encountered a problem and needs to
close. We are sorry for the inconvenience."

The problem is on the FT_Close function. If I comment it out, the
program runs OK (FT_Open returns Status OK), so I'm pretty sure the
problem has something to do with the handle. As I said, FT_Open should
give a pointer to the handle (2nd argument) and the handle itself is to
be used in FT_Close (only argument).

I thing it should work with the API, because it is pretty much the same
as in one of the examples in the API Documentation. But I can't get
it to work. Someone sees an error or something missing in my code?
Please tell me if you need more info from me or if you have any
suggestions on how I can fix this. This is really important for me. I
thank you very much in advance.


Posted by Thomas Kratz on May 12, 2006, 11:25 am
Please log in for more thread options


jrgoody@googlemail.com wrote:
> Hi,
>
> I'm working on a project and trying to use the Win32::API to call a
> DLL from Perl, but I have some problems. I'm using the last version
> of the API (0.41).
>
> I'll describe my problem. I hope someone can help me. The DLL I've
> been trying to call from Perl is the FTD2XX.dll from FTDI, a Library to
> communicate with a chip connected to the computer via USB. The thing is
> really simple, this is my code:
>
> #CODE
> STARTS******************************************************************************
> use Win32::API;
>
> Win32::API::Type->typedef( 'FT_HANDLE', 'PVOID' );
> Win32::API::Type->typedef( 'FT_STATUS', 'ULONG' );
>
> #FT_Open
> Win32::API->Import( 'FTD2XX', 'FT_STATUS FT_Open(int
> deviceNumber,FT_HANDLE *pHandle)' );
>
> #FT_Close
> Win32::API->Import( 'FTD2XX', 'FT_STATUS FT_Close(FT_HANDLE ftHandle)'
> );
>
>
> #Open port
> FT_Open(0,$ft_handle);
>
> #Close port
> $ft_status = FT_Close($$ft_handle);

This looks wrong. You are treating $ft_handle like a reference to a perl
scalar. But it contains the address of a void pointer.

I think you have to dereference the pointer in $ft_handle, but I have no
idea how to accomplish this with Win32::API. I googled for it, but nothing
helpful came up.

Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*3),,mg,@_=mapsplit;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-

Posted by harryfmudd [AT] comcast [DOT] on May 12, 2006, 1:53 pm
Please log in for more thread options


Thomas Kratz wrote:
> jrgoody@googlemail.com wrote:
>
>> Hi,
>>
>> I'm working on a project and trying to use the Win32::API to call a
>> DLL from Perl, but I have some problems. I'm using the last version
>> of the API (0.41).
>>
>> I'll describe my problem. I hope someone can help me. The DLL I've
>> been trying to call from Perl is the FTD2XX.dll from FTDI, a Library to
>> communicate with a chip connected to the computer via USB. The thing is
>> really simple, this is my code:
>>
>> #CODE
>> STARTS******************************************************************************
>>
>> use Win32::API;
>>
>> Win32::API::Type->typedef( 'FT_HANDLE', 'PVOID' );
>> Win32::API::Type->typedef( 'FT_STATUS', 'ULONG' );
>>
>> #FT_Open
>> Win32::API->Import( 'FTD2XX', 'FT_STATUS FT_Open(int
>> deviceNumber,FT_HANDLE *pHandle)' );
>>
>> #FT_Close
>> Win32::API->Import( 'FTD2XX', 'FT_STATUS FT_Close(FT_HANDLE ftHandle)'
>> );
>>
>>
>> #Open port
>> FT_Open(0,$ft_handle);
>>
>> #Close port
>> $ft_status = FT_Close($$ft_handle);
>
>
> This looks wrong. You are treating $ft_handle like a reference to a perl
> scalar. But it contains the address of a void pointer.
>
> I think you have to dereference the pointer in $ft_handle, but I have no
> idea how to accomplish this with Win32::API. I googled for it, but
> nothing helpful came up.
>
> Thomas
>

I agree with Thomas -- a Perl dereference is not what is needed here.

It has been a while since I messed with this sort of thing, but you
might want to try something like the following:

# >>>> begin code fragment

# Open port
$ft_handle = ' ';        # Preallocate 4 bytes.
FT_Open (0, $ft_handle);

# Dereference handle
$ft_handle = unpack ('L', $ft_handle);

# Close port
$ft_status = FT_Close ($ft_handle);

# >>>> end code fragment

This is based on some code I wrote a while back, which uses the older

$obj = Win32::API->new (dll, entrypoint, [argument ...], return)

syntax. I do not know that you need to switch to this syntax to use the
"preallocate before, unpack after" technique. I also do not know the
current state of Aldo Calpini's documentation, but I do recall
considerable code bashing to figure out what worked.

Tom Wyant

Posted by jrgoody on May 15, 2006, 6:11 am
Please log in for more thread options


Thank you all for your responses.

I've tried Tom's suggestions, but I still have the same problem.

I receive the same Windows message "Perl Command Line Interpreter has
encountered a problem and needs to close. We are sorry for the
inconvenience.", but this time here:

$ft_handle = unpack ('L',$ft_handle);

So it looks like it occurs the first time $ft_handle is used as
argument to a function.

I tried to print the following in different places (for debugging):
print ".$ft_handle."; with the following results:

At the beginning of the code: ..
After $ft_handle = ' '; :
. .
After $ft_status = $ft_open->Call(0,$ft_handle); : ..
After $ft_handle = unpack ('L',$ft_handle); : Nothing,

the program doesn't returns after calling unpack(). Any suggestions?
Need more info? Should I try something else?

Thanks a lot in advance.


Posted by Thomas Kratz on May 15, 2006, 10:30 am
Please log in for more thread options


jrgoody@googlemail.com wrote:
> Thank you all for your responses.
>
> I've tried Tom's suggestions, but I still have the same problem.
>
> I receive the same Windows message "Perl Command Line Interpreter has
> encountered a problem and needs to close. We are sorry for the
> inconvenience.", but this time here:
>
> $ft_handle = unpack ('L',$ft_handle);
>
> So it looks like it occurs the first time $ft_handle is used as
> argument to a function.
>
> I tried to print the following in different places (for debugging):
> print ".$ft_handle."; with the following results:
>
> At the beginning of the code: ..
> After $ft_handle = ' '; :
> . .
> After $ft_status = $ft_open->Call(0,$ft_handle); : ..
> After $ft_handle = unpack ('L',$ft_handle); : Nothing,
>
> the program doesn't returns after calling unpack(). Any suggestions?
> Need more info? Should I try something else?

Did you check that $ft_status reports a successful call to FT_OPEN?

Perhaps someone at perlmonks (http://perlmonks.org) or better still the
perl-win32-users mailing list at ActiveState has better ideas. If you are
very lucky, Aldo will still be reading the latter.

Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*3),,mg,@_=mapsplit;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-

Similar ThreadsPosted
Pointers for AxWindow August 23, 2007, 4:40 pm
Win32::API problem... May 25, 2005, 5:33 pm
Problem installing win32::OLE September 20, 2005, 12:52 pm
win32::ie::mechanize PROBLEM December 10, 2004, 7:06 am
problem in using win32::SerialPort September 7, 2007, 2:27 am
Problem Installing Win32::OLE under cygwin May 15, 2005, 9:45 pm
Problem in using Win32::Process::Create July 24, 2007, 6:25 am
Win32::Daemon - multiple processor problem August 10, 2004, 6:03 am
Problem printing a file uing Win32::Printer September 30, 2006, 3:36 pm
Dave Roth's site (Win32::AdminMisc, Win32::ODBC, etc.) not available. December 22, 2005, 8:46 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap