G
Guest
Hello,
It may be a repeated question but I don't find the solution to the situation
that I encounter in it.
My application is monitoring another application that is built in VB6. The
application monitors all the textboxes and other input & display controls on
that application.
The data from the textboxes and listboxes are retrived fine. But, when it
comes to Listview there I got memory exception.
I know that I need to do the in memory processing with the external listview
control by hack into the process.
I have successfuly do the external listview like Task Manager and my own
dummy appilcation that has the Listview control and I am getting data from
rows in it.
But when it comes to this specific application I am getting the memory
exception.
Below is the code that successfully get external listview code from listview
control.
IntPtr mainWindowHwnd;
IntPtr lvwHwnd = (IntPtr)0;
try
{
mainWindowHwnd = FindWindow(null, null);
lvwHwnd = FindWindowEx((IntPtr)mainWindowHwnd, IntPtr.Zero,
"ListView20WndClass", IntPtr.Zero);
if (lvwHwnd == (IntPtr)0)
throw new Win32Exception();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
int row_selected = SendMessage(lvwHwnd, (int)LVM_GETNEXTITEM,
-1,(IntPtr)1);
IntPtr pid = (IntPtr)0;
GetWindowThreadProcessId(lvwHwnd, ref pid);
IntPtr _lvi,_item;
IntPtr _subitem = IntPtr.Zero;
IntPtr process = OpenProcess(PROCESS_VM_OPERATION |
PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, false,
pid.ToInt32());
LVITEM lvi = new LVITEM();
_lvi = VirtualAllocEx(process, IntPtr.Zero,
Marshal.SizeOf(typeof(LVITEM)), MEM_COMMIT, PAGE_READWRITE);
_item = VirtualAllocEx(process, IntPtr.Zero, 512, MEM_COMMIT,
PAGE_READWRITE);
if (_lvi == IntPtr.Zero)
throw new SystemException("Failed to allocate memory in
remote process");
bool bsuccess;
lvi.cchTextMax = 512;
for (int j = 0; j <=10; j++)
{
lvi.iSubItem = j;
lvi.pszText = _item;
bsuccess = WriteProcessMemory(process, _lvi, ref lvi,
Marshal.SizeOf(typeof(LVITEM)), IntPtr.Zero);
SendMessage(lvwHwnd, LVM_GETITEMTEXT, row_selected, _lvi);
//Error comes here
IntPtr bytesReaded;
ReadProcessMemory(process, _item, item, 512, out bytesReaded);
}
virtualFreeEx(process, _lvi, 0, MEM_RELEASE);
VirtualFreeEx(process, _item, 0, MEM_RELEASE);
}
In my specific application when I call
SendMessage(lvwHwnd, LVM_GETITEMTEXT, row_selected, _lvi); //Error comes here
it crashes the other application.
What is wrong that I am doing? I have also check the user rights to run the
application and it is Administrator.
Anyone can suggest me the proper memory management or any other way to
handle this.
Thanks,
Irfan
It may be a repeated question but I don't find the solution to the situation
that I encounter in it.
My application is monitoring another application that is built in VB6. The
application monitors all the textboxes and other input & display controls on
that application.
The data from the textboxes and listboxes are retrived fine. But, when it
comes to Listview there I got memory exception.
I know that I need to do the in memory processing with the external listview
control by hack into the process.
I have successfuly do the external listview like Task Manager and my own
dummy appilcation that has the Listview control and I am getting data from
rows in it.
But when it comes to this specific application I am getting the memory
exception.
Below is the code that successfully get external listview code from listview
control.
IntPtr mainWindowHwnd;
IntPtr lvwHwnd = (IntPtr)0;
try
{
mainWindowHwnd = FindWindow(null, null);
lvwHwnd = FindWindowEx((IntPtr)mainWindowHwnd, IntPtr.Zero,
"ListView20WndClass", IntPtr.Zero);
if (lvwHwnd == (IntPtr)0)
throw new Win32Exception();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
int row_selected = SendMessage(lvwHwnd, (int)LVM_GETNEXTITEM,
-1,(IntPtr)1);
IntPtr pid = (IntPtr)0;
GetWindowThreadProcessId(lvwHwnd, ref pid);
IntPtr _lvi,_item;
IntPtr _subitem = IntPtr.Zero;
IntPtr process = OpenProcess(PROCESS_VM_OPERATION |
PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, false,
pid.ToInt32());
LVITEM lvi = new LVITEM();
_lvi = VirtualAllocEx(process, IntPtr.Zero,
Marshal.SizeOf(typeof(LVITEM)), MEM_COMMIT, PAGE_READWRITE);
_item = VirtualAllocEx(process, IntPtr.Zero, 512, MEM_COMMIT,
PAGE_READWRITE);
if (_lvi == IntPtr.Zero)
throw new SystemException("Failed to allocate memory in
remote process");
bool bsuccess;
lvi.cchTextMax = 512;
for (int j = 0; j <=10; j++)
{
lvi.iSubItem = j;
lvi.pszText = _item;
bsuccess = WriteProcessMemory(process, _lvi, ref lvi,
Marshal.SizeOf(typeof(LVITEM)), IntPtr.Zero);
SendMessage(lvwHwnd, LVM_GETITEMTEXT, row_selected, _lvi);
//Error comes here
IntPtr bytesReaded;
ReadProcessMemory(process, _item, item, 512, out bytesReaded);
}
virtualFreeEx(process, _lvi, 0, MEM_RELEASE);
VirtualFreeEx(process, _item, 0, MEM_RELEASE);
}
In my specific application when I call
SendMessage(lvwHwnd, LVM_GETITEMTEXT, row_selected, _lvi); //Error comes here
it crashes the other application.
What is wrong that I am doing? I have also check the user rights to run the
application and it is Administrator.
Anyone can suggest me the proper memory management or any other way to
handle this.
Thanks,
Irfan