Welcome to mirror list, hosted at ThFree Co, Russian Federation.

native.cs « awt - github.com/mono/ikvm-fork.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b5dee449953860d332128b0a26a24be105d6896e (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
/*
  Copyright (C) 2007, 2008, 2010 Jeroen Frijters
  Copyright (C) 2009 - 2012 Volker Berlin (i-net software)
  Copyright (C) 2010 Karsten Heinrich (i-net software)

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jeroen Frijters
  jeroen@frijters.net
  
*/
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using awt;

namespace IKVM.NativeCode.sun.awt
{
	static class KeyboardFocusManagerPeerImpl
	{
		public static object getNativeFocusedWindow() { return null; }
		public static object getNativeFocusOwner() { return null; }
		public static void clearNativeGlobalFocusOwner(object activeWindow) { }
	}

	static class SunToolkit
	{
		public static void closeSplashScreen() { }
	}
}

namespace IKVM.NativeCode.sun.awt.shell
{
	/// <summary>
	/// This class should use only on Windows that we can access shell32.dll
	/// </summary>
	public static class Win32ShellFolder2
	{
		private const uint IMAGE_BITMAP = 0;
		private const uint IMAGE_ICON = 1;

        private const int HINST_COMMCTRL = -1;
        private const int IDB_VIEW_SMALL_COLOR = 4;

        private const uint WM_USER = 0x0400;
        private const uint TB_GETIMAGELIST = WM_USER + 49;
        private const uint TB_LOADIMAGES = WM_USER + 50;

        private const uint ILD_TRANSPARENT = 0x00000001;

		private static readonly IntPtr hmodShell32;
		private static readonly bool isXP;

		[System.Security.SecuritySafeCritical]
		static Win32ShellFolder2()
		{
			hmodShell32 = LoadLibrary("shell32.dll");
			isXP = Environment.OSVersion.Version >= new Version(5, 1);
		}

		[System.Security.SecurityCritical]
		private sealed class SafeGdiObjectHandle : Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid
		{
			private SafeGdiObjectHandle()
				: base(true)
			{
			}

			[System.Security.SecurityCritical]
			protected override bool ReleaseHandle()
			{
				return DeleteObject(handle);
			}
		}

		[System.Security.SecurityCritical]
		private sealed class SafeDeviceContextHandle : Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid
		{
			[DllImport("user32.dll")]
			private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

			[DllImport("user32.dll")]
			private static extern SafeDeviceContextHandle GetDC(IntPtr hwnd);

			internal static SafeDeviceContextHandle Get()
			{
				return GetDC(IntPtr.Zero);
			}

			private SafeDeviceContextHandle()
				: base(true)
			{
			}

			[System.Security.SecurityCritical]
			protected override bool ReleaseHandle()
			{
				return ReleaseDC(IntPtr.Zero, handle) == 1;
			}
		}

		[DllImport("gdi32.dll")]
		private static extern int GetDIBits(SafeDeviceContextHandle hdc, IntPtr hbmp, uint uStartScan, uint cScanLines, int[] lpvBits, ref BITMAPINFO lpbmi, uint uUsage);

		[DllImport("gdi32.dll")]
		private static extern int GetDIBits(SafeDeviceContextHandle hdc, SafeGdiObjectHandle hbmp, uint uStartScan, uint cScanLines, int[] lpvBits, ref BITMAPINFO lpbmi, uint uUsage);

		[DllImport("gdi32.dll")]
		private static extern int GetObject(SafeGdiObjectHandle hgdiobj, int cbBuffer, ref BITMAPINFO lpvObject);

		[DllImport("shlwapi.dll")]
        private static extern int StrRetToBuf(ref ShellApi.STRRET pstr, IntPtr pIDL, StringBuilder pszBuf, uint cchBuf);

		[StructLayout(LayoutKind.Sequential)]
		private struct ICONINFO
		{
			internal bool fIcon;
			internal int xHotspot;
			internal int yHotspot;
			internal IntPtr hbmMask;
			internal IntPtr hbmColor;
		}

		[DllImport("user32.dll")]
		static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr CreateWindowEx(
           uint dwExStyle,
           string lpClassName,
           string lpWindowName,
           uint dwStyle,
           int x,
           int y,
           int nWidth,
           int nHeight,
           IntPtr hWndParent,
           IntPtr hMenu,
           IntPtr hInstance,
           IntPtr lpParam);

        [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool DestroyWindow(IntPtr hwnd);

        [DllImport("user32.dll")]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);

        [DllImport("comctl32.dll", SetLastError = true)]
        public static extern IntPtr ImageList_GetIcon(IntPtr himl, int i, uint flags);

        [DllImport("comctl32")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool ImageList_Destroy(IntPtr himl);

        [DllImport("user32")]
        public static extern int DestroyIcon(IntPtr hIcon);

        [StructLayout(LayoutKind.Sequential)]
		private struct BITMAPINFO
		{
			internal uint biSize;
			internal int biWidth;
			internal int biHeight;
			internal ushort biPlanes;
			internal ushort biBitCount;
			internal uint biCompression;
			internal uint biSizeImage;
			internal int biXPelsPerMeter;
			internal int biYPelsPerMeter;
			internal uint biClrUsed;
			internal uint biClrImportant;
			[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
			internal uint[] cols;
		}

		[DllImport("user32.dll")]
		private static extern SafeGdiObjectHandle LoadImage(IntPtr hInstance, IntPtr uID, uint type, int width, int height, int load);

		[DllImport("user32.dll")]
		private static extern SafeGdiObjectHandle LoadImage(IntPtr hInstance, string lpszName, uint type, int width, int height, int load);

		[DllImport("kernel32.dll")]
		private static extern IntPtr LoadLibrary(string Library);

		[DllImport("gdi32.dll")]
		private static extern bool DeleteObject(IntPtr hDc);

		/// <summary>
		/// Get the program to execute or open the file. If it is a exe then it is self
		/// </summary>
		/// <param name="path">path to the file</param>
		/// <returns></returns>
		[System.Security.SecuritySafeCritical]
		public static string getExecutableType(string path)
		{
			StringBuilder objResultBuffer = new StringBuilder(1024);
			int result = ShellApi.FindExecutable(path, path, objResultBuffer);
			if (result >= 32)
			{
				return objResultBuffer.ToString();
			}
			return null;
		}

		/// <summary>
		/// Get the type of a file or folder. On a file it depends on its extension.
		/// </summary>
		/// <param name="path">the path of the file or folder</param>
		/// <returns>The type in readable form or null, if the path cannot be resolved</returns>
		[System.Security.SecuritySafeCritical]
		public static string getFolderType(string path)
		{
            ShellApi.SHFILEINFO shinfo = new ShellApi.SHFILEINFO();
			if (ShellApi.SHGetFileInfo(path, 0, out shinfo, (uint)Marshal.SizeOf(shinfo), ShellApi.SHGFI.SHGFI_TYPENAME) == IntPtr.Zero)
			{
				return null;
			}
			return shinfo.szTypeName;
		}

		[System.Security.SecurityCritical]
        public static Bitmap getIconBits(IntPtr hIcon, int iconSize)
		{
			ICONINFO iconInfo;
			if (GetIconInfo(hIcon, out iconInfo))
			{
				using (SafeDeviceContextHandle dc = SafeDeviceContextHandle.Get())
				{
					BITMAPINFO bmi = new BITMAPINFO();
					bmi.biSize = 40;
					bmi.biWidth = iconSize;
					bmi.biHeight = -iconSize;
					bmi.biPlanes = 1;
					bmi.biBitCount = 32;
					bmi.biCompression = 0;
					int intArrSize = iconSize * iconSize;
					int[] iconBits = new int[intArrSize];
					GetDIBits(dc, iconInfo.hbmColor, 0, (uint)iconSize, iconBits, ref bmi, 0);
					bool hasAlpha = false;
					if (isXP)
					{
						for (int i = 0; i < iconBits.Length; i++)
						{
							if ((iconBits[i] & 0xFF000000) != 0)
							{
								hasAlpha = true;
								break;
							}
						}
					}
					if (!hasAlpha)
					{
						int[] maskBits = new int[intArrSize];
						GetDIBits(dc, iconInfo.hbmMask, 0, (uint)iconSize, maskBits, ref bmi, 0);
						for (int i = 0; i < iconBits.Length; i++)
						{
							if (maskBits[i] == 0)
							{
								iconBits[i] = (int)((uint)iconBits[i] | 0xFF000000);
							}
						}
					}
					DeleteObject(iconInfo.hbmColor);
					DeleteObject(iconInfo.hbmMask);

                    DeleteObject(hIcon);
                    Bitmap bitmap = new Bitmap(iconSize, iconSize, PixelFormat.Format32bppArgb);
                    BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, iconSize, iconSize), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
                    Marshal.Copy(iconBits, 0, bitmapData.Scan0, iconBits.Length);
                    bitmap.UnlockBits(bitmapData);
                    return bitmap;
				}
			}
			return null;
		}

        /// <summary>
        /// Retrieves information about an object in the file system, such as a file, folder, directory, or drive root.
        /// </summary>
        /// <param name="path">The path of the file system object</param>
        /// <returns>The SHGFI flags with the attributes</returns>
		[System.Security.SecuritySafeCritical]
		public static int getAttribute(string path)
		{
            ShellApi.SHFILEINFO shinfo = new ShellApi.SHFILEINFO();
            if (ShellApi.SHGetFileInfo(path, 0, out shinfo, (uint)Marshal.SizeOf(shinfo), ShellApi.SHGFI.SHGFI_ATTRIBUTES) == IntPtr.Zero)
			{
				return 0;
			}
			return (int)shinfo.dwAttributes;
		}

        /// <summary>Returns the link target as a pIDL relative to the desktop without resolving the link</summary>
        /// <param name="path">The path of the .lnk file</param>
        /// <returns>the link target as a pIDL relative to the desktop</returns>
		[System.Security.SecuritySafeCritical]
		public static string getLinkLocation(string path )
		{
			using (ShellLink link = new ShellLink())
			{
				link.Load(path);
				return link.GetPath();
			}
		}
        /// <summary>Returns the link target as a pIDL relative to the desktop</summary>
        /// <param name="path">The path of the .lnk file</param>
        /// <param name="resolve">If true, attempts to find the target of a Shell link, 
        /// even if it has been moved or renamed. This may open a file chooser</param>
        /// <returns>the link target as a pIDL relative to the desktop</returns>
        [System.Security.SecuritySafeCritical]
        public static IntPtr getLinkLocation(string path, Boolean resolve)
        {
            using (ShellLink link = new ShellLink())
            {
                link.Load(path);
                if (resolve)
                {
                    link.Resolve();
                }                
                return link.GetIDList();
            }
        }

        // Code copied from Java_sun_awt_shell_Win32ShellFolder2_getStandardViewButton0
        [System.Security.SecuritySafeCritical]
        public static Bitmap getStandardViewButton0(int iconIndex)
        {
            Bitmap result = null;
            using (new ThemingActivationContext())
            {
                // Create a toolbar
                IntPtr hWndToolbar = CreateWindowEx(0, "ToolbarWindow32", null, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                if (hWndToolbar != IntPtr.Zero)
                {
                    SendMessage(hWndToolbar, TB_LOADIMAGES, IDB_VIEW_SMALL_COLOR, HINST_COMMCTRL);

                    IntPtr hImageList = SendMessage(hWndToolbar, TB_GETIMAGELIST, 0, 0);
                    if (hImageList != IntPtr.Zero)
                    {
                        IntPtr hIcon = ImageList_GetIcon(hImageList, iconIndex, ILD_TRANSPARENT);
                        if (hIcon != IntPtr.Zero)
                        {
                            Icon icon = Icon.FromHandle(hIcon);
                            result = icon.ToBitmap();
                            icon.Dispose();
                            DestroyIcon(hIcon);
                        }
                        ImageList_Destroy(hImageList);
                    }
                    DestroyWindow(hWndToolbar);
                }
            }
            return result;
        }

        /// <summary>
        /// Retrieves an icon from the shell32.dell
        /// </summary>
        /// <param name="iconID">the index of the icon</param>
        /// <returns>The icon or null, if there is no icon at the given index</returns>
		[System.Security.SecuritySafeCritical]
        public static Bitmap getShell32IconResourceAsBitmap(int iconID, bool getLargeIcon)
		{
			if (hmodShell32 == IntPtr.Zero)
			{
				return null;
			}
            int size = getLargeIcon ? 32 : 16;
            using (SafeGdiObjectHandle hicon = LoadImage(hmodShell32, (IntPtr)iconID, IMAGE_ICON, size, size, 0))
			{
				if (hicon != null)
				{
                    return getIconBits(hicon.DangerousGetHandle(), 16);
				}
			}
			return null;
		}

        /// <summary>
        /// Returns the pIDL of the desktop itself
        /// </summary>
        [System.Security.SecurityCritical]
        public static IntPtr initDesktopPIDL()
        {
            IntPtr pidl = new IntPtr();

            // get the root shell folder
            ShellApi.SHGetSpecialFolderLocation(IntPtr.Zero, ShellApi.CSIDL.CSIDL_DESKTOP, ref pidl);

            return pidl;
        }

        /// <summary>
        /// Returns an IShellFolder for the desktop
        /// </summary>
        [System.Security.SecurityCritical]
        public static Object initDesktopFolder()
        {
            ShellApi.IShellFolder rootShell = null;

            // get the root shell folder
            ShellApi.SHGetDesktopFolder(ref rootShell);

            return rootShell;
        }

        /// <summary>
        /// Returns the desktop relative pIDL of a special folder
        /// </summary>
        /// <param name="desktopIShellFolder">The IShellFolder instance of the Desktop</param>
        /// <param name="csidl">The CSIDL of the special folder</param>
        /// <returns>the desktop relative pIDL of a special folder</returns>
        [System.Security.SecurityCritical]
        public static IntPtr initSpecialPIDL(Object desktopIShellFolder, int csidl)
        {
            IntPtr result = new IntPtr();        
            ShellApi.SHGetSpecialFolderLocation(IntPtr.Zero, (ShellApi.CSIDL)csidl, ref result);
            return result;
        }

        /// <summary>
        /// Creates an IShellFolder for a special folder
        /// </summary>
        /// <param name="desktopIShellFolder">The IShellFolder instance of the Desktop</param>
        /// <param name="pidl">The desktop relative pIDL of the special folder</param>
        /// <returns>The IShellFolder for a special folder</returns>
        [System.Security.SecurityCritical]
        public static Object initSpecialFolder(Object desktopIShellFolder, IntPtr pidl)
        {
            try
            {
                // get desktop instance
                ShellApi.IShellFolder desktop = (ShellApi.IShellFolder)desktopIShellFolder;
                // call BindToObject of the desktop
                ShellApi.IShellFolder specialFolder = null;
                desktop.BindToObject(pidl, IntPtr.Zero, ref ShellApi.GUID_ISHELLFOLDER, out specialFolder);
                return specialFolder;
            }
            catch (System.ArgumentException )
            {
                return 0;
            }
        }

        /// <summary>
        /// Goes down one entry in the given pIDL
        /// </summary>
        /// <param name="pIDL">the pIDL to operate on</param>
        /// <returns>the next entry in the pIDL</returns>
		[System.Security.SecurityCritical]
		public static IntPtr getNextPIDLEntry(IntPtr pIDL)
        {
            if (pIDL == IntPtr.Zero)
            {
                return IntPtr.Zero;
            }

            int length = Marshal.ReadInt16(pIDL);
            if (length == 0) // defined as terminator of a ITEMIDLIST
            {
                return IntPtr.Zero;
            }
            IntPtr newpIDL = new IntPtr(pIDL.ToInt64() + length);
            if (Marshal.ReadInt16(newpIDL) == 0)
            {
                return IntPtr.Zero;
            }
            else
            {
                return newpIDL;
            }
        }
        /// <summary>
        /// Copies the first entry in the given pIDL into a new relative pIDL (with terminator)
        /// </summary>
        /// <param name="pIDL">The pIDL to copy from</param>
        /// <returns>the relative pIDL of the first entry</returns>
		[System.Security.SecurityCritical]
		public static IntPtr copyFirstPIDLEntry(IntPtr pIDL)
        {
            if (pIDL == IntPtr.Zero)
            {
                return IntPtr.Zero;
            }
            int length = Marshal.ReadInt16(pIDL) + 2; // +2 for the terminator
            byte[] buffer = new byte[length];
            IntPtr newpIDLptr = Marshal.AllocCoTaskMem(length); // create pointer to new pIDL
            Marshal.Copy(pIDL, buffer, 0, length - 2); // copy content
            Marshal.Copy(buffer, 0, newpIDLptr, length); // copy content
            return newpIDLptr;
        }

        /// <summary>
        /// Concatinates two pIDLs
        /// </summary>
        /// <param name="ppIDL">a pIDL, if IntPtr.Zero, IntPtr.Zero will be returned</param>
        /// <param name="pIDL">a pIDL, if IntPtr.Zero, IntPtr.Zero will be returned</param>
        /// <returns>the concatination of ppIDL and pIDL</returns>
		[System.Security.SecurityCritical]
		public static IntPtr combinePIDLs(IntPtr ppIDL, IntPtr pIDL)
        {
            if (ppIDL == IntPtr.Zero)
            {
                return IntPtr.Zero;
            }
            if (pIDL == IntPtr.Zero)
            {
                return IntPtr.Zero;
            }
            int lengthP = getPIDLlength(ppIDL);
            int lengthR = getPIDLlength(pIDL);
            byte[] newPIDL = new byte[lengthP + lengthR + 2];
            Marshal.Copy(ppIDL, newPIDL, 0, lengthP);
            Marshal.Copy(pIDL, newPIDL, lengthP, lengthR);
            IntPtr newpIDLptr = Marshal.AllocCoTaskMem(lengthP + lengthR + 2); // create pointer to new pIDL
            Marshal.Copy(newPIDL,0,newpIDLptr,newPIDL.Length); // set pointer to new pIDL and delete local structure
            return newpIDLptr;
        }

        /// <summary>
        /// Calculates the size of a ITEMIDLIST without the two bytes of the terminator
        /// </summary>
        /// <param name="pIDL">a pointer to the IDL to get the length of</param>
        /// <returns>the length in bytes</returns>
		[System.Security.SecurityCritical]
		public static int getPIDLlength(IntPtr pIDL)
        {
            if( pIDL == IntPtr.Zero )
            {
                return 0;
            }
            int length = Marshal.ReadInt16(pIDL);
            int offset = length;
            while (length > 0)
            {
                length = Marshal.ReadInt16(new IntPtr( pIDL.ToInt64() + offset));
                offset += length;
            }
            return offset;
        }
        /// <summary>
        /// Releases the allocted memory of a pIDL
        /// </summary>
        /// <param name="pIDL">The pIDL to be released</param>
		[System.Security.SecurityCritical]
		public static void releasePIDL(IntPtr pIDL)
        {
            if (pIDL == IntPtr.Zero)
            {
                return;
            }
            Marshal.Release(pIDL);
        }

		/// <summary>
        /// Releases an IShellFolder COM object
        /// </summary>
        /// <param name="pIShellFolder">The IShellFolder to be released, must not be null</param>
		[System.Security.SecurityCritical]
		public static void releaseIShellFolder(Object pIShellFolder)
        {
            if (pIShellFolder == null)
            {
                return;
            }
            Marshal.ReleaseComObject(pIShellFolder);
        }

		[System.Security.SecurityCritical]
		public static int compareIDs(Object pParentIShellFolder, IntPtr pidl1, IntPtr pidl2)
        {
            if (pParentIShellFolder == null)
            {
                return 0;
            }
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)pParentIShellFolder;
            return folder.CompareIDs(0, pidl1, pidl2);
        }

		[System.Security.SecurityCritical]
		public static int getAttributes0(Object pParentIShellFolder, IntPtr pIDL, int attrsMask)
        {
            if (pParentIShellFolder == null || pIDL == IntPtr.Zero )
            {
                return 0;
            }
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)pParentIShellFolder;
            ShellApi.SFGAOF[] atts = new ShellApi.SFGAOF[]{ (ShellApi.SFGAOF)attrsMask };
            IntPtr[] pIDLs = new IntPtr[] { pIDL };
            folder.GetAttributesOf(1, pIDLs, atts);
            return (int)atts[0];
        }

        [System.Security.SecurityCritical]
        public static String getFileSystemPath(int csidl)
        {
            IntPtr pIDL = new IntPtr();
            int hRes = ShellApi.SHGetSpecialFolderLocation(IntPtr.Zero, (ShellApi.CSIDL)csidl, ref pIDL);
            if (hRes != 0)
            {
                //throw Marshal.ThrowExceptionForHR(hRes);
                // TODO exception for hRes
                return null;
            }
            StringBuilder builder = new StringBuilder( 1024 );
            if (ShellApi.SHGetPathFromIDList(pIDL, builder))
            {
                return builder.ToString();
            }
            else
            {
                return null;
            }
        }

        [System.Security.SecurityCritical]
        public static Object getEnumObjects(Object pIShellFolder, Boolean isDesktop, Boolean includeHiddenFiles)
        {
            if (pIShellFolder == null)
            {
                return null;
            }
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)pIShellFolder;
            ShellApi.SHCONTF flags = ShellApi.SHCONTF.SHCONTF_FOLDERS | ShellApi.SHCONTF.SHCONTF_NONFOLDERS;
            if( includeHiddenFiles )
            {
                flags |= ShellApi.SHCONTF.SHCONTF_INCLUDEHIDDEN;
            }

            ShellApi.IEnumIDList list = null;
            folder.EnumObjects(IntPtr.Zero, flags, out list);
            return list;
        }

        /// <summary>
        /// Returns the next pIDL in an IEnumIDList
        /// </summary>
        /// <param name="pEnumObjects">The IEnumIDList to get the next element of</param>
        /// <returns>a pIDL or IntPtr.Zero in case the end of the enum is reached</returns>
        [System.Security.SecurityCritical]
        public static IntPtr getNextChild(Object pEnumObjects)
        {
            if (pEnumObjects == null)
            {
                return IntPtr.Zero;
            }
            ShellApi.IEnumIDList list = (ShellApi.IEnumIDList)pEnumObjects;
            IntPtr pIDL = new IntPtr();
            int pceltFetched; // can be ignored, if celt = 1
            uint hRes = list.Next(1, out pIDL, out pceltFetched);
            if ( hRes != 0 || pceltFetched == 0 )
            {
                return IntPtr.Zero;
            }
            else
            {
                return pIDL;
            }
        }

        /// <summary>
        /// Releases an IEnumIDList
        /// </summary>
        /// <param name="pEnumObjects">The IEnumIDList to be released</param>
		[System.Security.SecurityCritical]
		public static void releaseEnumObjects(Object pEnumObjects)
        {
            if (pEnumObjects != null)
            {
                Marshal.ReleaseComObject(pEnumObjects);
            }
        }

        /// <summary>
        /// Binds an IShellFolder to the child of a given shell folder
        /// </summary>
        /// <param name="parentIShellFolder">the parent IShellFolder</param>
        /// <param name="pIDL">the relative pIDL to the child</param>
        /// <returns>The IShellFolder of the child or null, if there is no such child</returns>
		[System.Security.SecurityCritical]
		public static Object bindToObject(Object parentIShellFolder, IntPtr pIDL)
        {
            if (parentIShellFolder == null || pIDL == IntPtr.Zero )
            {
                return null;
            }
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)parentIShellFolder;
            ShellApi.IShellFolder newFolder = null;
            folder.BindToObject(pIDL, IntPtr.Zero, ref ShellApi.GUID_ISHELLFOLDER, out newFolder);
            return newFolder;
        }

        /// <summary>
        /// Parses the displayname of a child of a given folder
        /// </summary>
        /// <param name="pIShellFolder">The IShellFolder to get the chilf of</param>
        /// <param name="name">The display name of the child</param>
        /// <returns>the relative pIDL of the child or IntPrt.Zero in case there is no such child</returns>
        [System.Security.SecurityCritical]
        public static IntPtr parseDisplayName0(Object pIShellFolder, String name)
        {
            if (pIShellFolder == null)
            {
                return IntPtr.Zero;
            }
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)pIShellFolder;
            IntPtr pIDL = new IntPtr();
            uint pchEaten;
            uint pdwAttribute = 0;
            folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, name, out pchEaten, out pIDL, ref pdwAttribute);
            return pIDL;
        }

		[System.Security.SecurityCritical]
		public static String getDisplayNameOf(Object parentIShellFolder, IntPtr relativePIDL, int attrs)
        {
            if (parentIShellFolder == null || relativePIDL == IntPtr.Zero)
            {
                return null;
            }
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)parentIShellFolder;
            ShellApi.STRRET result;
            uint hRes = folder.GetDisplayNameOf(relativePIDL, (ShellApi.SHGDN)attrs, out result);
            if ( hRes == 0 )
            {
                StringBuilder name = new StringBuilder( 1024 );
                StrRetToBuf(ref result, relativePIDL, name, 1024);
                string stringName = name.ToString();
                return stringName;
            }
            return null;
        }

		[System.Security.SecurityCritical]
		public static String getFolderType(IntPtr pIDL)
        {
            ShellApi.SHFILEINFO fileInfo = new ShellApi.SHFILEINFO();
            ShellApi.SHGetFileInfo(pIDL, 0, out fileInfo, (uint)Marshal.SizeOf(fileInfo), ShellApi.SHGFI.SHGFI_PIDL | ShellApi.SHGFI.SHGFI_TYPENAME);
            return fileInfo.szTypeName;
        }

        public static Object getIShellIcon(Object pIShellFolder)
        {
            if (pIShellFolder is ShellApi.IShellIcon)
            {
                return pIShellFolder;
            }
            return null;
        }

		[System.Security.SecurityCritical]
		public static int getIconIndex(Object parentIShellFolder, IntPtr relativePIDL)
        {
            if (parentIShellFolder is ShellApi.IShellIcon)
            {
                ShellApi.IShellIcon shellIcon = (ShellApi.IShellIcon)parentIShellFolder;
                int index = 0;
                if( shellIcon.GetIconOf(relativePIDL, (uint)ShellApi.GIL.GIL_FORSHELL, out index) == 0 )
                {
                    return index;
                }
            }
            return 0;
        }

        [System.Security.SecurityCritical]
        public static IntPtr getIcon(String absolutePath, Boolean getLargeIcon) 
        {
            ShellApi.SHFILEINFO shinfo = new ShellApi.SHFILEINFO();
            if (ShellApi.SHGetFileInfo(absolutePath, 0, out shinfo, (uint)Marshal.SizeOf(shinfo), ShellApi.SHGFI.SHGFI_ICON | (getLargeIcon ? ShellApi.SHGFI.SHGFI_LARGEICON : ShellApi.SHGFI.SHGFI_SMALLICON)) == IntPtr.Zero)
            {
                return IntPtr.Zero;
            }
            return shinfo.hIcon;
        }

		[System.Security.SecurityCritical]
		public static IntPtr extractIcon(Object parentIShellFolder, IntPtr relativePIDL, Boolean getLargeIcon)
        {
            if (parentIShellFolder == null || relativePIDL == IntPtr.Zero)
            {
                return IntPtr.Zero;
            }
            ShellApi.IShellFolder folder = (ShellApi.IShellFolder)parentIShellFolder;
            Guid guid = new Guid("000214fa-0000-0000-c000-000000000046");
            object ppv;
            if (folder.GetUIObjectOf(IntPtr.Zero, 1, new IntPtr[] { relativePIDL }, ref guid, IntPtr.Zero, out ppv) == 0)
            {
                ShellApi.IExtractIcon extractor = (ShellApi.IExtractIcon)ppv;
                int size = 1024;
                StringBuilder path = new StringBuilder( size );
                int piIndex;
                uint pwFlags;
                if (extractor.GetIconLocation((uint)ShellApi.GIL.GIL_FORSHELL, path, size, out piIndex, out pwFlags) == 0)
                {
                    IntPtr hIconL = new IntPtr();
                    IntPtr hIconS = new IntPtr();
                    if (extractor.Extract(path.ToString(), (uint)piIndex, out hIconL, out hIconS, (16 << 16) + 32) == 0)
                    {
                        if (getLargeIcon)
                        {
                            ShellApi.DestroyIcon(hIconS);
                            return hIconL;
                        }
                        else
                        {
                            ShellApi.DestroyIcon(hIconL);
                            return hIconS;
                        }
                    }
                }
            }
            return IntPtr.Zero;
        }

		[System.Security.SecurityCritical]
		public static void disposeIcon(IntPtr hIcon)
        {
            ShellApi.DestroyIcon(hIcon);
        }

		public static Object doGetColumnInfo(Object iShellFolder2)
        {
            // TODO Dummy
            return null;
        }

		public static Object doGetColumnValue(Object parentIShellFolder2, IntPtr childPIDL, int columnIdx)
        {
            // TODO Dummy
            return null;
        }

		public static int compareIDsByColumn(Object pParentIShellFolder, IntPtr pidl1, IntPtr pidl2, int columnIdx)
        {
            // TODO Dummy
            return 0;
        }
	}

	[System.Security.SecurityCritical]
	class ShellLink : IDisposable
	{
		[ComImport]
		[Guid("0000010B-0000-0000-C000-000000000046")]
		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		internal interface IPersistFile
		{
			[PreserveSig]
			void GetClassID(out Guid pClassID);
			[PreserveSig]
			void IsDirty();
			[PreserveSig]
			void Load([MarshalAs(UnmanagedType.LPWStr)] string pszFileName, uint dwMode);
			[PreserveSig]
			void Save([MarshalAs(UnmanagedType.LPWStr)] string pszFileName, [MarshalAs(UnmanagedType.Bool)] bool fRemember);
			[PreserveSig]
			void SaveCompleted([MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
			[PreserveSig]
			void GetCurFile([MarshalAs(UnmanagedType.LPWStr)] out string ppszFileName);
		}

		[ComImport]
		[Guid("000214F9-0000-0000-C000-000000000046")]
		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
		private interface IShellLinkW
		{
			void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, IntPtr pfd, uint fFlags);
			void GetIDList(out IntPtr ppidl);
			void SetIDList(IntPtr pidl);
			void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxName);
			void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
			void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
			void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
			void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
			void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
			void GetHotkey(out short pwHotkey);
			void SetHotkey(short pwHotkey);
			void GetShowCmd(out uint piShowCmd);
			void SetShowCmd(uint piShowCmd);
			void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon);
			void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
			void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, uint dwReserved);
            /// <summary>Attempts to find the target of a Shell link, even if it has been moved or renamed.</summary>
			void Resolve(IntPtr hWnd, uint fFlags);
			void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
		}

		[Guid("00021401-0000-0000-C000-000000000046")]
		[ClassInterfaceAttribute(ClassInterfaceType.None)]
		[ComImport]
		private class CShellLink { }

		[Flags]
		public enum EShowWindowFlags : uint
		{
			SW_HIDE = 0,
			SW_SHOWNORMAL = 1,
			SW_NORMAL = 1,
			SW_SHOWMINIMIZED = 2,
			SW_SHOWMAXIMIZED = 3,
			SW_MAXIMIZE = 3,
			SW_SHOWNOACTIVATE = 4,
			SW_SHOW = 5,
			SW_MINIMIZE = 6,
			SW_SHOWMINNOACTIVE = 7,
			SW_SHOWNA = 8,
			SW_RESTORE = 9,
			SW_SHOWDEFAULT = 10,
			SW_MAX = 10
		}

		private IShellLinkW linkW = (IShellLinkW)new CShellLink();

		[System.Security.SecuritySafeCritical]
		public void Dispose()
		{
			if (linkW != null)
			{
				Marshal.ReleaseComObject(linkW);
				linkW = null;
			}
		}

		public void SetPath(string path)
		{
			linkW.SetPath(path);
		}

		public void SetDescription(string description)
		{
			linkW.SetDescription(description);
		}

		public void SetWorkingDirectory(string dir)
		{
			linkW.SetWorkingDirectory(dir);
		}

		public void SetArguments(string args)
		{
			linkW.SetArguments(args);
		}

		public void SetShowCmd(EShowWindowFlags cmd)
		{
			linkW.SetShowCmd((uint)cmd);
		}

		public void Save(string linkFile)
		{
			((IPersistFile)linkW).Save(linkFile, true);
		}

		public void Load(string linkFile)
		{
			((IPersistFile)linkW).Load(linkFile, 0);
		}

		public string GetArguments()
		{
			StringBuilder sb = new StringBuilder(512);
			linkW.GetArguments(sb, sb.Capacity);
			return sb.ToString();
		}

        public void Resolve()
        {
            linkW.Resolve(IntPtr.Zero, 0);
        }

        public IntPtr GetIDList(){
            IntPtr ppidl;
            linkW.GetIDList( out ppidl );
            return ppidl;
        }

		public string GetPath()
		{
			StringBuilder sb = new StringBuilder(512);
			linkW.GetPath(sb, sb.Capacity, IntPtr.Zero, 0);
			return sb.ToString();
		}
	}


}

namespace IKVM.NativeCode.sun.java2d
{
	static class DefaultDisposerRecord
	{
		public static void invokeNativeDispose(long disposerMethodPointer, long dataPointer)
		{
			throw new NotImplementedException();
		}
	}

	static class Disposer
	{
		public static void initIDs()
		{
		}
	}
}

namespace IKVM.NativeCode.sun.java2d.pipe
{
	static class Region
	{
		public static void initIDs() { }
	}

	static class RenderBuffer
	{
		public static void copyFromArray(object srcArray, long srcPos, long dstAddr, long length)
		{
			throw new NotImplementedException();
		}
	}
}