How to use ListView_FindItem

Hi,


I try to got a item index by String in Desktop List View, but it fail, is there something wrong to use ListView_FindItem?
Code as follow:
 
procedure TForm1.Button1Click(Sender: TObject);
  var
    hDesktop, hTemp : THandle;
    FindInfo : LV_FINDINFO;
  begin
       hTemp    := FindWindow('ProgMan', nil);
       hTemp    := GetWindow(hTemp, GW_CHILD);
       hDesktop := GetWindow(hTemp, GW_CHILD);
 
       FindInfo.flags := LVFI_STRING;
       FindInfo.psz := PChar('My Documents');
       if ListView_FindItem(hDesktop, 1, FindInfo) > 0 then
           showmessage('WORK!!!');
  end;
 
Thanks a lot.
 
y9y
y9yAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MadshiCommented:
The problem is that the desktop's listview is in the context of another process, so it's in another memory context, too. And THAT does mean, that pointers that are valid in YOUR process are not valid in the explorer's process. So the explorer gets a pointer to a string that is valid only in YOUR process. In the explorer's process this pchar pointer points to "wild" memory.
Again the solution is: Put it all in a dll and inject this dll into the explorer's process...

Regards, Madshi.
0
y9yAuthor Commented:
Hi, Madshi:

Thanks a lot for your answer, base on your comment, following line should not work either:

ListView_SetItemPosition(hDesktop, ListView_GetItemCount(hDesktop)-1, 550, 600 );

But it works in my app, always drag last icon to where I want (excect AutoArrange).

That make me a little bit confused, I know it is "another memory context", but why [SetItemPosition][GetItemCount] work, but [GetItemPosition][FindItem] do not work???
0
MadshiCommented:
Look at how ListView_SetItemPosition is defined in "CommCtrl.pas":

function ListView_SetItemPosition(hWnd: HWND; i, x, y: Integer): Bool;
begin
  Result := Bool( SendMessage(hWnd, LVM_SETITEMPOSITION, i, MakeLong(x, y)) );
end;

It is nothing but a simple message. You can transport two 32bit values with SendMessage (wParam and lParam). As long as you transport two cardinals or two integers, that is ORDINALS, you have no problem. That's why SetItemPosition and GetItemCount DO work, because there are no pointer involved.
As soon as a listView message needs pointers, you come into problems. And the caption of the list item you want to find doesn't fit into two 32bit values. One of those 32bit values takes the pointer to that find string. And there we have the POINTER - so there we have the problems...

Regards, Madshi.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

y9yAuthor Commented:
Madshi:

Very clear! thanks!

Why not I myself read "CommCtrl.pas" before I ask these 2 question!

Just the situation of [ListView_SetItemPosition] work and[ListView_GetItemPosition] not work make my head mess...


Best Regards.
0
MadshiCommented:
Just look at the sources again:

function ListView_SetItemPosition(hWnd: HWND; i, x, y: Integer): Bool;
begin
  Result := Bool( SendMessage(hWnd, LVM_SETITEMPOSITION, i, MakeLong(x, y)) );
end;

function ListView_GetItemPosition(hwndLV: HWND; i: Integer;
  var ppt: TPoint): Bool;
begin
  Result := Bool( SendMessage(hWndLV, LVM_GETITEMPOSITION, i, Longint(@ppt)) );
end;

SetItemPosition puts x and y into one 32 bit ordinal, so that works.
GetItemPosition sends a pointer to the listView, that does NOT work...

Regards, Madshi.
0
y9yAuthor Commented:
Hi, Madshi:

Thanks for your help, your answer is accepted already before you leave the last message to me.

Hope I do correct 'Accept' operation on my first question in Experter-Exchange. If it is wrong, £¨I realize the point changed from 20 to 2), feel free tell me.

Have a nice day.

y9y
0
MadshiCommented:
It's alright with the accepting. If you accept an answer the points go to 10% of its original value. If now another expert looks in the previously answered question, reads your question and wants to know the answer, he has to pay those 10% of the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.