NAME

Win32::API::Struct - C struct support package for Win32::API

SYNOPSIS

use Win32::API;

Win32::API::Struct->typedef( 'POINT', qw(
  LONG x; 
  LONG y; 
));

my $Point = Win32::API::Struct->new( 'POINT' ); 
$Point->{x} = 1024;
$Point->{y} = 768;

#### alternatively

tie %Point, 'Win32::API::Struct', 'POINT';
$Point{x} = 1024;
$Point{y} = 768;

ABSTRACT

This module enables you to define C structs for use with Win32::API.

See Win32::API for more info about its usage.

DESCRIPTION

This module is automatically imported by Win32::API, so you don't need to 'use' it explicitly. The main methods are typedef and new, which are documented below.

typedef NAME, TYPE, MEMBER, TYPE, MEMBER, ...

This method defines a structure named NAME. The definition consists of types and member names, just like in C. In fact, most of the times you can cut the C definition for a structure and paste it verbatim to your script, enclosing it in a qw() block. The function takes care of removing the semicolon after the member name.

The synopsis example could be written like this:

Win32::API::Struct->typedef('POINT', 'LONG', 'x', 'LONG', 'y');

But it could also be written like this (note the indirect object syntax), which is pretty cool:

typedef Win32::API::Struct POINT => qw{
  LONG x; 
  LONG y; 
};

Also note that typedef automatically defines an 'LPNAME' type, which holds a pointer to your structure. In the example above, 'LPPOINT' is also defined and can be used in a call to a Win32::API (in fact, this is what you're really going to use when doing API calls).

new NAME

This creates a structure (a Win32::API::Struct object) of the type NAME (it must have been defined with typedef). In Perl, when you create a structure, all the members are undefined. But when you use that structure in C (eg. a Win32::API call), you can safely assume that they will be treated as zero (or NULL).

sizeof

This returns the size, in bytes, of the structure. Acts just like the C function of the same name. It is particularly useful for structures that need a member to be initialized to the structure's own size.

align [SIZE]

Sets or returns the structure alignment (eg. how the structure is stored in memory). This is a very advanced option, and you normally don't need to mess with it. All structures in the Win32 Platform SDK should work without it. But if you define your own structure, you may need to give it an explicit alignment. In most cases, passing a SIZE of 'auto' should keep the world happy.

THE tie INTERFACE

Instead of creating an object with the new method, you can tie a hash, which will hold the desired structure, using the tie builtin function:

tie %structure, Win32::API::Struct => 'NAME';

The differences between the tied and non-tied approaches are:

  • with tied structures, you can access members directly as hash lookups, eg.

    # tied              # non-tied
    $Point{x}    vs.    $Point->{x}
  • with tied structures, when you try to fetch or store a member that is not part of the structure, it will result in a warning, eg.

    print $Point{z};
    # this will warn: 'z' is not a member of Win32::API::Struct POINT
  • when you pass a tied structure as a Win32::API parameter, remember to backslash it, eg.

    # tied                            # non-tied
    GetCursorPos( \%Point )    vs.    GetCursorPos( $Point )

AUTHOR

Aldo Calpini ( dada@perl.it ).