External listview control memory exception when sendmessage is cal

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
Hello,

Any update on this issue from anyone? Where are the experts?

One more thing to add, this particular problem occurs on a system that I am
running through terminal server. Is there some kind of rights issue? That
particular user is part of administrator group.
What kind of settings do I require here?

Thanks,
Irfan
 
Hello,

Someone suggested to increase the _lvi (LVITEM) type memory but it did work.

_lvi = VirtualAllocEx(process, IntPtr.Zero, Marshal.SizeOf(typeof(LVITEM)),
MEM_COMMIT, PAGE_READWRITE);

I tried it to 255 but no luck.
_lvi = VirtualAllocEx(process, IntPtr.Zero, 255, MEM_COMMIT, PAGE_READWRITE);

Please advice on this. I need solution really urgent.
Thank you for your corporation.
Irfan
 
Back
Top