Advertisement
parabola949

Moving icons

May 7th, 2012
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.17 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Microsoft.Win32;
  6. using System.Runtime.InteropServices;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.Diagnostics;
  9. using System.Windows.Forms;
  10. using System.Drawing;
  11. using System.Threading;
  12.  
  13.  
  14. namespace desktopIconTest
  15. {
  16.     class Program
  17.     {
  18.         //private static readonly DesktopShortcutCollection _desktopShortcuts;
  19.         static bool prank = false;
  20.         static bool debug = false;
  21.         private const int WM_COPYDATA = 0x004a;
  22.         private const int WM_USER = 0x400;
  23.         private const int LVM_SETITEMPOSITION = 0x1000 + 15;
  24.         private const int LVM_FIRST = 0x1000;
  25.         private const int LVM_GETITEMCOUNT = LVM_FIRST + 4;
  26.         private const int LVM_GETITEM = LVM_FIRST + 75;
  27.         private const int LVM_GETITEMTEXTW = (LVM_FIRST + 115);
  28.         private const int LVIF_TEXT = 0x0001;
  29.         private const uint LVM_GETITEMW = LVM_FIRST + 75;
  30.         private const uint LVM_GETITEMPOSITION = LVM_FIRST + 16;
  31.         private const uint PROCESS_VM_OPERATION = 0x0008;
  32.         private const uint PROCESS_VM_READ = 0x0010;
  33.         private const uint PROCESS_VM_WRITE = 0x0020;
  34.         private const uint MEM_COMMIT = 0x1000;
  35.         private const uint MEM_RELEASE = 0x8000;
  36.         private const uint MEM_RESERVE = 0x2000;
  37.         private const uint PAGE_READWRITE = 4;
  38.         private const int SW_HIDE = 0;
  39.         private const string TITLE = "ForceIcon";
  40.         static string arg = "";
  41.  
  42.  
  43.         static void Main(string[] args)
  44.         {
  45.             //HideWindow();
  46.             //arg = (args.Length > 0) ? args[0] : "";
  47.             //arg = "test";
  48.             //ForceIcon();
  49.             //Thread.Sleep(30000);
  50.             HideWindow();
  51.             while (true)
  52.             {
  53.                 ForceIcon();
  54.                 Thread.Sleep(30000);
  55.             }
  56.         }
  57.  
  58.  
  59.         static void HideWindow()
  60.         {
  61.             Console.Title = TITLE;
  62.             IntPtr hWnd = FindWindow(null, TITLE);
  63.            
  64.             //if (hWnd != IntPtr.Zero && arg != "test")
  65.                 //ShowWindow(hWnd, SW_HIDE);
  66.             //else
  67.             //{
  68.             //    Console.WriteLine("Running in test mode");
  69.             //    debug = true;
  70.             //}
  71.         }
  72.  
  73.         static void ForceIcon()
  74.         {
  75.            
  76.             try
  77.             {
  78.                 int SSP = -1;
  79.                 //Console.WriteLine("vHandle:");
  80.                 IntPtr vHandle = FindWindow("Progman", "Program Manager");
  81.                 //Console.WriteLine(vHandle);
  82.                 vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
  83.                 //Console.WriteLine(vHandle);
  84.                 vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SysListView32", "FolderView");
  85.                 //Console.WriteLine(vHandle);
  86.                 int right = Screen.PrimaryScreen.WorkingArea.Right - 64, down = 26;
  87.                 //Console.WriteLine("right = " + right);
  88.                 //Console.WriteLine("down = " + down);
  89.                 int vItemCount = SendMessage(vHandle, LVM_GETITEMCOUNT, 0, 0);
  90.                 //Console.WriteLine("vItemCount = " + vItemCount);
  91.                 uint vProcessId;
  92.                 GetWindowThreadProcessId(vHandle, out vProcessId);
  93.                 //Console.WriteLine("vProcessId = " + vProcessId);
  94.                 IntPtr vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, false, vProcessId);
  95.                 //Console.WriteLine("vProcess = " + vProcess);
  96.                 IntPtr vPointer = VirtualAllocEx(vProcess, IntPtr.Zero, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  97.                 //Console.WriteLine("vPointer = " + vPointer);
  98.                 //Random r = new Random();
  99.                 //r.Next();
  100.  
  101.                 Console.WriteLine(vItemCount);
  102.                 for (int j = 0; j < vItemCount; j++)
  103.                 {
  104.                     LVITEM[] vItem = new LVITEM[1];
  105.  
  106.                     byte[] vBuffer = new byte[256];
  107.  
  108.                     vItem[0].mask = LVIF_TEXT;
  109.                     vItem[0].iItem = j;
  110.                     vItem[0].iSubItem = 0;
  111.                     vItem[0].cchTextMax = vBuffer.Length;
  112.                     vItem[0].pszText = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(LVITEM)));
  113.                     //vItem[0].pszText = Marshal.AllocHGlobal(512);
  114.                     uint vNumberOfBytesRead = 0;
  115.  
  116.                     WriteProcessMemory(vProcess, vPointer, Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0), Marshal.SizeOf(typeof(LVITEM)), ref vNumberOfBytesRead);
  117.                     SendMessage(vHandle, LVM_GETITEMW, j, vPointer.ToInt32());
  118.                     ReadProcessMemory(vProcess, (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(LVITEM))), Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0), vBuffer.Length, ref vNumberOfBytesRead);
  119.                     string vText = Encoding.Unicode.GetString(vBuffer, 0, (int)vNumberOfBytesRead);
  120.                     string IconName = Marshal.PtrToStringAnsi(vItem[0].pszText);
  121.                     //Get icon location
  122.                     Console.WriteLine("Getting icon location: " + IconName + vText);
  123.                     SendMessage(vHandle, LVM_GETITEMPOSITION, j, vPointer.ToInt32());
  124.                     Point[] vPoint = new Point[1];
  125.                     ReadProcessMemory(vProcess, vPointer, Marshal.UnsafeAddrOfPinnedArrayElement(vPoint, 0), Marshal.SizeOf(typeof(Point)), ref vNumberOfBytesRead);
  126.                     string IconLocation = vPoint[0].ToString();
  127.                     Console.WriteLine(IconLocation);
  128.                     //Console.WriteLine(IconName);
  129.                     if (IconName.Contains("Self Service Portal"))
  130.                         SSP = j;
  131.                     //if (prank)
  132.                     //{
  133.                     //    int rright = r.Next(0, Screen.PrimaryScreen.WorkingArea.Right);
  134.                     //    int rdown = r.Next(0, Screen.PrimaryScreen.WorkingArea.Bottom);
  135.                     //    SendMessage(vHandle, LVM_SETITEMPOSITION, j, MakeLParam(rright, rdown));
  136.                     //}
  137.                     if (SSP > -1)
  138.                     {
  139.                         Console.WriteLine("Moving Icon");
  140.                         SendMessage(vHandle, LVM_SETITEMPOSITION, SSP, MakeLParam(right, down));
  141.                     }
  142.                     SSP = -1;
  143.                     //vItem[0].mask = 0;
  144.                     //vItem[0].iItem = 0;
  145.                     //vItem[0].iSubItem = 0;
  146.                     //vItem[0].cchTextMax = 0;
  147.                     //vItem[0].pszText = IntPtr.Zero;
  148.                     //vItem = null;
  149.                    
  150.                 }
  151.                 //VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
  152.                 //CloseHandle(vProcess);
  153.                
  154.             }
  155.             catch(Exception ex)
  156.             {
  157.                 Console.WriteLine(ex.Message);
  158.                 Console.WriteLine(ex.StackTrace);
  159.             }
  160.            
  161.                
  162.            
  163.         }
  164.  
  165.         private static int MakeLParam(int LoWord, int HiWord)
  166.         {
  167.             int res = (int)((HiWord << 16) | (LoWord & 0xffff));
  168.             return res;
  169.         }
  170.  
  171.         #region Imports
  172.         [StructLayoutAttribute(LayoutKind.Sequential)]
  173.         public struct LVITEM
  174.         {
  175.             public int mask;
  176.             public int iItem;
  177.             public int iSubItem;
  178.             public int state;
  179.             public int stateMask;
  180.             public IntPtr pszText; // string
  181.             public int cchTextMax;
  182.             public int iImage;
  183.             public IntPtr lParam;
  184.             public int iIndent;
  185.             public int iGroupId;
  186.             public int cColumns;
  187.             public IntPtr puColumns;
  188.         }
  189.  
  190.         [DllImport("user32.dll")]
  191.         static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  192.  
  193.         [DllImport("kernel32.dll")]
  194.         public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,
  195.             uint dwSize, uint flAllocationType, uint flProtect);
  196.         [DllImport("kernel32.dll")]
  197.         public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,
  198.            uint dwSize, uint dwFreeType);
  199.         [DllImport("kernel32.dll")]
  200.         public static extern bool CloseHandle(IntPtr handle);
  201.         [DllImport("kernel32.dll")]
  202.         public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
  203.            IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);
  204.         [DllImport("kernel32.dll")]
  205.         public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
  206.            IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);
  207.         [DllImport("kernel32.dll")]
  208.         public static extern IntPtr OpenProcess(uint dwDesiredAccess,
  209.             bool bInheritHandle, uint dwProcessId);
  210.         [DllImport("user32.DLL")]
  211.         public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
  212.         [DllImport("user32.DLL")]
  213.         public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);
  214.         [DllImport("user32.DLL")]
  215.         public static extern IntPtr FindWindowEx(IntPtr hwndParent,
  216.             IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
  217.         [DllImport("user32.dll")]
  218.         public static extern uint GetWindowThreadProcessId(IntPtr hWnd,
  219.             out uint dwProcessId);
  220.  
  221.         [DllImport("user32.dll", SetLastError = true)]
  222.         static extern IntPtr GetWindow(IntPtr hwnd, uint uCmd);
  223.  
  224.         [DllImport("User32.dll", EntryPoint = "SendMessage")]
  225.         public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
  226.  
  227.         [DllImport("user32.dll", EntryPoint = "SendMessage")]
  228.         private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
  229.  
  230.         [DllImport("User32.dll", EntryPoint = "PostMessage")]
  231.         public static extern int PostMessage(int hWnd, int Msg, int wParam, int lParam);
  232.  
  233.         [DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
  234.         public static extern bool SetForegroundWindow(int hWnd);
  235.         #endregion
  236.  
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement