summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/Win32/API/Type.pm
blob: 48f0ce1a32f23499af03b03623fcba72c32f86d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package Win32::API::Type;

# See the bottom of this file for the POD documentation.  Search for the
# string '=head'.

#######################################################################
#
# Win32::API::Type - Perl Win32 API type definitions
# 
# Author: Aldo Calpini <dada@perl.it>
# Maintainer: Cosimo Streppone <cosimo@cpan.org>
#
# $Id: Type.pm 458 2009-01-17 17:27:43Z cosimo.streppone $
#
#######################################################################

$VERSION = '0.59';

use Carp;

require Exporter;       # to export the constants to the main:: space
require DynaLoader;     # to dynuhlode the module.
@ISA = qw( Exporter DynaLoader );

use vars qw( %Known %PackSize %Modifier %Pointer );

sub DEBUG { 
    if ($Win32::API::DEBUG) { 
        printf @_ if @_ or return 1; 
    } else {
        return 0;
    }
}

%Known      = ();
%PackSize   = ();
%Modifier   = ();
%Pointer    = ();

# Initialize data structures at startup.
# Aldo wants to keep the <DATA> approach.
#
my $section = 'nothing';
foreach (<DATA>) {
    next if /^\s*#/ or /^\s*$/;
    chomp;
    if( /\[(.+)\]/) {
        $section = $1;
        next;
    }
    if($section eq 'TYPE') {
        my($name, $packing) = split(/\s+/);
        # DEBUG "(PM)Type::INIT: Known('$name') => '$packing'\n";
        $Known{$name} = $packing;
    } elsif($section eq 'PACKSIZE') {
        my($packing, $size) = split(/\s+/);
        # DEBUG "(PM)Type::INIT: PackSize('$packing') => '$size'\n";
        $PackSize{$packing} = $size;
    } elsif($section eq 'MODIFIER') {
        my($modifier, $mapto) = split(/\s+/, $_, 2);
        my %maps = ();
        foreach my $item (split(/\s+/, $mapto)) {
            my($k, $v) = split(/=/, $item);
            $maps{$k} = $v;
        }           
        # DEBUG "(PM)Type::INIT: Modifier('$modifier') => '%maps'\n";
        $Modifier{$modifier} = { %maps };
    } elsif($section eq 'POINTER') {
        my($pointer, $pointto) = split(/\s+/);
        # DEBUG "(PM)Type::INIT: Pointer('$pointer') => '$pointto'\n";
        $Pointer{$pointer} = $pointto;
    }
}
close(DATA);

sub new {
    my $class = shift;
    my($type) = @_; 
    my $packing = packing($type);
    my $size = sizeof($type);
    my $self = {
        type => $type,
        packing => $packing,
        size => $size,
    };
    return bless $self;
}

sub typedef {
    my $class = shift;
    my($name, $type) = @_;  
    my $packing = packing($type, $name);
    DEBUG "(PM)Type::typedef: packing='$packing'\n";
    my $size = sizeof($type);
    $Known{$name} = $packing;
    return 1;
}


sub is_known {
    my $self = shift;
    my $type = shift;
    $type = $self unless defined $type;
    if(ref($type) =~ /Win32::API::Type/) {
        return 1;
    } else {
        return defined packing($type);
    }
}

sub sizeof {
    my $self = shift;
    my $type = shift;
    $type = $self unless defined $type;
    if(ref($type) =~ /Win32::API::Type/) {
        return $self->{size};
    } else {
        my $packing = packing($type);
        if($packing =~ /(\w)\*(\d+)/) {
            return $PackSize{ $1 } * $2;
        } else {
            return $PackSize{ $packing };
        }
    }   
}

sub packing {
    # DEBUG "(PM)Type::packing: called by ". join("::", (caller(1))[0,3]). "\n";  
    my $self = shift;
    my $is_pointer = 0;
    if(ref($self) =~ /Win32::API::Type/) {
        # DEBUG "(PM)Type::packing: got an object\n"; 
        return $self->{packing};
    }
    my $type = ($self eq 'Win32::API::Type') ? shift : $self;
    my $name = shift;
    
    # DEBUG "(PM)Type::packing: got '$type', '$name'\n";  
    my($modifier, $size, $packing);
    if(exists $Pointer{$type}) {        
        # DEBUG "(PM)Type::packing: got '$type', is really '$Pointer{$type}'\n";
        $type = $Pointer{$type};
        $is_pointer = 1;
    } elsif($type =~ /(\w+)\s+(\w+)/) {
        $modifier = $1;
        $type = $2;
        # DEBUG "(PM)packing: got modifier '$modifier', type '$type'\n";
    }
    
    $type =~ s/\*$//;
    
    if(exists $Known{$type}) {
        if(defined $name and $name =~ s/\[(.*)\]$//) {
            $size = $1;
            $packing = $Known{$type}[0]."*".$size;  
            # DEBUG "(PM)Type::packing: composite packing: '$packing' '$size'\n";
        } else {
            $packing = $Known{$type};
            if($is_pointer and $packing eq 'c') {
               $packing = "p";
            }
            # DEBUG "(PM)Type::packing: simple packing: '$packing'\n";
        }
        if(defined $modifier and exists $Modifier{$modifier}->{$type}) {
            # DEBUG "(PM)Type::packing: applying modifier '$modifier' -> '$Modifier{$modifier}->{$type}'\n";
            $packing = $Modifier{$modifier}->{$type};
        }
        return $packing;
    } else {
        # DEBUG "(PM)Type::packing: NOT FOUND\n";
        return undef;
    }
}   


sub is_pointer {
    my $self = shift;
    my $type = shift;
    $type = $self unless defined $type;
    if(ref($type) =~ /Win32::API::Type/) {
        return 1;
    } else {    
        if($type =~ /\*$/) {
            return 1;
        } else {
            return exists $Pointer{$type};
        }
    }
}

sub Pack {
    my ($type, $arg) = @_;

    my $pack_type = packing($type);

    if($pack_type eq 'p') {
        $pack_type = 'Z*';
    }

    $arg = pack($pack_type, $arg);

    return $arg;
}

sub Unpack {
    my ($type, $arg) = @_;

    my $pack_type = packing($type);

    if($pack_type eq 'p') {       
        DEBUG "(PM)Type::Unpack: got packing 'p': is a pointer\n";
        $pack_type = 'Z*';
    }

    DEBUG "(PM)Type::Unpack: unpacking '$pack_type' '$arg'\n"; 
    $arg = unpack($pack_type, $arg);
    DEBUG "(PM)Type::Unpack: returning '" . ($arg || '') . "'\n";  
    return $arg;
}

1;

#######################################################################
# DOCUMENTATION
#

=head1 NAME

Win32::API::Type - C type support package for Win32::API

=head1 SYNOPSIS

  use Win32::API;
  
  Win32::API::Type->typedef( 'my_number', 'LONG' );


=head1 ABSTRACT

This module is a support package for Win32::API that implements
C types for the import with prototype functionality.

See L<Win32::API> for more info about its usage.

=head1 DESCRIPTION

This module is automatically imported by Win32::API, so you don't 
need to 'use' it explicitly. These are the methods of this package:

=over 4

=item C<typedef NAME, TYPE>

This method defines a new type named C<NAME>. This actually just 
creates an alias for the already-defined type C<TYPE>, which you
can use as a parameter in a Win32::API call.

=item C<sizeof TYPE>

This returns the size, in bytes, of C<TYPE>. Acts just like
the C function of the same name. 

=item C<is_known TYPE>

Returns true if C<TYPE> is known by Win32::API::Type, false
otherwise.

=back

=head2 SUPPORTED TYPES

This module should recognize all the types defined in the
Win32 Platform SDK header files. 
Please see the source for this module, in the C<__DATA__> section,
for the full list.

=head1 AUTHOR

Aldo Calpini ( I<dada@perl.it> ).

=head1 MAINTAINER

Cosimo Streppone ( I<cosimo@cpan.org> ).

=cut


__DATA__

[TYPE]
ATOM					s
BOOL					L
BOOLEAN					c
BYTE					C
CHAR					c
COLORREF				L
DWORD                   L
DWORD32                 L
DWORD64                 Q
FLOAT                   f
HACCEL                  L
HANDLE                  L
HBITMAP                 L
HBRUSH                  L
HCOLORSPACE             L
HCONV                   L
HCONVLIST               L
HCURSOR                 L
HDC                     L
HDDEDATA                L
HDESK                   L
HDROP                   L
HDWP                    L
HENHMETAFILE            L
HFILE                   L
HFONT                   L
HGDIOBJ                 L
HGLOBAL                 L
HHOOK                   L
HICON                   L
HIMC                    L
HINSTANCE               L
HKEY                    L
HKL                     L
HLOCAL                  L
HMENU                   L
HMETAFILE               L
HMODULE                 L
HPALETTE                L
HPEN                    L
HRGN                    L
HRSRC                   L
HSZ                     L
HWINSTA                 L
HWND                    L
INT                     i
INT32                   i
INT64                   q
LANGID                  s
LCID                    L
LCSCSTYPE               L
LCSGAMUTMATCH           L
LCTYPE                  L
LONG                    l
LONG32                  l
LONG64                  q
LONGLONG                q
LPARAM                  L
LRESULT                 L
REGSAM                  L
SC_HANDLE               L
SC_LOCK                 L
SERVICE_STATUS_HANDLE   L
SHORT                   s
SIZE_T                  L
SSIZE_T                 L
TBYTE                   c
TCHAR                   C
UCHAR                   C
UINT                    I
UINT_PTR                L
UINT32                  I
UINT64                  Q
ULONG                   L
ULONG32                 L
ULONG64                 Q
ULONGLONG               Q
USHORT                  S
WCHAR                   S
WORD                    S
WPARAM                  L
VOID                    c

int                     i
long                    l
float                   f
double                  d
char                    c

#CRITICAL_SECTION   24 -- a structure
#LUID                   ?   8 -- a structure
#VOID   0
#CONST  4
#FILE_SEGMENT_ELEMENT   8 -- a structure

[PACKSIZE]
c   1
C   1
d   8
f   4
i   4
I   4
l   4
L   4
q   8
Q   8
s   2
S   2
p   4

[MODIFIER]
unsigned    int=I long=L short=S char=C

[POINTER]
INT_PTR                 INT
LPBOOL                  BOOL
LPBYTE                  BYTE
LPCOLORREF              COLORREF
LPCSTR                  CHAR
#LPCTSTR                    CHAR or WCHAR
LPCTSTR                 CHAR
LPCVOID                 any
LPCWSTR                 WCHAR
LPDOUBLE                double
LPDWORD                 DWORD
LPHANDLE                HANDLE
LPINT                   INT
LPLONG                  LONG
LPSTR                   CHAR
#LPTSTR                 CHAR or WCHAR
LPTSTR                  CHAR
LPVOID                  VOID
LPWORD                  WORD
LPWSTR                  WCHAR

PBOOL                   BOOL
PBOOLEAN                BOOL
PBYTE                   BYTE
PCHAR                   CHAR
PCSTR                   CSTR
PCWCH                   CWCH
PCWSTR                  CWSTR
PDWORD                  DWORD
PFLOAT                  FLOAT
PHANDLE                 HANDLE
PHKEY                   HKEY
PINT                    INT
PLCID                   LCID
PLONG                   LONG
PSHORT                  SHORT
PSTR                    CHAR
#PTBYTE                 TBYTE --
#PTCHAR                 TCHAR --
#PTSTR                  CHAR or WCHAR
PTSTR                   CHAR
PUCHAR                  UCHAR
PUINT                   UINT
PULONG                  ULONG
PUSHORT                 USHORT
PVOID                   VOID
PWCHAR                  WCHAR
PWORD                   WORD
PWSTR                   WCHAR