[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

External application List to memo

Posted on 2006-11-15
8
Medium Priority
?
936 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 800 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

650 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