示例#1
0
        static IEnumerable<ExplorerSelectedFile> GetSelectedItemsOnDesktop()
        {
            //Get total count of the icons on the desktop
            int vItemCount = WinApi.SendMessage(listviewHandle, WinApi.LVM_GETITEMCOUNT, 0, 0);

            uint vProcessId;
            WinApi.GetWindowThreadProcessId(listviewHandle, out vProcessId);

            IntPtr vProcess = WinApi.OpenProcess(WinApi.PROCESS_VM_OPERATION | WinApi.PROCESS_VM_READ |
                                          WinApi.PROCESS_VM_WRITE, false, vProcessId);

            IntPtr vPointer = WinApi.VirtualAllocEx(vProcess, IntPtr.Zero, 4096,
                                             WinApi.MEM_RESERVE | WinApi.MEM_COMMIT, WinApi.PAGE_READWRITE);

            var selectedItems = GetListViewSelectedItems(listviewHandle);

            try
            {
                string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

                for (int j = 0; j < vItemCount; j++)
                {
                    byte[] vBuffer = new byte[256];
                    LVITEM[] vItem = new LVITEM[1];
                    vItem[0].mask = WinApi.LVIF_TEXT;
                    vItem[0].iItem = j;
                    vItem[0].iSubItem = 0;
                    vItem[0].cchTextMax = vBuffer.Length;
                    vItem[0].pszText = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(LVITEM)));

                    uint vNumberOfBytesRead = 0;

                    WinApi.WriteProcessMemory(vProcess, vPointer,
                                       Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0),
                                       Marshal.SizeOf(typeof(LVITEM)), ref vNumberOfBytesRead);

                    WinApi.SendMessage(listviewHandle, WinApi.LVM_GETITEMW, j, vPointer.ToInt32());

                    WinApi.ReadProcessMemory(vProcess,
                                      (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(LVITEM))),
                                      Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),
                                      vBuffer.Length, ref vNumberOfBytesRead);

                    string vText = Encoding.Unicode.GetString(vBuffer, 0,
                                                              (int)vNumberOfBytesRead);

                    string iconName = vText;

                    int index = vText.IndexOf('\0');
                    if (index > -1)
                        iconName = vText.Substring(0, index);

                    //Get icon location
                    WinApi.SendMessage(listviewHandle, WinApi.LVM_GETITEMPOSITION, j, vPointer.ToInt32());

                    Point[] vPoint = new Point[1];
                    WinApi.ReadProcessMemory(vProcess, vPointer,
                                      Marshal.UnsafeAddrOfPinnedArrayElement(vPoint, 0),
                                      Marshal.SizeOf(typeof(Point)), ref vNumberOfBytesRead);

                    Point iconLocation = vPoint[0];
                    bool selected = selectedItems.Contains(j);

                    if (selected)
                        yield return new ExplorerSelectedFile
                                   	{
                                        Filename = iconName,
                                        Location = desktopPath,
                                        Coordinates = iconLocation
                                   	};
                }
            }
            finally
            {
                WinApi.VirtualFreeEx(vProcess, vPointer, 0, WinApi.MEM_RELEASE);
                WinApi.CloseHandle(vProcess);
            }
        }
示例#2
0
        static IEnumerable <ExplorerSelectedFile> GetSelectedItemsOnDesktop()
        {
            //Get total count of the icons on the desktop
            int vItemCount = WinApi.SendMessage(listviewHandle, WinApi.LVM_GETITEMCOUNT, 0, 0);

            uint vProcessId;

            WinApi.GetWindowThreadProcessId(listviewHandle, out vProcessId);

            IntPtr vProcess = WinApi.OpenProcess(WinApi.PROCESS_VM_OPERATION | WinApi.PROCESS_VM_READ |
                                                 WinApi.PROCESS_VM_WRITE, false, vProcessId);

            IntPtr vPointer = WinApi.VirtualAllocEx(vProcess, IntPtr.Zero, 4096,
                                                    WinApi.MEM_RESERVE | WinApi.MEM_COMMIT, WinApi.PAGE_READWRITE);


            var selectedItems = GetListViewSelectedItems(listviewHandle);

            try
            {
                string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

                for (int j = 0; j < vItemCount; j++)
                {
                    byte[]   vBuffer = new byte[256];
                    LVITEM[] vItem   = new LVITEM[1];
                    vItem[0].mask       = WinApi.LVIF_TEXT;
                    vItem[0].iItem      = j;
                    vItem[0].iSubItem   = 0;
                    vItem[0].cchTextMax = vBuffer.Length;
                    vItem[0].pszText    = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(LVITEM)));

                    uint vNumberOfBytesRead = 0;

                    WinApi.WriteProcessMemory(vProcess, vPointer,
                                              Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0),
                                              Marshal.SizeOf(typeof(LVITEM)), ref vNumberOfBytesRead);

                    WinApi.SendMessage(listviewHandle, WinApi.LVM_GETITEMW, j, vPointer.ToInt32());

                    WinApi.ReadProcessMemory(vProcess,
                                             (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(LVITEM))),
                                             Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),
                                             vBuffer.Length, ref vNumberOfBytesRead);

                    string vText = Encoding.Unicode.GetString(vBuffer, 0,
                                                              (int)vNumberOfBytesRead);

                    string iconName = vText;

                    int index = vText.IndexOf('\0');
                    if (index > -1)
                    {
                        iconName = vText.Substring(0, index);
                    }

                    //Get icon location
                    WinApi.SendMessage(listviewHandle, WinApi.LVM_GETITEMPOSITION, j, vPointer.ToInt32());

                    Point[] vPoint = new Point[1];
                    WinApi.ReadProcessMemory(vProcess, vPointer,
                                             Marshal.UnsafeAddrOfPinnedArrayElement(vPoint, 0),
                                             Marshal.SizeOf(typeof(Point)), ref vNumberOfBytesRead);



                    Point iconLocation = vPoint[0];
                    bool  selected     = selectedItems.Contains(j);

                    if (selected)
                    {
                        yield return new ExplorerSelectedFile
                               {
                                   Filename    = iconName,
                                   Location    = desktopPath,
                                   Coordinates = iconLocation
                               }
                    }
                    ;
                }
            }
            finally
            {
                WinApi.VirtualFreeEx(vProcess, vPointer, 0, WinApi.MEM_RELEASE);
                WinApi.CloseHandle(vProcess);
            }
        }