Extraction of icon and text from shortcut on desktop after a drag and drop on my form

  • Thread starter Thread starter Jacky Perpète
  • Start date Start date
J

Jacky Perpète

Guest
Gentlemen,

I try to extraction the real image of icons that can be found on my desktop, because I am having some troubles when getting icons from exe files or lnk files. Sometimes, there are some differences of size and design of image icons.

On my current project, I found on link below a piece of code that doesn't seem to work after some slight modification.

How can i get desktop's icon's information ?

I only changed the structure of LVITEM and the research of the handle "syslistview32" (windows 10 - 64 bits).

After these changes, I can not get the text from the shortcut and the buffer is full of zeros. But, the position of the icon is correctly read.

Any help will be welcomed.

Jacky

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{

public const uint LVM_FIRST = 0x1000;
public const uint LVM_GETITEMCOUNT = LVM_FIRST + 4;
public const uint LVM_GETITEMW = LVM_FIRST + 75;
public const uint LVM_GETITEMPOSITION = LVM_FIRST + 16;
public const uint PROCESS_VM_OPERATION = 0x0008;
public const uint PROCESS_VM_READ = 0x0010;
public const uint PROCESS_VM_WRITE = 0x0020;
public const uint MEM_COMMIT = 0x1000;
public const uint MEM_RELEASE = 0x8000;
public const uint MEM_RESERVE = 0x2000;
public const uint PAGE_READWRITE = 4;
public const int LVIF_TEXT = 0x0001;

[DllImport("kernel32.dll")]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);

[DllImport("kernel32.dll")]
public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint dwFreeType);

[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr handle);

[DllImport("kernel32.dll")]
public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);

[DllImport("kernel32.dll")]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);

[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);

[DllImport("user32.DLL")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

[DllImport("user32.DLL")]
public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);

[DllImport("user32.DLL")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent,
IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint dwProcessId);

[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();

[StructLayout(LayoutKind.Sequential)]
public struct LVITEM
{
public int mask;
public int iItem;
public int iSubItem;
public uint state;
public uint stateMask;
public IntPtr pszText; // string
public int cchTextMax;
public int iImage;
public IntPtr lParam;
public int iIndent;
public int iGroupId;
public int cColumns;
public IntPtr puColumns;
public IntPtr piColFmt;
public int iGroup;
}

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
{
// get the handle of the desktop listview
IntPtr hWorkerW = IntPtr.Zero; ;
IntPtr hShellViewWin;
IntPtr vHandle;
IntPtr hDesktop = GetDesktopWindow();

do
{
hWorkerW = FindWindowEx(hDesktop, hWorkerW, "WorkerW", null);
hShellViewWin = FindWindowEx(hWorkerW, IntPtr.Zero, "SHELLDLL_DefView",null);
} while (hShellViewWin == IntPtr.Zero && hWorkerW != IntPtr.Zero);

vHandle = FindWindowEx(hShellViewWin, IntPtr.Zero, "syslistview32", "FolderView");

//Get total count of the icons on the desktop
int vItemCount = SendMessage(vHandle, LVM_GETITEMCOUNT, 0, 0);
label1.Text = vItemCount.ToString();

uint vProcessId;
GetWindowThreadProcessId(vHandle, out vProcessId);

IntPtr vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, false, vProcessId);
IntPtr vPointer = VirtualAllocEx(vProcess, IntPtr.Zero, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);

try
{
for (int j = 0; j < vItemCount; j++)
{
byte[] vBuffer = new byte[256];
LVITEM[] vItem = new LVITEM[1];
vItem[0].mask = 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)));


var a = Marshal.SizeOf(typeof(LVITEM));

uint vNumberOfBytesRead = 0;
WriteProcessMemory(vProcess, vPointer,Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0),Marshal.SizeOf(typeof(LVITEM)), ref vNumberOfBytesRead);
SendMessage(vHandle, LVM_GETITEMW, j, vPointer.ToInt32());
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;

//Get icon location
SendMessage(vHandle, LVM_GETITEMPOSITION, j, vPointer.ToInt32());
Point[] vPoint = new Point[1];

ReadProcessMemory(vProcess, vPointer, Marshal.UnsafeAddrOfPinnedArrayElement(vPoint, 0), Marshal.SizeOf(typeof(Point)), ref vNumberOfBytesRead);

string IconLocation = vPoint[0].ToString();

//Insert an item into the ListView
listView1.Items.Add(new ListViewItem(new string[] { IconName, IconLocation }));
}
}

finally
{
VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
CloseHandle(vProcess);
}

listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}

}

private void Form1_Load(object sender, EventArgs e)
{
listView1.Columns.Add("Icon Name");
listView1.Columns.Add("Icon Location");
listView1.View = View.Details;
}
}
}

Continue reading...
 

Similar threads

Back
Top