Hi monks,

I am wondering if there is any way to write data to a C structure in memory, knowing the address of the structure, using only Perl functions, modules, etc.

I am creating a GUI application using Win32::GUI and using a Listbox control which is to be owner-drawn. In order to get this to work, I need to handle the WM_MEASUREITEM message, among others. Since Win32::GUI doesn't have an event for this message, I need to create a hook. The message receives a pointer to a structure, which needs to be filled out with the desired item height.

I know how to use pack() and unpack() to access the data in the structure but I am unsure how to store the information back in the same structure. I have tried packing the data back into the argument passed to the sub (using @_) but this didn't seem to work. I also tried it with Inline::C, but the module seems to have a problem with directories with spaces in the names. The reason I would like to be able to do this with only Perl functions or core modules is that I will most likely distribute this app as a sample with a module I am creating and would like to keep the dependencies to a minimum.

Does anyone have any suggestions about getting this to work?

Here is my hook code:

$winMain->Hook( WM_MEASUREITEM, sub { my($self, $wParam, $lParam, $type, $msgcode) = @_; return 1 unless $type == 0; return 1 unless $msgcode == WM_MEASUREITEM; my %measureitem; # Unpack data from structure @measureitem{qw(CtrlType CtrlID itemID itemWidth itemHeight it +emData)} = unpack 'IIIIIL', unpack 'P24', pack 'L', $lParam; # Set the height of the items $measureitem{'itemHeight'} = 30; # Pack data back into structure my $struct = pack 'IIIIIL', @measureitem{qw(CtrlType CtrlID itemID itemWidth itemHeigh +t itemData)}; $lParam = unpack 'L', pack 'P24', $struct; # or #$_[2] = unpack 'L', pack 'P24', $struct; return 1; }, );

Here is a sample of how it would be done in C:

case WM_MEASUREITEM: lpmis = (LPMEASUREITEMSTRUCT) lParam; // Set the height of the list box items. lpmis->itemHeight = 30; return true;

Thanks in advance for any help.


In reply to Manipulating C Structures using only Perl by kejohm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.