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

I posted Re: opening the cd rom yesterday, but now I have a question myself. I copied the original code here, so it is easy to reference. It was a piece of code to eject CDROM:

use Win32::API; use strict; use warnings; my $f = Win32::API->new("winmm", "mciSendString", "PPNN", "N"); my $ret = ' ' x 127; my $return = $f->Call('set CDAudio door open', $ret, 127, 0);

I came from c background, so I tried to modify the API interface to c style, which makes much more sense to me:

use Win32::API; use strict; use warnings; my $f = Win32::API->new("winmm", "long mciSendString(char * a, char * +b, long c, long d)"); my $ret = ' ' x 127; my $return = $f->Call('set CDAudio door open', $ret, 127, 0);

This didn't work, and core dump. I tried couple of other variances, but none of them worked. Please help.

Update:

Changed the code to something like this:

use Win32::API; use strict; use warnings; my $f = Win32::API->new("winmm", 'long mciSendString(LPSTR a, LPSTR b, + long c, long d)'); my $ret = ' ' x 127; $f->Call('set CDAudio door open', $ret, 127, 0);

Now it gives this error:

Modification of a read-only value attempted at C:/Perl/site/lib/Win32/ +API/Type.p m line 195, <DATA> line 164.

Replies are listed 'Best First'.
Re: c style Win32::API interface
by Roger (Parson) on Oct 05, 2004 at 06:10 UTC
    There is a bug in the prototype definition. The quickest solution is to patch the Win32::API module by 'voiding' the custom prototype.

    ... my $f = Win32::API->new("winmm", "long mciSendString(LPSTR a, LPSTR b, + long c, long d)"); delete $f->{proto}; delete $f->{intypes}; ...

    This will force Win32::API to behave the same as
    Win32::API->new("winmm", "PPNN", "N");


    It is often useful to turn on the internal debugging of Win32::API to work out what is the prototype parser doing. Turn on debugging with $Win32::API::DEBUG = 1;. This shows that in your first prototype, the char * was interpreted as char. Win32::API was not doing a good job at parsing char *. (grin...)

Re: c style Win32::API interface
by dfaure (Chaplain) on Oct 05, 2004 at 05:53 UTC

    You just have a small bug in the prototype you use. The real one is there:

    MCIERROR mciSendString( LPCTSTR lpszCommand, LPTSTR lpszReturnString, UINT cchReturn, HANDLE hwndCallback );

    ____
    HTH, Dominique
    My two favorites:
    If the only tool you have is a hammer, you will see every problem as a nail. --Abraham Maslow
    Bien faire, et le faire savoir...