473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Getting Items from ListView from other application

Hi
If it is wrong newsgroup, please let me know where to find the answer.
I am trying to get item text form ListView (there are few items and 5
columns) from other application (application written in MFC). As I know I
have to allocate memory in target application address space, send a message
and than read from allocated memory. But I still doing something wrong and
can't find correct solution. I fell I am close to it. Please find code below
(C#) :

[StructLayout( LayoutKind.Sequential)]
public struct LV_ITEMA
{
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 int lParam;
public int iIndent;
}
/// <summary>
/// Summary description for ListViewItem.
/// </summary>
public class ListViewItem
{
public ListViewItem()
{
}

public const int PROCESS_QUERY_INFORMATION = 1024;
public const int PROCESS_VM_OPERATION = 0x8;
public const int PROCESS_VM_READ = 0x10;
public const int PROCESS_VM_WRITE = 0x20;
public const int STANDARD_RIGHTS_REQUIRED = 0xF0000;
public const int MAX_LVMSTRING = 255;
public const int MEM_COMMIT = 0x1000;
public const int MEM_RELEASE = 0x8000;
public const int PAGE_READWRITE = 0x4;
public const int LVIF_TEXT = 0x1;

private const int LVM_FIRST = 0x1000;
private const int LVM_GETITEMCOUNT = LVM_FIRST + 4;
private const int LVM_GETCOLUMN = LVM_FIRST + 25;
private const int LVM_GETITEM = LVM_FIRST + 5;
private const int LVM_GETITEMTEXT = 0x102D;

bool result;
IntPtr pHandle;
IntPtr pStrBufferMemory;
IntPtr pMyItemMemory;

[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(int dwDesiredAccess, bool
bInheritHandle, int dwProcId );
[DllImport("kernel32.dll")]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess,IntPtr lpAddress,
int dwSize, int flAllocationType, int flProtect );
[DllImport("kernel32.dll")]
public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,
int dwSize, int dwFreeType);
[DllImport("kernel32.dll")]
public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr
lpBaseAddress,ref LV_ITEMA lpBuffer, int nSize, ref int
lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr
lpBaseAddress, ref LV_ITEMA lpBuffer, int nSize, ref int
lpNumberOfBytesWritten);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam,
IntPtr lParam);
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr hObject);

public void Run(IntPtr ptr)
{
strBuffer = new byte[MAX_LVMSTRING];
Process [] localByName = Process.GetProcessesByName("myProcess");
Process proc = localByName[0];
pHandle = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ |
PROCESS_VM_WRITE, false, proc.Id);
pStrBufferMemory = VirtualAllocEx(pHandle,IntPtr.Zero, MAX_LVMSTRING,
MEM_COMMIT, PAGE_READWRITE);
LV_ITEMA lv = new LV_ITEMA();
lv.mask = LVIF_TEXT;
lv.iItem = 1;
lv.iSubItem = 5;
lv.pszText = pStrBufferMemory;
lv.cchTextMax = MAX_LVMSTRING;
pMyItemMemory = VirtualAllocEx(pHandle,IntPtr.Zero, Marshal.SizeOf(lv),
MEM_COMMIT, PAGE_READWRITE);
IntPtr lvptr = Marshal.AllocHGlobal(Marshal.SizeOf(lv));
Marshal.StructureToPtr(lv,lvptr,false);
int written = 0;
result = WriteProcessMemory(pHandle, pMyItemMemory,ref lv,
Marshal.SizeOf(lv), ref written);

int i = SendMessage(ptr, LVM_GETITEM, 0, pMyItemMemory);
i = SendMessage(ptr, LVM_GETITEMTEXT, 1, pStrBufferMemory);
IntPtr ppr = IntPtr.Zero;
int read = 0;
StringBuilder str = new StringBuilder(MAX_LVMSTRING);
result = ReadProcessMemory(pHandle, pMyItemMemory,ref lv, written,ref read);
string str1 = Marshal.PtrToStringAnsi(lv.pszText);
}

As a result of i = SendMessage(ptr, LVM_GETITEMTEXT, 1, pStrBufferMemory);
I'm receiving 0 - means false and
string str1 = Marshal.PtrToStringAnsi(lv.pszText);
Gives me an error : Object instance not set to object.
Please help
Thanks in advance
brgds
Jacek
Jul 21 '05 #1
0 1860

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: thomas | last post by:
hi, i make a listview and add three items. the backgroundcolor of the listview is white and the textcolor ist black. on my notebook (IBM Thinkpad, XP Sp2) i cannnot see the items. on my pc (P4,...
6
by: Nick | last post by:
Hi there, I'm trying to implement drag-drop for my listview control in large icon view mode. Unfortunately the order of the items gets completely messed up upon inserting the item back into the...
0
by: Jacek | last post by:
Hi If it is wrong newsgroup, please let me know where to find the answer. I am trying to get item text form ListView (there are few items and 5 columns) from other application (application written...
1
by: dvestal | last post by:
I have a ListView with checkboxes. I want to remove items when the checkboxes are unchecked, but to do so yields an ArgumentOutOfRangeException. Is there an easy way to get around this? A...
0
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
2
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
11
by: Armin Zingler | last post by:
"Bill Schanks" <wschanks@gmail.comschrieb Try to execute lvMembers.beginupdate before filling and lvMembers.endupdate
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, In my application I need to periodically remove all current items from a ListView and add a new set into it. The following abbreviated code contains the basic idea: private void...
1
by: prpradip | last post by:
In my application ListView is inside panel (XP theme enable), item text in the ListView control overlaps other items in small icon view. The items are drawn as if aligned to a grid and this causes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.