forked from mhammond/pywin32
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.cpp
More file actions
4173 lines (3896 loc) · 163 KB
/
shell.cpp
File metadata and controls
4173 lines (3896 loc) · 163 KB
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
// shell.cpp :
// $Id$
// Interfaces that support the Explorer Shell interfaces.
/***
Note that this source file contains embedded documentation.
This documentation consists of marked up text inside the
C comments, and is prefixed with an '@' symbol. The source
files are processed by a tool called "autoduck" which
generates Windows .hlp files.
@doc
***/
// Any python API functions that use 's#' format must use Py_ssize_t for length
#define PY_SSIZE_T_CLEAN
#include "shell_pch.h"
#include "EmptyVC.h"
#include "PyIShellLink.h"
#include "PyICategorizer.h"
#include "PyICategoryProvider.h"
#include "PyIContextMenu.h"
#include "PyIContextMenu2.h"
#include "PyIContextMenu3.h"
#include "PyIDefaultExtractIconInit.h"
#include "PyIExtractIcon.h"
#include "PyIExtractIconW.h"
#include "PyIShellExtInit.h"
#include "PyIShellFolder.h"
#include "PyIShellFolder2.h"
#include "PyIEmptyVolumeCache.h"
#include "PyIEmptyVolumeCacheCallBack.h"
#include "PyIEnumExplorerCommand.h"
#include "PyIEnumIDList.h"
#include "PyICopyHook.h"
#include "PyIOleWindow.h"
#include "PyIShellView.h"
#include "PyIShellIcon.h"
#include "PyIShellIconOverlay.h"
#include "PyIShellIconOverlayManager.h"
#include "PyIShellIconOverlayIdentifier.h"
#include "PyIShellBrowser.h"
#include "PyIBrowserFrameOptions.h"
#include "PyIPersist.h"
#include "PyIPersistFolder.h"
#include "PyIColumnProvider.h"
#include "PyIDropTargetHelper.h"
#include "PyIAsyncOperation.h"
#include "PyIQueryAssociations.h"
#include "PyIDockingWindow.h"
#include "PyIDeskBand.h"
#include "PyIShellLinkDataList.h"
#include "PyIUniformResourceLocator.h"
#include "PyIActiveDesktop.h"
#include "PyIExtractImage.h"
#include "PyIExplorerBrowser.h"
#include "PyIExplorerBrowserEvents.h"
#include "PyIExplorerCommand.h"
#include "PyIExplorerCommandProvider.h"
#include "PyIExplorerPaneVisibility.h"
#include "PyIShellItem.h"
#include "PyIShellItem2.h"
#include "PyIShellItemArray.h"
#include "PyINameSpaceTreeControl.h"
#include "PyIEnumShellItems.h"
#include "PyIKnownFolder.h"
#include "PyIKnownFolderManager.h"
#include "PyITaskbarList.h"
#include "PyIFileOperation.h"
#include "PyIFileOperationProgressSink.h"
#include "PyITransferSource.h"
#include "PyITransferDestination.h"
#include "PyITransferAdviseSink.h"
#include "PyIShellItemResources.h"
#include "PyIEnumResources.h"
#include "PyIFolderView.h"
#include "PyIRelatedItem.h" // Next 4 all derived from IRelatedItem
#include "PyIDisplayItem.h"
#include "PyICurrentItem.h"
#include "PyITransferMediumItem.h"
#include "PyIIdentityName.h"
#include "PyIEnumObjects.h"
#include "PyIApplicationDocumentLists.h"
#include "PyIApplicationDestinations.h"
#include "PyIObjectArray.h"
#include "PyIObjectCollection.h"
#include "PyICustomDestinationList.h"
#include "PyIShellLibrary.h"
#include "PythonCOMRegister.h" // For simpler registration of IIDs etc.
// We should not be using this!
#define OleSetOleError PyCom_BuildPyException
static HMODULE shell32 = NULL;
static HMODULE shfolder = NULL;
static HMODULE shlwapi = NULL;
typedef BOOL(WINAPI *PFNSHGetSpecialFolderPath)(HWND, LPWSTR, int, BOOL);
static PFNSHGetSpecialFolderPath pfnSHGetSpecialFolderPath = NULL;
typedef HRESULT(WINAPI *PFNSHGetFolderLocation)(HWND, int, HANDLE, DWORD, LPITEMIDLIST *);
static PFNSHGetFolderLocation pfnSHGetFolderLocation = NULL;
typedef HRESULT(WINAPI *PFNSHEmptyRecycleBin)(HWND, LPSTR, DWORD);
static PFNSHEmptyRecycleBin pfnSHEmptyRecycleBin = NULL;
typedef void(WINAPI *PFNSHGetSettings)(LPSHELLFLAGSTATE, DWORD);
static PFNSHGetSettings pfnSHGetSettings = NULL;
typedef HRESULT(WINAPI *PFNSHGetFolderPath)(HWND, int, HANDLE, DWORD, LPWSTR);
static PFNSHGetFolderPath pfnSHGetFolderPath = NULL;
typedef HRESULT(WINAPI *PFNSHSetFolderPath)(int, HANDLE, DWORD, LPCWSTR);
static PFNSHSetFolderPath pfnSHSetFolderPath = NULL;
typedef HRESULT(WINAPI *PFNSHQueryRecycleBin)(LPCWSTR, LPSHQUERYRBINFO);
static PFNSHQueryRecycleBin pfnSHQueryRecycleBin = NULL;
typedef HRESULT(WINAPI *PFNSHGetViewStatePropertyBag)(LPCITEMIDLIST, LPCWSTR, DWORD, REFIID, void **);
static PFNSHGetViewStatePropertyBag pfnSHGetViewStatePropertyBag = NULL;
typedef HRESULT(WINAPI *PFNSHILCreateFromPath)(LPCWSTR, LPITEMIDLIST *, DWORD *);
static PFNSHILCreateFromPath pfnSHILCreateFromPath = NULL;
typedef HRESULT(WINAPI *PFNAssocCreate)(CLSID, REFIID, LPVOID);
static PFNAssocCreate pfnAssocCreate = NULL;
typedef HRESULT(WINAPI *PFNAssocCreateForClasses)(const ASSOCIATIONELEMENT *, ULONG cClasses, REFIID riid, void **ppv);
static PFNAssocCreateForClasses pfnAssocCreateForClasses = NULL;
typedef LRESULT(WINAPI *PFNSHShellFolderView_Message)(HWND, UINT, LPARAM);
static PFNSHShellFolderView_Message pfnSHShellFolderView_Message = NULL;
typedef BOOL(WINAPI *PFNIsUserAnAdmin)();
static PFNIsUserAnAdmin pfnIsUserAnAdmin = NULL;
typedef BOOL(WINAPI *PFNSHGetNameFromIDList)(PCIDLIST_ABSOLUTE, SIGDN, PWSTR *);
static PFNSHGetNameFromIDList pfnSHGetNameFromIDList = NULL;
typedef BOOL(WINAPI *PFNSHCreateShellFolderView)(const SFV_CREATE *, IShellView **ppsv);
static PFNSHCreateShellFolderView pfnSHCreateShellFolderView = NULL;
typedef BOOL(WINAPI *PFNSHCreateDefaultExtractIcon)(REFIID riid, void **ppv);
static PFNSHCreateDefaultExtractIcon pfnSHCreateDefaultExtractIcon = NULL;
typedef BOOL(WINAPI *PFNSHCreateDataObject)(PCIDLIST_ABSOLUTE, UINT, PCUITEMID_CHILD_ARRAY, IDataObject *, REFIID,
void **);
static PFNSHCreateDataObject pfnSHCreateDataObject = NULL;
typedef BOOL(WINAPI *PFNSHCreateShellItemArray)(PCIDLIST_ABSOLUTE, IShellFolder *, UINT, PCUITEMID_CHILD_ARRAY,
IShellItemArray **);
static PFNSHCreateShellItemArray pfnSHCreateShellItemArray = NULL;
typedef BOOL(WINAPI *PFNSHCreateShellItemArrayFromDataObject)(IDataObject *pdo, REFIID, void **);
static PFNSHCreateShellItemArrayFromDataObject pfnSHCreateShellItemArrayFromDataObject = NULL;
typedef BOOL(WINAPI *PFNSHCreateShellItemArrayFromIDLists)(UINT, PCIDLIST_ABSOLUTE_ARRAY, IShellItemArray **);
static PFNSHCreateShellItemArrayFromIDLists pfnSHCreateShellItemArrayFromIDLists = NULL;
typedef BOOL(WINAPI *PFNSHCreateShellItemArrayFromShellItem)(IShellItem *, REFIID riid, void **);
static PFNSHCreateShellItemArrayFromShellItem pfnSHCreateShellItemArrayFromShellItem = NULL;
typedef BOOL(WINAPI *PFNSHCreateDefaultContextMenu)(const DEFCONTEXTMENU *, REFIID, void **);
static PFNSHCreateDefaultContextMenu pfnSHCreateDefaultContextMenu = NULL;
typedef HRESULT(WINAPI *PFNSHCreateItemFromIDList)(PCIDLIST_ABSOLUTE, REFIID, void **);
static PFNSHCreateItemFromIDList pfnSHCreateItemFromIDList = NULL;
typedef HRESULT(WINAPI *PFNSHCreateItemFromParsingName)(PCWSTR, IBindCtx *, REFIID, void **);
static PFNSHCreateItemFromParsingName pfnSHCreateItemFromParsingName = NULL;
typedef HRESULT(WINAPI *PFNSHCreateItemFromRelativeName)(IShellItem *, PCWSTR, IBindCtx *, REFIID, void **);
static PFNSHCreateItemFromRelativeName pfnSHCreateItemFromRelativeName = NULL;
typedef HRESULT(WINAPI *PFNSHCreateItemInKnownFolder)(REFKNOWNFOLDERID, DWORD, PCWSTR, REFIID, void **);
static PFNSHCreateItemInKnownFolder pfnSHCreateItemInKnownFolder = NULL;
typedef HRESULT(WINAPI *PFNSHCreateItemWithParent)(PCIDLIST_ABSOLUTE, IShellFolder *, PCUITEMID_CHILD, REFIID, void **);
static PFNSHCreateItemWithParent pfnSHCreateItemWithParent = NULL;
typedef HRESULT(WINAPI *PFNSHGetIDListFromObject)(IUnknown *, PIDLIST_ABSOLUTE *);
static PFNSHGetIDListFromObject pfnSHGetIDListFromObject = NULL;
typedef HRESULT(WINAPI *PFNSHCreateShellItem)(PCIDLIST_ABSOLUTE, IShellFolder *, PCUITEMID_CHILD, IShellItem **);
static PFNSHCreateShellItem pfnSHCreateShellItem = NULL;
typedef HRESULT(WINAPI *PFNSHOpenFolderAndSelectItems)(PCIDLIST_ABSOLUTE, UINT, PCUITEMID_CHILD_ARRAY, DWORD);
static PFNSHOpenFolderAndSelectItems pfnSHOpenFolderAndSelectItems = NULL;
typedef HRESULT(WINAPI *PFNSHCreateStreamOnFileEx)(LPCWSTR, DWORD, DWORD, BOOL, IStream *, IStream **);
static PFNSHCreateStreamOnFileEx pfnSHCreateStreamOnFileEx = NULL;
typedef HRESULT(WINAPI *PFNSetCurrentProcessExplicitAppUserModelID)(WCHAR *);
static PFNSetCurrentProcessExplicitAppUserModelID pfnSetCurrentProcessExplicitAppUserModelID;
typedef HRESULT(WINAPI *PFNGetCurrentProcessExplicitAppUserModelID)(WCHAR **);
static PFNGetCurrentProcessExplicitAppUserModelID pfnGetCurrentProcessExplicitAppUserModelID;
typedef HRESULT(WINAPI *PFNSHParseDisplayName)(LPCWSTR, IBindCtx *, PIDLIST_ABSOLUTE *, SFGAOF, SFGAOF *);
static PFNSHParseDisplayName pfnSHParseDisplayName;
// Some magic hackery macros :-)
#define _ILSkip(pidl, cb) ((LPITEMIDLIST)(((BYTE *)(pidl)) + cb))
#define _ILNext(pidl) _ILSkip(pidl, (pidl)->mkid.cb)
UINT PyShell_ILGetSize(LPCITEMIDLIST pidl)
{
UINT cbTotal = 0;
if (pidl) {
cbTotal += sizeof(pidl->mkid.cb); // "Null" (ie, 0 .cb) terminator
while (pidl->mkid.cb) {
cbTotal += pidl->mkid.cb;
pidl = _ILNext(pidl);
}
}
return cbTotal;
}
PyObject *PyObject_FromPIDL(LPCITEMIDLIST pidl, BOOL bFreeSystemPIDL)
{
if (pidl == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
LPCITEMIDLIST pidl_free = pidl;
PyObject *ret = PyList_New(0);
if (!ret)
return NULL;
__try {
while (pidl->mkid.cb) {
// cb includes sizeof(cb) itself - so string len is cb-sizeof(cb)
if (pidl->mkid.cb <= sizeof(pidl->mkid.cb)) {
Py_DECREF(ret);
ret = NULL;
PyErr_SetString(PyExc_ValueError, "This string has an invalid sub-item (too short)");
break;
}
// The length may be too large to read (and causing an
// exception deep inside Python doesn't always leave
// things in a good state! It's also inconvenient to
// always pass the size of the object - so explicitly
// check we can read the memory.
UINT cbdata = pidl->mkid.cb - sizeof(pidl->mkid.cb);
if (IsBadReadPtr(pidl->mkid.abID, cbdata)) {
Py_DECREF(ret);
ret = NULL;
PyErr_SetString(PyExc_ValueError, "This string has an invalid sub-item (too long)");
break;
}
PyObject *sub = PyBytes_FromStringAndSize((char *)pidl->mkid.abID, cbdata);
if (sub) {
PyList_Append(ret, sub);
Py_DECREF(sub);
}
pidl = _ILNext(pidl);
}
}
#if defined(__MINGW32__) || defined(MAINWIN)
catch (...)
#else
__except (EXCEPTION_EXECUTE_HANDLER)
#endif
{
Py_DECREF(ret);
PyErr_SetString(PyExc_ValueError, "This string is an invalid PIDL (win32 exception unpacking)");
ret = NULL;
}
if (bFreeSystemPIDL)
CoTaskMemFree((void *)pidl_free);
return ret;
}
// @object PyIDL|A Python representation of an IDL. Implemented as a sequence of Python strings.
BOOL PyObject_AsPIDL(PyObject *ob, LPITEMIDLIST *ppidl, BOOL bNoneOK /*= FALSE*/, UINT *pcb /* = NULL */)
{
if (ob == Py_None) {
if (!bNoneOK) {
PyErr_SetString(PyExc_TypeError, "None is not a valid ITEMIDLIST in this context");
return FALSE;
}
*ppidl = NULL;
if (pcb)
*pcb = 0;
return TRUE;
}
if (!PySequence_Check(ob) || PyBytes_Check(ob)) {
PyErr_Format(PyExc_TypeError, "Only sequences (but not strings) are valid ITEMIDLIST objects (got %s).",
ob->ob_type->tp_name);
return FALSE;
}
Py_ssize_t num_items = PySequence_Length(ob);
// SHITEMID.cb is a ushort, and includes its own size - make sure size of python string doesn't overflow it
Py_ssize_t cbMax = USHRT_MAX - sizeof((*ppidl)->mkid.cb);
// first pass over the sequence to determine number of bytes.
size_t cbTotal = sizeof((*ppidl)->mkid.cb); // Null terminator
Py_ssize_t i;
for (i = 0; i < num_items; i++) {
PyObject *sub = PySequence_GetItem(ob, i);
if (!sub)
return FALSE;
if (!PyBytes_Check(sub)) {
PyErr_Format(PyExc_TypeError, "ITEMIDLIST sub-items must be strings (got %s)", sub->ob_type->tp_name);
Py_DECREF(sub);
return FALSE;
}
if (PyBytes_GET_SIZE(sub) > cbMax) {
PyErr_Format(PyExc_ValueError, "Python string exceeds maximum size for a PIDL item");
Py_DECREF(sub);
return FALSE;
}
cbTotal += sizeof((*ppidl)->mkid.cb) + PyBytes_GET_SIZE(sub);
Py_DECREF(sub);
}
// Now again, filling our buffer.
void *buf = CoTaskMemAlloc(cbTotal);
if (!buf) {
PyErr_NoMemory();
return FALSE;
}
LPITEMIDLIST pidl = (LPITEMIDLIST)buf;
for (i = 0; i < num_items; i++) {
PyObject *sub = PySequence_GetItem(ob, i);
if (!sub)
return FALSE;
/* Don't need to check this again, called holding GIL so nothing can modify the sequence
if (!PyBytes_Check(sub)) {
PyErr_Format(PyExc_TypeError, "ITEMIDLIST sub-items must be strings (got %s)", sub->ob_type->tp_name);
Py_DECREF(sub);
return FALSE;
}
*/
pidl->mkid.cb = (USHORT)PyBytes_GET_SIZE(sub) + sizeof(pidl->mkid.cb);
memcpy(pidl->mkid.abID, PyBytes_AS_STRING(sub), PyBytes_GET_SIZE(sub));
Py_DECREF(sub);
pidl = _ILNext(pidl);
}
pidl->mkid.cb = 0;
*ppidl = (LPITEMIDLIST)buf;
// ??? Should change pcb to a size_t, only place it's used is CIDA conversion ???
if (pcb)
*pcb = (UINT)cbTotal;
return TRUE;
}
void PyObject_FreePIDL(LPCITEMIDLIST pidl) { CoTaskMemFree((void *)pidl); }
BOOL PyObject_AsPIDLArray(PyObject *obSeq, UINT *pcidl, LPCITEMIDLIST **ret)
{
// string is a seq - handle that
*pcidl = 0;
*ret = NULL;
if (PyBytes_Check(obSeq) || !PySequence_Check(obSeq)) {
PyErr_SetString(PyExc_TypeError, "Must be an array of IDLs");
return FALSE;
}
int n = PySequence_Length(obSeq);
LPCITEMIDLIST *ppidl = (LPCITEMIDLIST *)malloc(n * sizeof(ITEMIDLIST *));
if (!ppidl) {
PyErr_NoMemory();
return FALSE;
}
ZeroMemory(ppidl, n * sizeof(ITEMIDLIST *));
for (int i = 0; i < n; i++) {
PyObject *ob = PySequence_GetItem(obSeq, i);
if (!ob || !PyObject_AsPIDL(ob, (ITEMIDLIST **)&ppidl[i], FALSE)) {
Py_XDECREF(ob);
PyObject_FreePIDLArray(n, ppidl);
return FALSE;
}
Py_DECREF(ob);
}
*pcidl = n;
*ret = ppidl;
return TRUE;
}
void PyObject_FreePIDLArray(UINT cidl, LPCITEMIDLIST *pidl)
{
for (UINT i = 0; i < cidl; i++)
if (pidl[i])
PyObject_FreePIDL(pidl[i]);
free(pidl);
}
PyObject *PyObject_FromPIDLArray(UINT cidl, LPCITEMIDLIST *pidl)
{
if (pidl == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject *ob = PyList_New(cidl);
if (!ob)
return NULL;
for (UINT i = 0; i < cidl; i++) {
PyObject *n = PyObject_FromPIDL(pidl[i], FALSE);
if (!n) {
Py_DECREF(ob);
return NULL;
}
PyList_SET_ITEM(ob, i, n); // consumes ref to 'n'
}
return ob;
}
// See MSDN
// https://learn.microsoft.com/en-us/windows/win32/shell/dragdrop
// https://learn.microsoft.com/en-us/windows/win32/shell/clipboard#cfstr_shellidlist
// (or search for "CFSTR_SHELLIDLIST")
#define GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida) + (pida)->aoffset[0])
#define GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida) + (pida)->aoffset[i + 1])
PyObject *PyObject_FromCIDA(CIDA *pida)
{
unsigned int i;
PyObject *ret = NULL;
PyObject *obItems = NULL;
if (pida == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject *obFolder = PyObject_FromPIDL(GetPIDLFolder(pida), FALSE);
if (obFolder == NULL)
goto done;
// cidl == Number of PIDLs that are being transferred, *not* counting the parent folder
obItems = PyList_New(pida->cidl);
for (i = 0; i < pida->cidl; i++) {
PyObject *obChild = PyObject_FromPIDL(GetPIDLItem(pida, i), FALSE);
if (obChild == NULL)
goto done;
PyList_SET_ITEM(obItems, i, obChild);
}
assert(obFolder && obItems);
ret = Py_BuildValue("OO", obFolder, obItems);
done:
Py_XDECREF(obItems);
Py_XDECREF(obFolder);
return ret;
}
struct PyCIDAHelper {
ITEMIDLIST *pidl;
UINT pidl_size;
};
PyObject *PyObject_AsCIDA(PyObject *ob)
{
PyObject *obParent, *obKids;
PyObject *ret = NULL;
ITEMIDLIST *pidlParent = NULL;
UINT cbParent;
PyCIDAHelper *pKids = NULL;
int nKids = 0;
int i;
if (!PyArg_ParseTuple(ob, "OO:CIDA", &obParent, &obKids))
return NULL;
if (!PyObject_AsPIDL(obParent, &pidlParent, FALSE, &cbParent))
goto done;
if (!PySequence_Check(obKids)) {
PyErr_Format(PyExc_ValueError, "Kids must be a sequence if PIDLs (not %s)", obKids->ob_type->tp_name);
goto done;
}
nKids = PySequence_Length(obKids);
if (nKids == -1)
goto done;
pKids = (PyCIDAHelper *)malloc(sizeof(PyCIDAHelper) * nKids);
if (pKids == NULL) {
PyErr_NoMemory();
goto done;
}
memset(pKids, 0, sizeof(PyCIDAHelper) * nKids);
for (i = 0; i < nKids; i++) {
BOOL ok;
PyObject *obKid = PySequence_GetItem(obKids, i);
if (!obKids)
goto done;
ok = PyObject_AsPIDL(obKid, &pKids[i].pidl, FALSE, &pKids[i].pidl_size);
Py_DECREF(obKid);
if (!ok)
goto done;
}
/* Calculate size of final buffer. */
{ /* temp scope for new locals */
UINT nbytes, pidl_offset;
LPBYTE pidl_buf;
CIDA *pcida;
// count, plus array of offsets.
nbytes = pidl_offset = sizeof(UINT) + (sizeof(UINT) * (nKids + 1));
// The parent.
nbytes += cbParent;
// and each kid.
for (i = 0; i < nKids; i++) nbytes += pKids[i].pidl_size;
ret = PyBytes_FromStringAndSize(NULL, nbytes);
pcida = (CIDA *)PyBytes_AS_STRING(ret);
pcida->cidl = nKids; // not counting parent.
pidl_buf = ((LPBYTE)pcida) + pidl_offset;
pcida->aoffset[0] = pidl_offset;
memcpy(pidl_buf, pidlParent, cbParent);
pidl_buf += cbParent;
pidl_offset += cbParent;
for (i = 0; i < nKids; i++) {
pcida->aoffset[i + 1] = pidl_offset;
memcpy(pidl_buf, pKids[i].pidl, pKids[i].pidl_size);
pidl_offset += pKids[i].pidl_size;
pidl_buf += pKids[i].pidl_size;
}
assert(pidl_buf == ((LPBYTE)pcida) + nbytes);
} // end temp scope
done:
if (pidlParent)
PyObject_FreePIDL(pidlParent);
if (pKids) {
for (i = 0; i < nKids; i++) {
if (pKids[i].pidl)
PyObject_FreePIDL(pKids[i].pidl);
}
free(pKids);
}
return ret;
}
void PyObject_FreeTBBUTTONs(TBBUTTON *p, UINT nButtons)
{
for (UINT i = 0; i < nButtons; i++) PyWinObject_FreeResourceId((TCHAR *)p[i].iString);
free(p);
}
BOOL PyObject_AsTBBUTTONs(PyObject *ob, TBBUTTON **ppButtons, UINT *pnButtons)
{
*ppButtons = NULL;
*pnButtons = 0;
if (ob == Py_None)
return TRUE;
DWORD nButtons;
TmpPyObject obbuttons = PyWinSequence_Tuple(ob, &nButtons);
if (obbuttons == NULL)
return FALSE;
*ppButtons = (TBBUTTON *)malloc(nButtons * sizeof(TBBUTTON));
if (!*ppButtons) {
PyErr_NoMemory();
return FALSE;
}
BOOL ok;
memset(*ppButtons, 0, nButtons * sizeof(TBBUTTON));
for (UINT i = 0; i < nButtons; i++) {
TBBUTTON *pThis = (*ppButtons) + i;
PyObject *sub = PyTuple_GET_ITEM((PyObject *)obbuttons, i);
PyObject *obdata, *obstring;
ok = PyArg_ParseTuple(sub, "iiBB|OO", &pThis->iBitmap, &pThis->idCommand, &pThis->fsState, &pThis->fsStyle,
&obdata, &obstring) &&
PyWinLong_AsVoidPtr(obdata, (void **)&pThis->dwData) &&
PyWinObject_AsResourceId(obstring, (TCHAR **)&pThis->iString);
if (!ok)
break;
}
if (!ok) {
PyObject_FreeTBBUTTONs(*ppButtons, nButtons);
*ppButtons = NULL;
}
else
*pnButtons = nButtons;
return ok;
}
PyObject *PyWinObject_FromRESOURCESTRING(LPCSTR str)
{
if (HIWORD(str) == 0)
return PyLong_FromLong(LOWORD(str));
return PyBytes_FromString(str);
}
// @object PyCMINVOKECOMMANDINFO|A tuple of parameters to be converted to a CMINVOKECOMMANDINFO struct
// @tupleitem 0|int|Mask|Combination of shellcon.CMIC_MASK_* constants, can be 0
// @tupleitem 1|<o PyHANDLE>|hwnd|Window that owns the shortcut menu
// @tupleitem 2|int or str|Verb|Action to be carried out, specified as a string command or integer menu item id
// @tupleitem 3|str|Parameters|Extra parameters to be passed to the command line for the action, can be None
// @tupleitem 4|str|Directory|Working directory, can be None
// @tupleitem 5|int|Show|Combination of win32con.SW_* constants for any windows that may be created
// @tupleitem 6|int|HotKey|Hot key for any application that may be started
// @tupleitem 7|<o PyHANDLE>|Icon|Handle to icon to use for application, can be None
BOOL PyObject_AsCMINVOKECOMMANDINFO(PyObject *ob, CMINVOKECOMMANDINFO *pci)
{
PyObject *obVerb, *obhwnd, *obhIcon;
ZeroMemory(pci, sizeof(CMINVOKECOMMANDINFO));
pci->cbSize = sizeof(CMINVOKECOMMANDINFO);
if (!PyArg_ParseTuple(ob, "iOOzziiO:CMINVOKECOMMANDINFO tuple", &pci->fMask, &obhwnd, &obVerb, &pci->lpParameters,
&pci->lpDirectory, &pci->nShow, &pci->dwHotKey, &obhIcon))
return FALSE;
if (!PyWinObject_AsHANDLE(obhwnd, (HANDLE *)&pci->hwnd))
return FALSE;
if (!PyWinObject_AsHANDLE(obhIcon, (HANDLE *)&pci->hIcon))
return FALSE;
if (!PyWinObject_AsResourceIdA(obVerb, (char **)&pci->lpVerb))
return FALSE;
return TRUE;
}
void PyObject_FreeCMINVOKECOMMANDINFO(CMINVOKECOMMANDINFO *pci) { PyWinObject_FreeResourceIdA((char *)pci->lpVerb); }
static PyObject *PyString_FromMaybeNullString(const char *sz)
{
if (sz)
return PyWinCoreString_FromString(sz);
Py_INCREF(Py_None);
return Py_None;
}
PyObject *PyObject_FromCMINVOKECOMMANDINFO(const CMINVOKECOMMANDINFO *pci)
{
if (!pci) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject *obVerb = PyWinObject_FromRESOURCESTRING(pci->lpVerb);
if (!obVerb)
return NULL;
PyObject *obParams = PyString_FromMaybeNullString(pci->lpParameters);
if (!obParams) {
Py_DECREF(obVerb);
return NULL;
}
PyObject *obDir = PyString_FromMaybeNullString(pci->lpDirectory);
if (!obDir) {
Py_DECREF(obVerb);
Py_DECREF(obParams);
return NULL;
}
return Py_BuildValue("iNNNNiiN", pci->fMask, PyWinLong_FromHANDLE(pci->hwnd), obVerb, obParams, obDir, pci->nShow,
pci->dwHotKey, PyWinLong_FromHANDLE(pci->hIcon));
}
void PyObject_FreeSTRRET(STRRET &s)
{
if (s.uType == STRRET_WSTR) {
CoTaskMemFree(s.pOleStr);
s.pOleStr = NULL;
}
}
PyObject *PyObject_FromSTRRET(STRRET *ps, ITEMIDLIST *pidl, BOOL bFree)
{
if (ps == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject *ret;
switch (ps->uType) {
case STRRET_CSTR:
ret = PyBytes_FromString(ps->cStr);
break;
case STRRET_OFFSET:
ret = PyBytes_FromString(((char *)pidl) + ps->uOffset);
break;
case STRRET_WSTR:
ret = PyWinObject_FromWCHAR(ps->pOleStr);
break;
default:
PyErr_SetString(PyExc_RuntimeError, "unknown uType");
ret = NULL;
break;
}
if (bFree)
PyObject_FreeSTRRET(*ps);
return ret;
}
BOOL PyObject_AsMSG(PyObject *obpmsg, MSG *msg)
{
PyObject *obhwnd;
return PyArg_ParseTuple(obpmsg, "Oiiii(ii)", &obhwnd, &msg->message, &msg->wParam, &msg->lParam, &msg->time,
&msg->pt.x, &msg->pt.y) &&
PyWinObject_AsHANDLE(obhwnd, (HANDLE *)&msg->hwnd);
}
PyObject *PyObject_FromMSG(const MSG *msg)
{
if (!msg) {
Py_INCREF(Py_None);
return Py_None;
}
return Py_BuildValue("Niiii(ii)", PyWinLong_FromHANDLE(msg->hwnd), msg->message, msg->wParam, msg->lParam,
msg->time, msg->pt.x, msg->pt.y);
}
BOOL PyObject_AsFOLDERSETTINGS(PyObject *ob, FOLDERSETTINGS *pf)
{
return PyArg_ParseTuple(ob, "ii", &pf->ViewMode, &pf->fFlags);
}
PyObject *PyObject_FromFOLDERSETTINGS(const FOLDERSETTINGS *pf)
{
if (!pf) {
Py_INCREF(Py_None);
return Py_None;
}
return Py_BuildValue("ii", pf->ViewMode, pf->fFlags);
}
BOOL PyObject_AsRECT(PyObject *ob, RECT *r)
{
return PyArg_ParseTuple(ob, "iiii", &r->left, &r->top, &r->right, &r->bottom);
}
PyObject *PyObject_FromRECT(const RECT *r)
{
if (!r) {
Py_INCREF(Py_None);
return Py_None;
}
return Py_BuildValue("iiii", r->left, r->top, r->right, r->bottom);
}
static BOOL _CopyToWCHAR(PyObject *ob, WCHAR *buf, unsigned buf_size)
{
WCHAR *sz;
if (!PyWinObject_AsWCHAR(ob, &sz, FALSE))
return FALSE;
wcsncpy(buf, sz, buf_size);
buf[buf_size - 1] = L'\0';
PyWinObject_FreeWCHAR(sz);
return TRUE;
}
#define COPY_TO_WCHAR(ob, buf) _CopyToWCHAR((ob), (buf), sizeof((buf)) / sizeof((buf)[0]))
BOOL PyObject_AsSHCOLUMNID(PyObject *ob, SHCOLUMNID *p)
{
PyObject *obGUID;
if (!PyArg_ParseTuple(ob, "Oi:SHCOLUMNID tuple", &obGUID, &p->pid))
return FALSE;
return PyWinObject_AsIID(obGUID, &p->fmtid);
}
PyObject *PyObject_FromSHCOLUMNID(LPCSHCOLUMNID p)
{
if (!p) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject *obIID = PyWinObject_FromIID(p->fmtid);
if (!obIID)
return NULL;
return Py_BuildValue("Ni", obIID, p->pid);
}
BOOL PyObject_AsSHCOLUMNINIT(PyObject *ob, SHCOLUMNINIT *p)
{
PyObject *obName;
if (!PyArg_ParseTuple(ob, "iiO:SHCOLUMNINIT tuple", &p->dwFlags, &p->dwReserved, &obName))
return FALSE;
return COPY_TO_WCHAR(obName, p->wszFolder);
}
PyObject *PyObject_FromSHCOLUMNINIT(LPCSHCOLUMNINIT p)
{
if (!p) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject *obName = PyWinObject_FromWCHAR(p->wszFolder);
if (!obName)
return NULL;
return Py_BuildValue("iiN", p->dwFlags, p->dwReserved, obName);
}
BOOL PyObject_AsSHCOLUMNINFO(PyObject *ob, SHCOLUMNINFO *p)
{
PyObject *obID, *obTitle, *obDescription;
if (!PyArg_ParseTuple(ob, "OiiiiOO:SHCOLUMNINFO tuple", &obID, &p->vt, &p->fmt, &p->cChars, &p->csFlags, &obTitle,
&obDescription))
return FALSE;
if (!PyObject_AsSHCOLUMNID(obID, &p->scid))
return FALSE;
if (!COPY_TO_WCHAR(obTitle, p->wszTitle))
return FALSE;
if (!COPY_TO_WCHAR(obDescription, p->wszDescription))
return FALSE;
return TRUE;
}
PyObject *PyObject_FromSHCOLUMNINFO(LPCSHCOLUMNINFO p)
{
if (!p) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject *rc = NULL, *obID = NULL;
PyObject *obDescription = NULL, *obTitle = NULL;
obID = PyObject_FromSHCOLUMNID(&p->scid);
if (!obID)
goto done;
obTitle = PyWinObject_FromWCHAR(p->wszTitle);
if (!obTitle)
goto done;
obDescription = PyWinObject_FromWCHAR(p->wszDescription);
if (!obDescription)
goto done;
rc = Py_BuildValue("OiiiiOO", obID, p->vt, p->fmt, p->cChars, p->csFlags, obTitle, obDescription);
done:
Py_XDECREF(obID);
Py_XDECREF(obDescription);
Py_XDECREF(obTitle);
return rc;
}
BOOL PyObject_AsSHCOLUMNDATA(PyObject *ob, SHCOLUMNDATA *p)
{
PyObject *obExt, *obFile;
if (!PyArg_ParseTuple(ob, "iiiOO:SHCOLUMNDATA tuple", &p->dwFlags, &p->dwFileAttributes, &p->dwReserved, &obExt,
&obFile))
return FALSE;
if (!PyWinObject_AsWCHAR(obExt, &p->pwszExt, FALSE))
return FALSE;
return COPY_TO_WCHAR(obFile, p->wszFile);
}
void PyObject_FreeSHCOLUMNDATA(SHCOLUMNDATA *p) { PyWinObject_FreeWCHAR(p->pwszExt); }
PyObject *PyObject_FromSHCOLUMNDATA(LPCSHCOLUMNDATA p)
{
if (!p) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject *obFile = PyWinObject_FromWCHAR(p->wszFile);
if (!obFile)
return NULL;
PyObject *obExt = PyWinObject_FromWCHAR(p->pwszExt);
if (!obExt) {
Py_DECREF(obFile);
return NULL;
}
return Py_BuildValue("iiiNN", p->dwFlags, p->dwFileAttributes, p->dwReserved, obExt, obFile);
}
// @object SHFILEINFO|A tuple representing a SHFILEINFO structure
// Represented as a tuple of (hIcon, iIcon, dwAttributes, displayName, typeName)
PyObject *PyObject_FromSHFILEINFO(SHFILEINFO *p)
{
if (!p) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject *obhIcon = PyWinLong_FromHANDLE(p->hIcon);
PyObject *obDisplayName = PyWinObject_FromTCHAR(p->szDisplayName);
PyObject *obTypeName = PyWinObject_FromTCHAR(p->szTypeName);
return Py_BuildValue("NikNN", obhIcon, p->iIcon, p->dwAttributes, obDisplayName, obTypeName);
}
// Note - 'cleanup' as we don't free the object itself, just a couple of
// pointers inside it.
void PyObject_CleanupDEFCONTEXTMENU(DEFCONTEXTMENU *dcm)
{
PY_INTERFACE_PRECALL; // so all ->Releases() happen with GIL released.
if (dcm->pcmcb)
dcm->pcmcb->Release();
if (dcm->psf)
dcm->psf->Release();
if (dcm->punkAssociationInfo)
dcm->punkAssociationInfo->Release();
PY_INTERFACE_POSTCALL;
if (dcm->pidlFolder)
PyObject_FreePIDL(dcm->pidlFolder);
if (dcm->apidl)
PyObject_FreePIDLArray(dcm->cidl, dcm->apidl);
}
// @object DEFCONTENTMENU|A tuple representing a DEFCONTEXTMENU structure.
BOOL PyObject_AsDEFCONTEXTMENU(PyObject *ob, DEFCONTEXTMENU *dcm)
{
BOOL ok = FALSE;
memset(dcm, 0, sizeof(*dcm));
PyObject *obhwnd, *obcb, *obpidlFolder, *obsf, *obpidlChildren, *obai = Py_None, *obkeys = Py_None;
if (!PyArg_ParseTuple(ob, "OOOOO|OO", &obhwnd, &obcb, &obpidlFolder, &obsf, &obpidlChildren, &obai, &obkeys))
return NULL;
// @pyparm <o PyHANDLE>|hwnd||
if (!PyWinObject_AsHANDLE(obhwnd, (HANDLE *)&dcm->hwnd))
goto done;
// @pyparm <o PyIContextMenuCB>|callback||May be None
if (!PyCom_InterfaceFromPyInstanceOrObject(obcb, IID_IContextMenuCB, (void **)&dcm->pcmcb, TRUE /* bNoneOK */))
goto done;
// @pyparm <o PIDL>|pidlFolder||May be None
if (!PyObject_AsPIDL(obpidlFolder, (LPITEMIDLIST *)&dcm->pidlFolder, TRUE))
goto done;
// @pyparm <o PyIShellFolder>|sf||The Shell data source object that is the parent of the child items specified in
// children. If parent is specified, this parameter can be NULL.
if (!PyCom_InterfaceFromPyInstanceOrObject(obsf, IID_IShellFolder, (void **)&dcm->psf, TRUE /* bNoneOK */))
goto done;
// @pyparm [<o PIDL>, ...]|children||
if (!PyObject_AsPIDLArray(obpidlChildren, &dcm->cidl, &dcm->apidl))
goto done;
// @pyparm <o PyIUnknown>|unkAssocInfo||May be None
if (!PyCom_InterfaceFromPyInstanceOrObject(obsf, IID_IUnknown, (void **)&dcm->punkAssociationInfo,
TRUE /* bNoneOK */))
goto done;
if (obkeys != Py_None) {
PyErr_SetString(PyExc_ValueError, "Only None is supported for obKeys");
goto done;
}
ok = TRUE;
done:
if (!ok)
PyObject_CleanupDEFCONTEXTMENU(dcm);
return ok;
}
static BOOL MakeDoubleTerminatedStringList(PyObject *ob, TCHAR **ret)
{
if (ob == Py_None) {
*ret = NULL;
return TRUE;
}
DWORD len;
TCHAR *sz;
if (!PyWinObject_AsTCHAR(ob, &sz, FALSE, &len))
return FALSE;
*ret = (TCHAR *)malloc(sizeof(TCHAR) * (len + 2));
if (!*ret) {
PyWinObject_FreeTCHAR(sz);
PyErr_NoMemory();
return FALSE;
}
memcpy(*ret, sz, sizeof(TCHAR) * (len + 1));
(*ret)[len + 1] = '\0'; // second term.
PyWinObject_FreeTCHAR(sz);
return TRUE;
}
void PyObject_FreeSHFILEOPSTRUCT(SHFILEOPSTRUCT *p)
{
if (p->pFrom)
free((void *)p->pFrom);
if (p->pTo)
free((void *)p->pTo);
if (p->lpszProgressTitle)
PyWinObject_FreeTCHAR((TCHAR *)p->lpszProgressTitle);
if ((p->fFlags & FOF_WANTMAPPINGHANDLE) && (p->hNameMappings != NULL))
SHFreeNameMappings(p->hNameMappings);
}
// @object SHFILEOPSTRUCT|A tuple representing a Win32 shell SHFILEOPSTRUCT structure, used with <om
// shell.SHFileOperation>
// @comm From and To can contain multiple file names concatenated with a single null between them, eg
// "c:\\file1.txt\0c:\\file2.txt". A double null terminator will be appended automatically.
// If To specifies multiple file names, flags must contain FOF_MULTIDESTFILES
BOOL PyObject_AsSHFILEOPSTRUCT(PyObject *ob, SHFILEOPSTRUCT *p)
{
PyObject *obFrom, *obTo, *obNameMappings = Py_None, *obProgressTitle = Py_None, *obhwnd;
memset(p, 0, sizeof(*p));
if (!PyArg_ParseTuple(
ob, "OiOO|iOO",
&obhwnd, // @tupleitem 0|int|hwnd|Handle of window in which to display status messages
&p->wFunc, // @tupleitem 1|int|wFunc|One of the shellcon.FO_* values
&obFrom, // @tupleitem 2|string|From|String containing source file name(s) separated by nulls
&obTo, // @tupleitem 3|string|To|String containing destination file name(s) separated by nulls, can be
// None
&p->fFlags, // @tupleitem 4|int|flags|Combination of shellcon.FOF_* flags. Default=0
&obNameMappings, // @tupleitem 5|None|NameMappings|Maps input file names to their new names. This is
// actually output, and must be None if passed as input. Default=None
&obProgressTitle)) // @tupleitem 6|string|ProgressTitle|Title for progress dialog (flags must contain
// FOF_SIMPLEPROGRESS). Default=None
return FALSE;
if (!PyWinObject_AsHANDLE(obhwnd, (HANDLE *)&p->hwnd))
return NULL;
if (obNameMappings != Py_None) {
PyErr_SetString(PyExc_TypeError, "The NameMappings value must be None");
return FALSE;
}
if (!MakeDoubleTerminatedStringList(obFrom, (LPTSTR *)&p->pFrom))
goto error;
if (!MakeDoubleTerminatedStringList(obTo, (LPTSTR *)&p->pTo))
goto error;
if (!PyWinObject_AsTCHAR(obProgressTitle, (LPTSTR *)&p->lpszProgressTitle, TRUE))
return FALSE;
return TRUE;
error:
PyObject_FreeSHFILEOPSTRUCT(p);
return FALSE;
}
BOOL PyObject_AsEXPLORER_BROWSER_OPTIONS(PyObject *ob, EXPLORER_BROWSER_OPTIONS *ret)
{
*ret = (EXPLORER_BROWSER_OPTIONS)PyLong_AsUnsignedLongMask(ob);
return *ret != -1 || !PyErr_Occurred();
}
PyObject *PyObject_FromEXPLORER_BROWSER_OPTIONS(EXPLORER_BROWSER_OPTIONS val) { return PyLong_FromUnsignedLong(val); }
BOOL PyObject_AsEXPLORER_BROWSER_FILL_FLAGS(PyObject *ob, EXPLORER_BROWSER_FILL_FLAGS *ret)
{
*ret = (EXPLORER_BROWSER_FILL_FLAGS)PyLong_AsUnsignedLongMask(ob);
return *ret != -1 || !PyErr_Occurred();
}
PyObject *PyObject_FromEXPLORER_BROWSER_FILL_FLAGS(EXPLORER_BROWSER_FILL_FLAGS val)
{
return PyLong_FromUnsignedLong(val);
}
void PyObject_FreeASSOCIATIONELEMENTs(ULONG celems, ASSOCIATIONELEMENT *a)
{
for (ULONG i = 0; i < celems; i++)