デスクトップのアイコン情報の取得
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
enum WindowMessage
{
WM_KEYDOWN = 0x0100,
}
public const uint PROCESS_VM_OPERATION = 0x8;
public const uint PROCESS_VM_READ = 0x10;
public const uint PROCESS_VM_WRITE = 0x20;
public const uint MEM_RESERVE = 0x2000;
public const uint MEM_COMMIT = 0x1000;
public const uint PAGE_READWRITE = 0x4;
public const int LVM_GETITEM = 0x1005;
public const int LVM_GETITEMPOSITION = 0x1010;
public const int LVM_SETITEMPOSITION = 0x100F;
public const uint MEM_RELEASE = 0x8000;
public const uint LVM_FIRST = 0x1000;
public const uint LVM_GETITEMCOUNT = LVM_FIRST + 4;
public const uint LVM_GETITEMW = LVM_FIRST + 75;
public const int LVIF_TEXT = 0x0001;
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string strclassName, string strWindowName);
[DllImport("user32", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint wMsg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
[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 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);
[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 VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint dwFreeType);
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr hObject);
public struct LVITEM
{
public int mask;
public int iItem;
public int iSubItem;
public int state;
public int stateMask;
public IntPtr pszText;
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;
}
private void button1_Click(object sender, EventArgs e)
{
GetIconPosition();
}
public static int MakeLParam(int wLow, int wHigh)
{
return (((short)wHigh << 16) | (wLow & 0xffff));
}
private void GetIconPosition()
{
IntPtr hWndDesktop;
hWndDesktop = FindWindow("Progman", "Program Manager");
hWndDesktop = FindWindowEx(hWndDesktop, IntPtr.Zero, "SHELLDLL_DefView", null);
hWndDesktop = FindWindowEx(hWndDesktop, IntPtr.Zero, "SysListView32", null);
if (hWndDesktop == IntPtr.Zero)
{
IntPtr hWorkerW = IntPtr.Zero;
IntPtr hShellViewWin = IntPtr.Zero;
do
{
hWorkerW = FindWindowEx(IntPtr.Zero, hWorkerW, "WorkerW", null);
hShellViewWin = FindWindowEx(hWorkerW, IntPtr.Zero, "SHELLDLL_DefView", null);
} while (hShellViewWin == IntPtr.Zero && hWorkerW != IntPtr.Zero);
hWndDesktop = FindWindowEx(hShellViewWin, IntPtr.Zero, "SysListView32", null);
}
if (hWndDesktop == IntPtr.Zero)
{
MessageBox.Show("Desktop hwnd could not be retrieved.", "Error");
return;
}
int iconCount = SendMessage(hWndDesktop, LVM_GETITEMCOUNT, 0, 0);
uint dwProcessId;
GetWindowThreadProcessId(hWndDesktop, out dwProcessId);
IntPtr hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, false, dwProcessId);
if (hProcess == null)
{
MessageBox.Show("Desktop process could not be retrieved.", "Error");
return;
}
IntPtr pProcInfo = VirtualAllocEx(hProcess, IntPtr.Zero, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
for (int i = 0; i < iconCount; i++)
{
byte[] iconNameBytes = new byte[256];
LVITEM[] vItem = new LVITEM[1];
vItem[0].mask = LVIF_TEXT;
vItem[0].iItem = i;
vItem[0].iSubItem = 0;
vItem[0].cchTextMax = iconNameBytes.Length;
vItem[0].pszText = (IntPtr)((int)pProcInfo + Marshal.SizeOf(typeof(LVITEM)));
uint vNumberOfBytesRead = 0;
WriteProcessMemory(hProcess, pProcInfo, Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0), Marshal.SizeOf(typeof(LVITEM)), ref vNumberOfBytesRead);
SendMessage(hWndDesktop, LVM_GETITEMW, i, pProcInfo.ToInt32());
ReadProcessMemory(hProcess, (IntPtr)((int)pProcInfo + Marshal.SizeOf(typeof(LVITEM))), Marshal.UnsafeAddrOfPinnedArrayElement(iconNameBytes, 0), iconNameBytes.Length, ref vNumberOfBytesRead);
string vText = Encoding.Unicode.GetString(iconNameBytes, 0, (int)vNumberOfBytesRead);
int endOfTextIndex = vText.IndexOf('\0');
string IconName = vText.Substring(0, endOfTextIndex);
SendMessage(hWndDesktop, LVM_GETITEMPOSITION, i, pProcInfo.ToInt32());
Point[] vPoint = new Point[1];
ReadProcessMemory(hProcess, pProcInfo, Marshal.UnsafeAddrOfPinnedArrayElement(vPoint, 0), Marshal.SizeOf(typeof(Point)), ref vNumberOfBytesRead);
string IconLocation = vPoint[0].ToString();
SendMessage(hWndDesktop, LVM_GETITEM, i, pProcInfo.ToInt32());
ReadProcessMemory(hProcess, pProcInfo, Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0), Marshal.SizeOf(typeof(LVITEM)), ref vNumberOfBytesRead);
listBox1.Items.Add("location:" + IconLocation + " name:" + IconName + " iItem:" + vItem[0].iItem);
}
VirtualFreeEx(hProcess, pProcInfo, 0, MEM_RELEASE);
CloseHandle(hProcess);
SendMessage(hWndDesktop, (uint)WindowMessage.WM_KEYDOWN, VK_F5, 0);
}