Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Microsoft.Win32;
- using System.Runtime.InteropServices;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Diagnostics;
- using System.Windows.Forms;
- using System.Drawing;
- using System.Threading;
- namespace desktopIconTest
- {
- class Program
- {
- //private static readonly DesktopShortcutCollection _desktopShortcuts;
- static bool prank = false;
- static bool debug = false;
- private const int WM_COPYDATA = 0x004a;
- private const int WM_USER = 0x400;
- private const int LVM_SETITEMPOSITION = 0x1000 + 15;
- private const int LVM_FIRST = 0x1000;
- private const int LVM_GETITEMCOUNT = LVM_FIRST + 4;
- private const int LVM_GETITEM = LVM_FIRST + 75;
- private const int LVM_GETITEMTEXTW = (LVM_FIRST + 115);
- private const int LVIF_TEXT = 0x0001;
- private const uint LVM_GETITEMW = LVM_FIRST + 75;
- private const uint LVM_GETITEMPOSITION = LVM_FIRST + 16;
- private const uint PROCESS_VM_OPERATION = 0x0008;
- private const uint PROCESS_VM_READ = 0x0010;
- private const uint PROCESS_VM_WRITE = 0x0020;
- private const uint MEM_COMMIT = 0x1000;
- private const uint MEM_RELEASE = 0x8000;
- private const uint MEM_RESERVE = 0x2000;
- private const uint PAGE_READWRITE = 4;
- private const int SW_HIDE = 0;
- private const string TITLE = "ForceIcon";
- static string arg = "";
- static void Main(string[] args)
- {
- //HideWindow();
- //arg = (args.Length > 0) ? args[0] : "";
- //arg = "test";
- //ForceIcon();
- //Thread.Sleep(30000);
- HideWindow();
- while (true)
- {
- ForceIcon();
- Thread.Sleep(30000);
- }
- }
- static void HideWindow()
- {
- Console.Title = TITLE;
- IntPtr hWnd = FindWindow(null, TITLE);
- //if (hWnd != IntPtr.Zero && arg != "test")
- //ShowWindow(hWnd, SW_HIDE);
- //else
- //{
- // Console.WriteLine("Running in test mode");
- // debug = true;
- //}
- }
- static void ForceIcon()
- {
- try
- {
- int SSP = -1;
- //Console.WriteLine("vHandle:");
- IntPtr vHandle = FindWindow("Progman", "Program Manager");
- //Console.WriteLine(vHandle);
- vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
- //Console.WriteLine(vHandle);
- vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SysListView32", "FolderView");
- //Console.WriteLine(vHandle);
- int right = Screen.PrimaryScreen.WorkingArea.Right - 64, down = 26;
- //Console.WriteLine("right = " + right);
- //Console.WriteLine("down = " + down);
- int vItemCount = SendMessage(vHandle, LVM_GETITEMCOUNT, 0, 0);
- //Console.WriteLine("vItemCount = " + vItemCount);
- uint vProcessId;
- GetWindowThreadProcessId(vHandle, out vProcessId);
- //Console.WriteLine("vProcessId = " + vProcessId);
- IntPtr vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, false, vProcessId);
- //Console.WriteLine("vProcess = " + vProcess);
- IntPtr vPointer = VirtualAllocEx(vProcess, IntPtr.Zero, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
- //Console.WriteLine("vPointer = " + vPointer);
- //Random r = new Random();
- //r.Next();
- Console.WriteLine(vItemCount);
- for (int j = 0; j < vItemCount; j++)
- {
- LVITEM[] vItem = new LVITEM[1];
- byte[] vBuffer = new byte[256];
- 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)));
- //vItem[0].pszText = Marshal.AllocHGlobal(512);
- 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 = Marshal.PtrToStringAnsi(vItem[0].pszText);
- //Get icon location
- Console.WriteLine("Getting icon location: " + IconName + vText);
- 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();
- Console.WriteLine(IconLocation);
- //Console.WriteLine(IconName);
- if (IconName.Contains("Self Service Portal"))
- SSP = j;
- //if (prank)
- //{
- // int rright = r.Next(0, Screen.PrimaryScreen.WorkingArea.Right);
- // int rdown = r.Next(0, Screen.PrimaryScreen.WorkingArea.Bottom);
- // SendMessage(vHandle, LVM_SETITEMPOSITION, j, MakeLParam(rright, rdown));
- //}
- if (SSP > -1)
- {
- Console.WriteLine("Moving Icon");
- SendMessage(vHandle, LVM_SETITEMPOSITION, SSP, MakeLParam(right, down));
- }
- SSP = -1;
- //vItem[0].mask = 0;
- //vItem[0].iItem = 0;
- //vItem[0].iSubItem = 0;
- //vItem[0].cchTextMax = 0;
- //vItem[0].pszText = IntPtr.Zero;
- //vItem = null;
- }
- //VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
- //CloseHandle(vProcess);
- }
- catch(Exception ex)
- {
- Console.WriteLine(ex.Message);
- Console.WriteLine(ex.StackTrace);
- }
- }
- private static int MakeLParam(int LoWord, int HiWord)
- {
- int res = (int)((HiWord << 16) | (LoWord & 0xffff));
- return res;
- }
- #region Imports
- [StructLayoutAttribute(LayoutKind.Sequential)]
- public struct LVITEM
- {
- public int mask;
- public int iItem;
- public int iSubItem;
- public int state;
- public int 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;
- }
- [DllImport("user32.dll")]
- static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
- [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", SetLastError = true)]
- static extern IntPtr GetWindow(IntPtr hwnd, uint uCmd);
- [DllImport("User32.dll", EntryPoint = "SendMessage")]
- public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
- [DllImport("user32.dll", EntryPoint = "SendMessage")]
- private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
- [DllImport("User32.dll", EntryPoint = "PostMessage")]
- public static extern int PostMessage(int hWnd, int Msg, int wParam, int lParam);
- [DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
- public static extern bool SetForegroundWindow(int hWnd);
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement