Click here to Skip to main content
15,887,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am a beginner in C#, I am trying to get text from a list view of another process, like Internet download manager.

I copy this code from a webpage But it doesn't work. Please!! anyone helps me!!
<pre>[DllImport("user32.dll")]
        static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false) ]
        static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, StringBuilder lParam);
 //
        [DllImport("user32.dll", SetLastError = true)]
        static public extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
        [DllImport("kernel32.dll")]
        static public extern IntPtr OpenProcess(uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);       
        [DllImport("kernel32.dll")]
        static public extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref TvItem buffer, int dwSize, IntPtr lpNumberOfBytesWritten);
        [DllImport("kernel32.dll")]
        static public extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int dwSize, IntPtr lpNumberOfBytesRead);
        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        static public extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, uint dwFreeType);
        [DllImport("kernel32.dll")]
        static public extern bool CloseHandle(IntPtr hObject);
        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        static public extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect); 
        
private void GetTextMonitor()
        {
            IntPtr hWnd = FindWindow(null, "Internet Download Manager 5.14");
            IntPtr hWndList = FindWindowEx(hWnd, IntPtr.Zero, null, null);

            int rowCount = (int)SendMessage(hWndList, LVM_GETITEMCOUNT, IntPtr.Zero, IntPtr.Zero);
            string[] lbItems = new string[rowCount];
            label1.Text = "Parent:" + hWnd + ", Child:" + hWndList;
            label2.Text = "ListRow: " + rowCount + " " +
                (int)SendMessage(hWndList, 0x1003, IntPtr.Zero, IntPtr.Zero);
            for (int index = 0; index < rowCount; index++)
            {               
                string[] cells = new string[10]; 
                for (int a = 0; a < 10; a++) 
                {
                   cells[a] = GetListViewItem(hWndList, index, a).ToLower();
                   label2.Text += cells[a].ToString() +"\n";
                } 
            }
        }


 static private string GetListViewItem(IntPtr hWnd, int index, int subitem) 
        { 
            LvItem lvItem = new LvItem(); 
            IntPtr lpLocalBuffer = Marshal.AllocHGlobal(1024); 
            uint pid; 
            GetWindowThreadProcessId(hWnd, out pid); 
            IntPtr hProcess = OpenProcess(0x001f0fff, false, (int)pid); 
            IntPtr lpRemoteBuffer = VirtualAllocEx(hProcess, IntPtr.Zero, 1024, 0x1000, 4); 
            lvItem.mask = 1; 
            lvItem.iItem = index; 
            lvItem.iSubItem = subitem; 
            lvItem.pszText = (IntPtr)((int)lpRemoteBuffer + Marshal.SizeOf(typeof(LvItem))); 
            lvItem.cchTextMax = 50;             
            //WriteProcessMemory(hProcess, lpRemoteBuffer, ref lvItem, Marshal.SizeOf(typeof(LvItem)), (IntPtr)0); 
            SendMessage(hWnd, 0x1005, IntPtr.Zero, lpRemoteBuffer);
            ReadProcessMemory(hProcess, lpRemoteBuffer, lpLocalBuffer, 1024, (IntPtr)0); 
            string ret = Marshal.PtrToStringAnsi((IntPtr)((int)lpLocalBuffer + Marshal.SizeOf(typeof(LvItem)))); 
            Marshal.FreeHGlobal(lpLocalBuffer); 
            VirtualFreeEx(hProcess, lpRemoteBuffer, 0, 0x8000); 
            CloseHandle(hProcess); 
            return ret; 
        } 
Posted
Comments
LittleYellowBird 13-Oct-10 10:30am    
Hi, please try to explain why it does not work. Do you get errors? Does it compile? Try to find out where it goes wrong by single stepping through your code. The more information you can offer, the better help you will get. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900