-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRT77525-0.69.patch
3176 lines (2952 loc) · 103 KB
/
RT77525-0.69.patch
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff -ruNZ old/API.h new/API.h
--- old/API.h 2012-02-11 12:47:56.000000000 -0500
+++ new/API.h 2012-05-29 10:18:18.046875000 -0400
@@ -23,11 +23,15 @@
#define T_FLOAT 4
#define T_DOUBLE 5
#define T_CHAR 6
+#define T_SHORT 7
#define T_STRUCTURE 51
#define T_POINTERPOINTER 22
-#define T_CODE 101
+#define T_CODE 55
+
+#define T_FLAG_UNSIGNED (0x80)
+#define T_FLAG_NUMERIC (0x40)
typedef char *ApiPointer(void);
typedef long ApiNumber(void);
@@ -35,7 +39,9 @@
typedef double ApiDouble(void);
typedef void ApiVoid(void);
typedef int ApiInteger(void);
+typedef short ApiShort(void);
+//This is a packing padding nightmare, union or reorder, side effects unknown
typedef struct {
int t;
LPBYTE b;
@@ -54,3 +60,17 @@
typedef struct {
SV* object;
} APICALLBACK;
+
+#define STATIC_ASSERT(expr) ((void)sizeof(char[1 - 2*!!!(expr)]))
+
+//because of unknown alignment, put 2 wide nulls,
+//some permutation will be 1 wide null char
+#pragma pack(push)
+#pragma pack(push, 1)
+typedef struct {
+ wchar_t null1;
+ wchar_t null2;
+ LARGE_INTEGER counter;
+} SENTINAL_STRUCT;
+#pragma pack(pop)
+#pragma pack(pop)
diff -ruNZ old/API.pm new/API.pm
--- old/API.pm 2012-04-10 14:29:10.000000000 -0400
+++ new/API.pm 2012-05-29 02:50:22.765625000 -0400
@@ -18,10 +18,17 @@
require Exporter; # to export the constants to the main:: space
require DynaLoader; # to dynuhlode the module.
@ISA = qw( Exporter DynaLoader );
+@EXPORT_OK = qw( ReadMemory IsBadReadPtr IsBadStringPtr MoveMemory
+WriteMemory ); # symbols to export on request
-use vars qw( $DEBUG );
+use vars qw( $DEBUG $sentinal );
$DEBUG = 0;
+BEGIN {
+sub ERROR_NOACCESS () { 998 }
+}
+
+
sub DEBUG {
if ($Win32::API::DEBUG) {
printf @_ if @_ or return 1;
@@ -38,7 +45,7 @@
#######################################################################
# STATIC OBJECT PROPERTIES
#
-$VERSION = '0.68';
+$VERSION = '0.69';
#### some package-global hash to
#### keep track of the imported
@@ -56,83 +63,102 @@
# PUBLIC METHODS
#
sub new {
- my ($class, $dll, $proc, $in, $out, $callconvention) = @_;
- my $hdll;
+ my ($class, $dll, $hproc) = (shift, shift);
+ if(! defined $dll){
+ $hproc = shift;
+ }
+ my ($proc, $in, $out, $callconvention) = @_;
+ my ($hdll, $freedll) = (0, 0);
my $self = {};
+ if(! defined $hproc){
+ if ($^O eq 'cygwin' and $dll ne File::Basename::basename($dll)) {
- if ($^O eq 'cygwin' and $dll ne File::Basename::basename($dll)) {
-
- # need to convert $dll to win32 path
- # isn't there an API for this?
- my $newdll = `cygpath -w "$dll"`;
- chomp $newdll;
- DEBUG "(PM)new: converted '$dll' to\n '$newdll'\n";
- $dll = $newdll;
- }
+ # need to convert $dll to win32 path
+ # isn't there an API for this?
+ my $newdll = `cygpath -w "$dll"`;
+ chomp $newdll;
+ DEBUG "(PM)new: converted '$dll' to\n '$newdll'\n";
+ $dll = $newdll;
+ }
- #### avoid loading a library more than once
- if (exists($Libraries{$dll})) {
- DEBUG "Win32::API::new: Library '$dll' already loaded, handle=$Libraries{$dll}\n";
- $hdll = $Libraries{$dll};
- }
- else {
- DEBUG "Win32::API::new: Loading library '$dll'\n";
- $hdll = Win32::API::LoadLibrary($dll);
+ #### avoid loading a library more than once
+ if (exists($Libraries{$dll})) {
+ DEBUG "Win32::API::new: Library '$dll' already loaded, handle=$Libraries{$dll}\n";
+ $hdll = $Libraries{$dll};
+ }
+ else {
+ DEBUG "Win32::API::new: Loading library '$dll'\n";
+ $hdll = Win32::API::LoadLibrary($dll);
+ $freedll = 1;
+ # $Libraries{$dll} = $hdll;
+ }
-# $Libraries{$dll} = $hdll;
+ #### if the dll can't be loaded, set $! to Win32's GetLastError()
+ if (!$hdll) {
+ $! = Win32::GetLastError();
+ DEBUG "FAILED Loading library '$dll': $!\n";
+ return undef;
+ }
}
-
- #### if the dll can't be loaded, set $! to Win32's GetLastError()
- if (!$hdll) {
- $! = Win32::GetLastError();
- DEBUG "FAILED Loading library '$dll': $!\n";
- delete $Libraries{$dll};
- return undef;
+ else{
+ if(IsBadReadPtr($hproc, 4)){
+ Win32::SetLastError(ERROR_NOACCESS);
+ DEBUG "FAILED Function pointer '$hproc' is not a valid memory location\n";
+ return undef;
+ }
}
-
- #### determine if we have a prototype or not
+ #### determine if we have a prototype or not, outtype is for future use in XS
if ((not defined $in) and (not defined $out)) {
- ($proc, $self->{in}, $self->{intypes}, $self->{out}, $self->{cdecl}) =
- parse_prototype($proc);
- return undef unless $proc;
+ ($proc, $self->{in}, $self->{intypes}, $self->{out}, $self->{outtype},
+ $self->{cdecl}) = parse_prototype($class, $proc);
+ if( ! $proc ){
+ Win32::API::FreeLibrary($hdll) if $freedll;
+ return undef;
+ }
$self->{proto} = 1;
}
else {
$self->{in} = [];
if (ref($in) eq 'ARRAY') {
foreach (@$in) {
- push(@{$self->{in}}, type_to_num($_));
+ push(@{$self->{in}}, $class->type_to_num($_));
}
}
else {
my @in = split '', $in;
foreach (@in) {
- push(@{$self->{in}}, type_to_num($_));
+ push(@{$self->{in}}, $class->type_to_num($_));
}
}
- $self->{out} = type_to_num($out);
+ $self->{out} = $class->type_to_num($out);
$self->{cdecl} = calltype_to_num($callconvention);
}
- #### first try to import the function of given name...
- my $hproc = Win32::API::GetProcAddress($hdll, $proc);
+ if(!$hproc){ #if not non DLL func
+ #### first try to import the function of given name...
+ $hproc = Win32::API::GetProcAddress($hdll, $proc);
+
+ #### ...then try appending either A or W (for ASCII or Unicode)
+ if (!$hproc) {
+ my $tproc = $proc;
+ $tproc .= (IsUnicode() ? "W" : "A");
- #### ...then try appending either A or W (for ASCII or Unicode)
- if (!$hproc) {
- my $tproc = $proc;
- $tproc .= (IsUnicode() ? "W" : "A");
-
- # print "Win32::API::new: procedure not found, trying '$tproc'...\n";
- $hproc = Win32::API::GetProcAddress($hdll, $tproc);
- }
-
- #### ...if all that fails, set $! accordingly
- if (!$hproc) {
- $! = Win32::GetLastError();
- DEBUG "FAILED GetProcAddress for Proc '$proc': $!\n";
- return undef;
+ # print "Win32::API::new: procedure not found, trying '$tproc'...\n";
+ $hproc = Win32::API::GetProcAddress($hdll, $tproc);
+ }
+
+ #### ...if all that fails, set $! accordingly
+ if (!$hproc) {
+ $! = Win32::GetLastError();
+ DEBUG "FAILED GetProcAddress for Proc '$proc': $!\n";
+ Win32::API::FreeLibrary($hdll) if $freedll;
+ return undef;
+ }
+ DEBUG "GetProcAddress('$proc') = '$hproc'\n";
+ }
+ else {
+ DEBUG "Using non-DLL function pointer '$hproc' for '$proc'\n";
}
- DEBUG "GetProcAddress('$proc') = '$hproc'\n";
#### ok, let's stuff the object
$self->{procname} = $proc;
@@ -141,9 +167,10 @@
$self->{proc} = $hproc;
#### keep track of the imported function
- $Libraries{$dll} = $hdll;
- $Procedures{$dll}++;
-
+ if(defined $dll){
+ $Libraries{$dll} = $hdll;
+ $Procedures{$dll}++;
+ }
DEBUG "Object blessed!\n";
#### cast the spell
@@ -168,6 +195,7 @@
sub DESTROY {
my ($self) = @_;
+ return if ! defined $self->{dllname};
#### decrease this library's procedures reference count
$Procedures{$self->{dllname}}--;
@@ -188,7 +216,7 @@
if (!$type || $type eq "__stdcall") {
return 0;
}
- elsif ($type eq "_cdecl") {
+ elsif ($type eq "_cdecl" || $type eq "__cdecl") {
return 1;
}
else {
@@ -197,15 +225,26 @@
}
}
+
sub type_to_num {
+ die "wrong class" if shift ne "Win32::API";
my $type = shift;
my $out = shift;
- my $num;
+ my ($num, $numeric);
+ if(index($type, 'num', 0) == 0){
+ substr($type, 0, length('num'), '');
+ $numeric = 1;
+ }
+ else{
+ $numeric = 0;
+ }
if ( $type eq 'N'
or $type eq 'n'
or $type eq 'l'
- or $type eq 'L')
+ or $type eq 'L'
+ or $type eq 'Q'
+ or $type eq 'q')
{
$num = 1;
}
@@ -236,7 +275,7 @@
}
else {
$num = 0;
- }
+ }#not valid return types of the C func
unless (defined $out) {
if ( $type eq 's'
or $type eq 'S')
@@ -251,22 +290,124 @@
elsif ($type eq 'k'
or $type eq 'K')
{
- $num = 101;
+ $num = 55;
+ }
+ }
+ $num |= 0x40 if $numeric;
+ return $num;
+}
+
+package Win32::API::More;
+
+@ISA = qw ( Win32::API );
+sub type_to_num {
+ die "wrong class" if shift ne "Win32::API::More";
+ my $type = shift;
+ my $out = shift;
+ my ($num, $numeric);
+ if(index($type, 'num', 0) == 0){
+ substr($type, 0, length('num'), '');
+ $numeric = 1;
+ }
+ else{
+ $numeric = 0;
+ }
+
+ if ( $type eq 'N'
+ or $type eq 'n'
+ or $type eq 'l'
+ or $type eq 'L'
+ or $type eq 'Q'
+ or $type eq 'q'
+ or (! $out and # in XS short 'in's are interger/numbers code
+ $type eq 'S'
+ || $type eq 's'))
+ {
+ $num = 1;
+ if(defined $out && ($type eq 'N' || $type eq 'L'
+ || $type eq 'S' || $type eq 'Q')){
+ $num |= 0x80;
+ }
+ }
+ elsif ($type eq 'P'
+ or $type eq 'p')
+ {
+ $num = 2;
+ }
+ elsif ($type eq 'I'
+ or $type eq 'i')
+ {
+ $num = 3;
+ if(defined $out && $type eq 'I'){
+ $num |= 0x80;
+ }
+ }
+ elsif ($type eq 'f'
+ or $type eq 'F')
+ {
+ $num = 4;
+ }
+ elsif ($type eq 'D'
+ or $type eq 'd')
+ {
+ $num = 5;
+ }
+ elsif ($type eq 'c'
+ or $type eq 'C')
+ {
+ $num = 6;
+ if(defined $out && $type eq 'C'){
+ $num |= 0x80;
+ }
+ }
+ elsif ($type eq 's') #7 is only used for out params
+ {
+ $num = 7;
+ }
+ elsif ($type eq 'S')
+ {
+ $num = 7 | 0x80;
+ }
+ else {
+ $num = 0;
+ } #not valid return types of the C func
+ unless (defined $out) {
+ if ( $type eq 't'
+ or $type eq 'T')
+ {
+ $num = 51;
+ }
+ elsif ($type eq 'b'
+ or $type eq 'B')
+ {
+ $num = 22;
+ }
+ elsif ($type eq 'k'
+ or $type eq 'K')
+ {
+ $num = 55;
}
}
+ $num |= 0x40 if $numeric;
return $num;
}
+package Win32::API;
sub parse_prototype {
- my ($proto) = @_;
+ my ($class, $proto) = @_;
my @in_params = ();
- my @in_types = ();
- if ($proto =~ /^\s*(\S+)(?:\s+(\w+))?\s+(\S+)\s*\(([^\)]*)\)/) {
- my $ret = $1;
- my $callconvention = $2;
- my $proc = $3;
- my $params = $4;
+ my @in_types = (); #one day create a BNF-ish formal grammer parser here
+ if ($proto =~ /^\s*((?:(?:un|)signed\s+|) #optional signedness
+ \S+)(?:\s*(\*)\s*|\s+) #type and maybe a *
+ (?:(\w+)\s+)? # maybe a calling convention
+ (\S+)\s* #func name
+ \(([^\)]*)\) #param list
+ /x) {
+ my $ret = $1.(defined($2)?$2:'');
+ my $callconvention = $3;
+ my $proc = $4;
+ my $params = $5;
$params =~ s/^\s+//;
$params =~ s/\s+$//;
@@ -276,35 +417,41 @@
foreach my $param (split(/\s*,\s*/, $params)) {
my ($type, $name);
- if ($param =~ /(\S+)\s+(\S+)/) {
- ($type, $name) = ($1, $2);
+ #match "in_t* _var" "in_t * _var" "in_t *_var" "in_t _var" "in_t*_var" supported
+ #unsigned or signed or nothing as prefix supported
+ # "in_t ** _var" and "const in_t* var" not supported
+ if ($param =~ /((?:(?:un|)signed\s+|)\w+)(?:\s*(\*)\s*|\s+)(\w+)/) {
+ ($type, $name) = ($1.(defined($2)? $2:''), $3);
+ }
+ {
+ no warnings 'uninitialized';
+ if($type eq '') {goto BADPROTO;} #something very wrong, bail out
}
-
if (Win32::API::Type::is_known($type)) {
if (Win32::API::Type::is_pointer($type)) {
DEBUG "(PM)parse_prototype: IN='%s' PACKING='%s' API_TYPE=%d\n",
$type,
Win32::API::Type->packing($type),
- type_to_num('P');
- push(@in_params, type_to_num('P'));
+ $class->type_to_num('P');
+ push(@in_params, $class->type_to_num('P'));
}
else {
DEBUG "(PM)parse_prototype: IN='%s' PACKING='%s' API_TYPE=%d\n",
$type,
Win32::API::Type->packing($type),
- type_to_num(Win32::API::Type->packing($type));
- push(@in_params, type_to_num(Win32::API::Type->packing($type)));
+ $class->type_to_num(Win32::API::Type->packing($type, undef, 1));
+ push(@in_params, $class->type_to_num(Win32::API::Type->packing($type, undef, 1)));
}
}
elsif (Win32::API::Struct::is_known($type)) {
DEBUG "(PM)parse_prototype: IN='%s' PACKING='%s' API_TYPE=%d\n",
- $type, 'S', type_to_num('S');
- push(@in_params, type_to_num('S'));
+ $type, 'T', Win32::API::More->type_to_num('T');
+ push(@in_params, Win32::API::More->type_to_num('T'));
}
else {
warn
"Win32::API::parse_prototype: WARNING unknown parameter type '$type'";
- push(@in_params, type_to_num('I'));
+ push(@in_params, $class->type_to_num('I'));
}
push(@in_types, $type);
@@ -317,36 +464,39 @@
DEBUG "parse_prototype: OUT='%s' PACKING='%s' API_TYPE=%d\n",
$ret,
Win32::API::Type->packing($ret),
- type_to_num('P');
- return ($proc, \@in_params, \@in_types, type_to_num('P'),
- calltype_to_num($callconvention));
+ $class->type_to_num('P');
+ return ($proc, \@in_params, \@in_types, $class->type_to_num('P', 1),
+ $ret, calltype_to_num($callconvention));
}
else {
DEBUG "parse_prototype: OUT='%s' PACKING='%s' API_TYPE=%d\n",
$ret,
Win32::API::Type->packing($ret),
- type_to_num(Win32::API::Type->packing($ret));
+ $class->type_to_num(Win32::API::Type->packing($ret, undef, 1), 1);
return (
$proc, \@in_params, \@in_types,
- type_to_num(Win32::API::Type->packing($ret)),
- calltype_to_num($callconvention)
+ $class->type_to_num(Win32::API::Type->packing($ret, undef, 1), 1),
+ $ret, calltype_to_num($callconvention)
);
}
}
else {
warn
"Win32::API::parse_prototype: WARNING unknown output parameter type '$ret'";
- return ($proc, \@in_params, \@in_types, type_to_num('I'),
- calltype_to_num($callconvention));
+ return ($proc, \@in_params, \@in_types, $class->type_to_num('I', 1),
+ $ret, calltype_to_num($callconvention));
}
}
else {
+ BADPROTO:
warn "Win32::API::parse_prototype: bad prototype '$proto'";
return undef;
}
}
+
+
1;
__END__
@@ -364,23 +514,39 @@
#### Method 1: with prototype
use Win32::API;
- $function = Win32::API->new(
- 'mydll, 'int sum_integers(int a, int b)',
+ $function = Win32::API::More->new(
+ 'mydll', 'int sum_integers(int a, int b)',
);
$return = $function->Call(3, 2);
- #### Method 2: with parameter list
+ #### Method 2: with prototype and your function pointer
use Win32::API;
- $function = Win32::API->new(
+ $function = Win32::API::More->new(
+ undef, 38123456, 'int name_ignored(int a, int b)',
+ );
+ $return = $function->Call(3, 2);
+
+ #### Method 3: with parameter list
+
+ use Win32::API;
+ $function = Win32::API::More->new(
'mydll', 'sum_integers', 'II', 'I',
);
$return = $function->Call(3, 2);
- #### Method 3: with Import
+ #### Method 4: with parameter list and your function pointer
+
+ use Win32::API;
+ $function = Win32::API::More->new(
+ undef, 38123456, 'name_ignored', 'II', 'I',
+ );
+ $return = $function->Call(3, 2);
+
+ #### Method 5: with Import
use Win32::API;
- Win32::API->Import(
+ Win32::API::More->Import(
'mydll', 'int sum_integers(int a, int b)',
);
$return = sum_integers(3, 2);
@@ -395,7 +561,8 @@
=head1 ABSTRACT
With this module you can import and call arbitrary functions
-from Win32's Dynamic Link Libraries (DLL), without having
+from Win32's Dynamic Link Libraries (DLL) or arbitrary functions for
+which you have a pointer (MS COM, etc), without having
to write an XS extension. Note, however, that this module
can't do everything. In fact, parameters input and output is
limited to simpler cases.
@@ -411,9 +578,13 @@
the current process, eg. same as Perl's internal C<$$>):
use Win32::API;
- Win32::API->Import("kernel32", "int GetCurrentProcessId()");
+ Win32::API::More->Import("kernel32", "int GetCurrentProcessId()");
$PID = GetCurrentProcessId();
+Starting with 0.69. Win32::API initiated objects are deprecated due to numerous
+bugs and improvements, use Win32::API::More now. The use statement remains
+as C<use Win32::API;>.
+
The possibilities are nearly infinite (but not all are good :-).
Enjoy it.
@@ -424,23 +595,23 @@
use Win32::API;
You can now use the C<new()> function of the Win32::API module to create a
-new Win32::API object (see L<IMPORTING A FUNCTION>) and then invoke the
+new Win32::API::More object (see L<IMPORTING A FUNCTION>) and then invoke the
C<Call()> method on this object to perform a call to the imported API
(see L<CALLING AN IMPORTED FUNCTION>).
-Starting from version 0.40, you can also avoid creating a Win32::API object
+Starting from version 0.40, you can also avoid creating a Win32::API::More object
and instead automatically define a Perl sub with the same name of the API
function you're importing. The details of the API definitions are the same,
just the call is different:
- my $GetCurrentProcessId = Win32::API->new(
+ my $GetCurrentProcessId = Win32::API::More->new(
"kernel32", "int GetCurrentProcessId()"
);
my $PID = $GetCurrentProcessId->Call();
#### vs.
- Win32::API->Import("kernel32", "int GetCurrentProcessId()");
+ Win32::API::More->Import("kernel32", "int GetCurrentProcessId()");
$PID = GetCurrentProcessId();
Note that C<Import> returns 1 on success and 0 on failure (in which case you
@@ -449,8 +620,9 @@
=head2 IMPORTING A FUNCTION
You can import a function from a 32 bit Dynamic Link Library (DLL) file
-with the C<new()> function. This will create a Perl object that contains the
-reference to that function, which you can later C<Call()>.
+with the C<new()> function or, starting in 0.69, supply your own
+function pointer. This will create a Perl object that contains
+the reference to that function, which you can later C<Call()>.
What you need to know is the prototype of the function you're going to import
(eg. the definition of the function expressed in C syntax).
@@ -461,17 +633,28 @@
=head2 IMPORTING A FUNCTION BY PROTOTYPE
-You need to pass 2 parameters:
+You need to pass 2 or 3 parameters:
=over 4
=item 1.
-The name of the library from which you want to import the function.
+The name of the library from which you want to import the function. If the
+name is undef, you are requesting a object created from a function pointer,
+and must supply item 2.
=item 2.
-The C prototype of the function.
+This parameter is optional, most people should skip it, skip does not mean
+supplying undef. Supply a function pointer in the format of number 1234, not
+string "\x01\x02\x03\x04". Undef will be returned if the pointer is not
+readable, GetLastError will be ERROR_NOACCESS.
+
+=item 3.
+
+The C prototype of the function. If you are using a function pointer, the name
+of the function should be something "friendly" to you and no attempt is made
+to retrive such a name from any DLL's export table.
=back
@@ -482,9 +665,15 @@
See L<Win32::API::Type> for a list of the known parameter types and
L<Win32::API::Struct> for information on how to define a structure.
+If a prototype type is exactly C<signed char> or C<unsigned char> for an
+"in" parameter or the return parameter, and for "in" parameters only
+C<signed char *> or C<unsigned char *> the parameters will be treated as a
+number, C<0x01>, not C<"\x01">. "UCHAR" is not "unsigned char". Change the
+C prototype if you want numeric handling for your chars.
+
=head2 IMPORTING A FUNCTION WITH A PARAMETER LIST
-You need to pass 4 parameters:
+You need to pass at minimum 4 parameters.
=over 4
@@ -492,15 +681,23 @@
The name of the library from which you want to import the function.
=item 2.
-The name of the function (as exported by the library).
+This parameter is optional, most people should skip it, skip does not mean
+supplying undef. Supply a function pointer in the format of number C<1234>,
+not string C<"\x01\x02\x03\x04">. Undef will be returned if the pointer is not
+readable, GetLastError will be ERROR_NOACCESS.
=item 3.
-The number and types of the arguments the function expects as input.
+The name of the function (as exported by the library) or for function pointers
+a name that is "friendly" to you. No attempt is made to retrive such a
+name from any DLL's export table in the 2nd case.
=item 4.
-The type of the value returned by the function.
+The number and types of the arguments the function expects as input.
=item 5.
+The type of the value returned by the function.
+
+=item 6.
And optionally you can specify the calling convention, this defaults to
'__stdcall', alternatively you can specify '_cdecl'.
@@ -545,11 +742,16 @@
So, you don't have to write F<C:\windows\system\kernel32.dll>;
only F<kernel32> is enough:
- $GetTempPath = new Win32::API('kernel32', ...
+ $GetTempPath = new Win32::API::More('kernel32', ...
=item B<2.>
-Now for the second parameter: the name of the function.
+Since this function is from a DLL, skip the 2nd parameter. Skip does not
+mean supplying undef.
+
+=item B<3.>
+
+Now for the real second parameter: the name of the function.
It must be written exactly as it is exported
by the library (case is significant here).
If you are using Windows 95 or NT 4.0, you can use the B<Quick View>
@@ -560,7 +762,11 @@
when you see that all the exported functions are in upper case,
the DLL is a 16 bit one and you can't use it.
If their capitalization looks correct, then it's probably a 32 bit
-DLL.
+DLL. If you have Platform SDK or Visual Studio, you can use the Dumpbin
+tool. Call it as "dumpbin /exports name_of_dll.dll" on the command line.
+If you have Mingw GCC, use objdump as
+"objdump -x name_of_dll.dll > dlldump.txt" and search for the word exports in
+the very long output.
Also note that many Win32 APIs are exported twice, with the addition of
a final B<A> or B<W> to their name, for - respectively - the ASCII
@@ -570,11 +776,11 @@
Unicode system, then it will try with the B<W> instead.
So our function name will be:
- $GetTempPath = new Win32::API('kernel32', 'GetTempPath', ...
+ $GetTempPath = new Win32::API::More('kernel32', 'GetTempPath', ...
In our case C<GetTempPath> is really loaded as C<GetTempPathA>.
-=item B<3.>
+=item B<4.>
The third parameter, the input parameter list, specifies how many
arguments the function wants, and their types. It can be passed as
@@ -597,10 +803,16 @@
=over 4
=item C<I>:
-value is an integer (int)
+value is an unsigned integer (unsigned int)
+
+=item C<i>:
+value is an signed integer (signed int or int)
=item C<N>:
-value is a number (long)
+value is a unsigned pointer sized number (unsigned long)
+
+=item C<n>:
+value is a signed pointer sized number (signed long or long)
=item C<F>:
value is a floating point number (float)
@@ -608,13 +820,24 @@
=item C<D>:
value is a double precision number (double)
+=item C<S>:
+value is a unsigned short (unsigned short)
+
+=item C<s>:
+value is a signed short (signed short or short)
+
=item C<C>:
-value is a char (char)
+value is a char (char), pass as C<"a>", not C<97>, C<"abc"> will truncate to C<"a">
=item C<P>:
value is a pointer (to a string, structure, etc...)
+padding out the buffer string is required, buffer overflow detection is
+performed. Pack and unpack the data yourself. If P is a return type, only
+null terminated strings or NULL pointer are supported. It is suggested to
+not use P as a return type and instead use N and read the memory yourself, and
+free the pointer if applicable.
-=item C<S>:
+=item C<T>:
value is a Win32::API::Struct object (see below)
=item C<K>:
@@ -632,10 +855,10 @@
The fourth and final parameter is the type of the value returned by the
function. It can be one of the types seen above, plus another type named B<V>
(for C<void>), used for functions that do not return a value.
-In our example the value returned by GetTempPath() is a C<DWORD>, so
-our return type will be B<N>:
+In our example the value returned by GetTempPath() is a C<DWORD>, which is a
+typedef for unsigned long, so our return type will be B<N>:
- $GetTempPath = new Win32::API('kernel32', 'GetTempPath', 'NP', 'N');
+ $GetTempPath = new Win32::API::More('kernel32', 'GetTempPath', 'NP', 'N');
Now the line is complete, and the GetTempPath() API is ready to be used
in Perl. Before calling it, you should test that $GetTempPath is
@@ -644,7 +867,7 @@
by Windows.
Our definition, with error checking added, should then look like this:
- $GetTempPath = new Win32::API('kernel32', 'GetTempPath', 'NP', 'N');
+ $GetTempPath = new Win32::API::More('kernel32', 'GetTempPath', 'NP', 'N');
if(not defined $GetTempPath) {
die "Can't import API GetTempPath: $!\n";
}
@@ -668,10 +891,9 @@
The two parameters needed here are the length of the buffer
that will hold the returned temporary path, and a pointer to the
buffer itself.
-For numerical parameters, you can use either a constant expression
-or a variable, while B<for pointers you must use a variable name> (no
-Perl references, just a plain variable name).
-Also note that B<memory must be allocated before calling the function>,
+For numerical parameters except for char, you can use either a constant expression
+or a variable, it will be numified similar to the expression C<($var+0)>.
+For pointers, also note that B<memory must be allocated before calling the function>,
just like in C.
For example, to pass a buffer of 80 characters to GetTempPath(),
it must be initialized before with:
@@ -679,14 +901,14 @@
$lpBuffer = " " x 80;
This allocates a string of 80 characters. If you don't do so, you'll
-probably get C<Runtime exception> errors, and generally nothing will
-work. The call should therefore include:
+probably get a fatal buffer overflow error starting in 0.69.
+The call should therefore include:
$lpBuffer = " " x 80;
$GetTempPath->Call(80, $lpBuffer);
And the result will be stored in the $lpBuffer variable.
-Note that you don't need to pass a reference to the variable
+Note that you never need to pass a reference to the variable
(eg. you B<don't need> C<\$lpBuffer>), even if its value will be set
by the function.
@@ -762,18 +984,98 @@
you have to pack() the required elements in a variable:
- $lpPoint = pack('LL', 0, 0); # store two LONGs
+ $lpPoint = pack('ll', 0, 0); # store two LONGs
=item 2.
to access the values stored in a structure, unpack() it as required:
- ($x, $y) = unpack('LL', $lpPoint); # get the actual values
+ ($x, $y) = unpack(';;', $lpPoint); # get the actual values
=back
The rest is left as an exercise to the reader...
+=head2 EXPORTED FUNCTIONS
+
+=head3 ReadMemory
+
+ $copy_of_memblock = ReadMemory($SourcePtr, $length);
+
+Reads the source pointer for C<$length> number of bytes. Returns a copy of
+the memory block in a scalar. No readability checking is done on C<$SourcePtr>.
+C<$SourcePtr>'s format is 123456, not C<"\x01\x02\x03\x04">.
+
+=head3 WriteMemory
+
+ WriteMemory($DestPtr, $sourceScalar, $length);
+
+Copies the string contents of the C<$sourceScalar> scalar to C<$DestPtr> for
+C<$length> bytes. $length must be less than or equal to the length of
+C<$sourceScalar>, otherwise the function croaks. No readability checking is
+done on C<$DestPtr>. C<$DestPtr>'s format is 123456, not
+C<"\x01\x02\x03\x04">. Returns nothing.
+
+=head3 MoveMemory
+
+ MoveMemory($destPtr, $sourcePtr, $length);
+
+Copies a block of memory from one location to another. The source and
+destination blocks may overlap. All pointers are in the format of 123456,
+not C<"\x01\x02\x03\x04">. No readability checking is done. Returns nothing.
+
+=head3 IsBadReadPtr
+
+ if(IsBadReadPtr($ptr, $length)) {die "bad ptr";}
+
+Probes a memory block for C<$length> bytes for readability. Returns true if
+access violation occurs, otherwise false is returned. This function is useful
+to avoid dereferencing pointers which will crash the perl process. This function
+has many limitations, including not detecting uninitialized memory, not
+detecting freed memory, and not detecting giberrish. It can not tell whether a
+function pointer is valid x86 machine code. Ideally, you should never use it,
+or remove it once your code is stable. C<$ptr> is in the format of 123456,
+not C<"\x01\x02\x03\x04">. See MS's documentation for alot more
+on this function of the same name.
+
+
+=head1 HISTORY
+
+=over 4
+
+=item return value signedness
+
+Prior to 0.69, for numeric integer types, the return scalar was always signed.
+Unsigned-ness was ignored.
+
+=item shorts
+
+Prior to 0.69, shorts were not supported. 'S' meant a sturct. To fix this
+Win32::API::More class was created for 0.69. 'S'/'s' now means short, per pack's
+letters. Struct has been moved to letter 'T'. Win32::API will continue to exist
+for legacy code.
+
+=item float return types
+
+Prior to 0.69, if a function had a return type of float, it was silently
+not called.
+
+=item buffer overflow protection
+
+Introduced in 0.69. If disabling is required, which is highly
+B<not recommended>, set an enviromental variable called
+WIN32_API_SORRY_I_WAS_AN_IDIOT to 1.
+
+=item automatic un/pack
+
+Starting with 0.69, when using Win32::API::More, there is automatic un/packing
+of pointers to numbers-ish things for in parameters when using the C
+prototype interface.
+
+=back
+
+See the C<Changes> file for more details.
+
=head1 AUTHOR
Aldo Calpini ( I<[email protected]> ).
diff -ruNZ old/API.xs new/API.xs
--- old/API.xs 2011-12-25 16:09:32.000000000 -0500
+++ new/API.xs 2012-05-29 10:23:14.296875000 -0400
@@ -10,7 +10,7 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <memory.h>
-
+#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
@@ -51,39 +51,38 @@
#error "Don't know what architecture I'm on."
#endif
-void pointerCallPack(SV* param, int idx, AV* types) {
- dSP;
- SV* type;
+#ifndef mPUSHs
+# define mPUSHs(s) PUSHs(sv_2mortal(s))
+#endif
+
+#ifndef mXPUSHs
+# define mXPUSHs(s) XPUSHs(sv_2mortal(s))
+#endif
- type = *( av_fetch(types, idx, 0) );
+void pointerCallPack(pTHX_ SV * obj, SV * param, SV * type) {