http://www.perlmonks.org?node_id=1034870


in reply to Win32::API pointers / NULL

If you are using letter mode ("P"), put , 0, and it will be null. Do not write , "0",, that is a string. This is undocumented in the stable Win32::API on CPAN right now (the alpha version of Win32::API fixes the POD and finally says how to pass NULL for letter P parameter, it isn't undef due to backcompat, for C prototype mode, undef is NULL, which is normal IMO, I am not a fan of tye's , [], meaning NULL convention, but TIMTOWTDI), but it has been that way since forever I think http://github.com/bulk88/perl5-win32-api/blob/6466f18dc13e33cf4a07e54e5c76164444b33b1b/API.xs#L320 Next, F letter is a 32 bit floating pointer number, D is a 64 bit floating pointer number. I assume your float64 is a double in C. So that is wrong. Change the F to a D. You didn't say what bit CPU you are on (32/64), but if you are on 32 bits, and you replace a 8 byte param with 4 byte or vice versa, ALL parameters to the right of the replaced one become garbage. With your F, dataLayout in perl in the eyes of the function become part of the timeout, writeArray in Perl became dataLayout, sampsPerChanWritten in Perl became writeArray, reserved/your NULL became sampsPerChanWritten, and the C func, for reserved didn't see NULL but non-NULL garbage (probably the C instruction return address). On 64 bits all C stack params are extended to 8 bytes each and stay at 8, so what I described happens on 32 bits, wouldn't happen on 64. On 64, corrupt params keep their corruption to themselves and dont spill it over into neighboring params.

Next, in your pack stuff, why did you use N instead of V or L (L is the most appropriate pack letter to use).

my $sampsPerChanWritten= pack("N4",0,0,0,0);
Why? int32 *sampsPerChanWritten should be my $sampsPerChanWritten= pack('L', 0)"; not an array of Ls. It is just one according to the prototype (then again I dont have access to the formal docs for that function, that might a pointer to just one int32, or many int32s and the length of the array is some other param to the func). If I read the docs for that function http://zone.ni.com/reference/en-XX/help/370471W-01/daqmxcfunc/daqmxwritedigitalu32/, $writearray should just be pack('L', 0), just one L, since $numSamps is 1. BTW, as a performance increase, you could write $writearray = "\x00\x00\x00\x00"; or $writearray = "\x00" x 4; instead of $writearray = pack('L', 0);. They are equal. Test it with eq operator if you dont believe me.

Replies are listed 'Best First'.
Re^2: Win32::API pointers / NULL
by Anonymous Monk on May 23, 2013 at 06:48 UTC

    (L is the most appropriate pack letter to use).

    L< even :p

Re^2: Win32::API pointers / NULL
by Anonymous Monk on May 23, 2013 at 20:10 UTC

    Thank you so much for the detailed explanation of my errors!. Changing the "F" to "D" for the float64 parameter eliminated the crash.

    The clarifications provided by you and Tye on passing the NULL pointer was also very helpful. For some reason the function kept returning "Reserved Parameter must be NULL." until I re-installed Win32::API on my development machine.

    I think I have the code working properly and I'm now packaging it for deployment to my test hardware! Thanks again perlmonks!