Solved

External application List to memo

Posted on 2006-11-15
8
919 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 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