Solved

External application List to memo

Posted on 2006-11-15
8
897 Views
Last Modified: 2008-01-09
Hi.

How can i pass the content of a list of an external application to my memo ?

This is a part of my code :
...
     cnt:=ListView_GetItemCount(StrToInt(Edit1.Text)); //Handle of the list component (SysListView)
     GetMem(lpBuff, 200);
     for I:=0 to cnt-1 do
     begin
         ListView_GetItemText(Handle, I, 0, lpBuff, 200);
         Memo1.Lines.Add(string(lpBuff));
     end;
     FreeMem(lpBuff);
...

But his doesnt work. It brings weird stuff..

Thanks in advance.
0
Comment
Question by:CodedK
  • 3
  • 3
  • 2
8 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 17946134
is lpBuff of type pchar or pointer? if pointer, make it pchar. can't see anything else wrong.
0
 
LVL 16

Author Comment

by:CodedK
ID: 17946154
Hi Ciuly.

Its Pchar...

One line thats wrong is : >> ListView_GetItemText(Handle, I, 0, lpBuff, 200);

It should be :                 >> ListView_GetItemText(StrToInt(Edit1.Text), I, 0, lpBuff, 200);

But everytime i try to do it the external application crash (I've tried many apps not just one)
0
 
LVL 28

Expert Comment

by:2266180
ID: 17946398
hm.. I did some investigation and it seems that this might be "a process accessing another process memory" issue. I am not exactly sure how to fix this. I was thinking to allocate the memory in adifferent way but can't seem to find a function that would allow me to set a flag so that the memory can be shared across processes. didn't work much with this sort of stuff.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 26

Accepted Solution

by:
Russell Libby earned 200 total points
ID: 17947155

I can provide a solution, but its NT based only due to the call to the API calls used (NT/Win2k/XP/2003). Anyways, you have 2 problems:

1 - Reading / passing memory in another process. (you can't pass memory from your process to another, it just does not work that way.)
2 - A lot of list view handlers don't respond to the ListView_GetItemText, but they ALL respond to ListView_GetItem

So this means allocating memory in the other process, then reading / writing / passing that memory pointer. I have source that simplifies much of this, and can be downloaded from:

http://users.adelphia.net/~rllibby/downloads/processmemory.zip

Include the ProcessMemory unit in your uses clause, then try the following code. The code allocates a block of memory to use as the list view item record, and another to use as the string pointer to pass. Once the remote funtion fills in the list view item, you only have to read the string blocks LocalMemory pointer (which handles the reading from remote to a local storage area).

Let me know if you have problems or questions,
Russell

---
var  pmRemote:      TProcessMemory;
     szString:      String;
     lvItem:        TLVItem;
     handle:        HWND;
     cnt:           Integer;
     i:             Integer;
     idxItem:       Integer;
     idxString:     Integer;
begin

  // Get handle to list view in remote process
  handle:=StrToInt(Edit1.Text);

  // Create handler for remote process
  pmRemote:=TProcessMemory.CreateFromHwnd(handle);
  try
     // Allocate block for the lvItem and the string to read the text from
     idxItem:=pmRemote.Add(SizeOf(TLVItem));
     idxString:=pmRemote.Add(1024);
     // Get list count
     cnt:=ListView_GetItemCount(handle);
     // Walk items
     for i:=0 to cnt-1 do
     begin
        // Clear local item
        FillChar(lvItem, SizeOf(TLVItem), 0);
        // Set params
        lvItem.mask:=LVIF_TEXT;
        lvItem.iItem:=i;
        lvItem.iSubItem:=0;
        lvItem.pszText:=pmRemote[idxString].Memory;
        lvItem.cchTextMax:=1024;
        // Set the remote memory pointer
        pmRemote[idxItem].Write(lvItem, SizeOf(TLVItem));
        // Get the item
        if ListView_GetItem(handle, TLVItem(pmRemote[idxItem].Memory^)) then
        begin
           // Get string from local memory part of handler
           szString:=PChar(pmRemote[idxString].LocalMemory);
           // Read local memory (which handles the transfer)
           Memo1.Lines.Add(szString);
        end;
     end;
  finally
     // Free object
     pmRemote.Free;
  end;

end;
0
 
LVL 16

Author Comment

by:CodedK
ID: 17959151
Thank you Russell, it works.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17960465
Welcome..

You *are* going to close the question? right? <g>

Russell
0
 
LVL 16

Author Comment

by:CodedK
ID: 17960533
I'm sorry ...
I thought i did ...
 
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17960586
lol, no worries...
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now