Page 1 of 2 12 LastLast
Results 1 to 40 of 53

Thread: Working example of getting external listview items/text

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Working example of getting external listview items/text

    Sorry but this seems neverending, I have not yet come accross a proper working example (code). Please somebody put up an example (code) that actually works! thankyou

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Re: Working example of getting external listview items/text

    Yup, thats what I thought.

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Working example of getting external listview items/text

    What have you tried that didn't work?

    Also, if it's owner drawn it might not work...


    Has someone helped you? Then you can Rate their helpful post.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Re: Working example of getting external listview items/text

    I have tried everything, hax0rs function looked most promising but returns null strings. Its not owner drawn. This just simply cant be done, nobody can put up working code for this, only reaons why it wont work or code that doesnt work! sucks

  5. #5
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Working example of getting external listview items/text

    You need to be a little more specific about what does not work.
    I've read throught the other threads and simply stating that it returns null strings is not of any help.

    Do you examine the return code from each API call to determine if any of your functions are returning an error?

    The code posted does work, it is just not working in your situation so something specific about your program is not working.

  6. #6
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Working example of getting external listview items/text

    Did you want to get the text of the listview item?
    Last edited by benmartin101; Mar 30th, 2006 at 01:25 PM.

  7. #7
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Working example of getting external listview items/text

    Using WriteProcessMemory and ReadProcessMemory works fine from VB.

  8. #8
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Working example of getting external listview items/text

    From what i've read from others, they say you need to do it from within the external app's process memory. So I made this code that did just that. I tried it on Windows Explorer and it gave me the correct text.

    i used c++ and vb.net

    for the c++ dll:

    Code:
    #include "stdafx.h"
    #include "Commctrl.h"
    
    
    
    #pragma data_seg(".shared")
    	HWND _hTarget = 0;
    	HHOOK hook=0;
    	
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:.shared,RWS")
    
    LVITEM *lvItem;
    char lpText[255];
    HINSTANCE hInst;
    bool flag = true;;
    
    
    
    //send LVM_GETITEMTEXT message from within external app's process
    int getLVText(HWND hTarget)
    {
    	lvItem = new LVITEM;
    	
    	lvItem->iSubItem = 0;
    	lvItem->cchTextMax = 255;
    	lvItem->pszText = lpText;
    	int res = SendMessage(hTarget, LVM_GETITEMTEXT, 0, (long)lvItem);
    	
    	if (res != 0)
    	{
    		flag = false;
    		MessageBox(NULL, lvItem->pszText, "", MB_OK); //messagebox shows the listview item string
    		return 1;
    	}
    	return 0;
    }
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    {
    	hInst = (HINSTANCE)hModule;
        return TRUE;
    }
    
    
    LRESULT CALLBACK hookproc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    	if(flag)  //I put the flag so it only calls getLVText once
    	{
    		getLVText(_hTarget);
    	}
    	return ::CallNextHookEx(hook, nCode, wParam, lParam);
    }
    
    
    //these codes injects the dll into external app's process
    __declspec(dllexport) int _stdcall installHook(HWND hTarget)
    {
    	_hTarget = hTarget;
    	DWORD tid;
    	tid = ::GetWindowThreadProcessId(hTarget, 0);
    	hook = ::SetWindowsHookEx(WH_GETMESSAGE, hookproc, hInst, tid);
    	if (hook != NULL)
    		return 1;
    	return 0;
    }
    now for the vb.net code that calls the dll function installHook():
    VB Code:
    1. Public Declare Function installHook Lib "C:\Program Files\Microsoft Visual Studio\MyProjects\getListView\Debug\getListView.dll" (ByVal hwnd As Integer) As Integer
    2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3.      Dim hTarget As Integer 'the handle to the target ListView control, Im sure you could implement the code to get the handle
    4.      Dim res As Integer = installHook(hTarget)
    5. End Sub

    I hope getting the text of the listview items is what you wanted because it took me a while to do this. This is probably not the exact code you'll be using in your program, but it should give you an idea of how to get the text of the listview item.

  9. #9
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Working example of getting external listview items/text

    Moeur, was that last comment of yours for me?

  10. #10
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Working example of getting external listview items/text

    My last comment was for anyone looking to do this.
    You have shown one way to do it and I was commenting that the original way works well too and does not require C++.

  11. #11
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Working example of getting external listview items/text

    oh. Well, I was just curious how that other way works. If you write the structure in the external process, then you sendmessage() using the pointer that you get from the writeprocessmemory(), which process memory is that pointer pointing to? The calling process or the external process? I'm a bit confused. When I sendmessage(), lets say WM_SETTEXT, the pointer to the buffer containing the string is pointing to an address in the calling process. But the method with WriteProcessMemory() appear to be pointing at an address in the external process.

  12. #12
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Working example of getting external listview items/text

    You are correct, when you call SendMessage with the WM_SETTEXT message, the operating system recognizes the message and marshals the data for you from one process to the next as required.

    User-defined messages are not marshalled since the OS does not know about them (since they are user defined).

    Many messages that are specific to certain controls like the listview and RichTextbox are user-defined messages and the OS does not marshal them for you.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Re: Working example of getting external listview items/text

    benmartin101, any chance of the actual dll? and also the code for vb where u put the vb.net code in the button. Thanks, sorry.. know im asking alot!

  14. #14
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Re: Working example of getting external listview items/text

    hum..
    Dim res As Integer = installHook(hTarget)

    Integer ? should it be string ?

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Re: Working example of getting external listview items/text

    Also the window handle is integar shouldnt that be long

  16. #16
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Working example of getting external listview items/text

    I attached the project with the dll.
    Here is the vb.net code to call and use the dll.

    VB Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3.  
    4.     Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    5.  
    6.     Public Const WM_CHAR = &H102
    7.  Public Declare Function installHook Lib "C:\Program Files\Microsoft Visual Studio\MyProjects\getListView\Debug\getListView.dll" (ByVal hwnd As Integer, ByVal vbhwnd As Integer) As Integer
    8.     Public Declare Function getItemText Lib "C:\Program Files\Microsoft Visual Studio\MyProjects\getListView\Debug\getListView.dll" (ByVal index As Integer) As IntPtr
    9.  
    10.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    11.         Dim res As Integer = installHook(listViewHandle, Me.Handle.ToInt32)
    12.         PostMessage(child, WM_CHAR, 0, 0)    
    13.     End Sub
    14.  
    15.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    16.         Dim strPtr As IntPtr = getItemText(itemIndex)  '0 based
    17.         Dim str As String = Marshal.PtrToStringAnsi(strPtr)
    18.         MsgBox(str)
    19.     End Sub
    Attached Files Attached Files

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Re: Working example of getting external listview items/text

    Thanks for your effort but I am so confused, forgive my ignorance.

    Ok first of all why is your api " hwnd As Integer" and not long????

    Also on the first button click, what is:

    "PostMessage(child, WM_CHAR, 0, 0) "

    I don't see a child variable anywhere... whats going on there??

    Just everything about that code doesnt make any sense to me, maybe its cause its in .net

    Can anybody transfer this to vb???? belive me I have tried

  18. #18
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Working example of getting external listview items/text

    its integer instead of long because in vb6, long is 4 bytes, while long in vb.net is 8 bytes. To make it work, substitute long with vb.net's integer, which is 4 bytes. The "child" part, you should replace that with the handle of your target listview.

  19. #19
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Working example of getting external listview items/text

    I made changes to dll, so I attach new version.

    Here is vb6 code:

    VB Code:
    1. Private Const WM_CHAR = &H102
    2.  
    3. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    4.  
    5. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    6.  
    7. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    8. Private Declare Function installHook Lib "C:\Program Files\Microsoft Visual Studio\MyProjects\getListView\Debug\getListView.dll" (ByVal hwnd As Long) As Long
    9. Private Declare Function getItemText Lib "C:\Program Files\Microsoft Visual Studio\MyProjects\getListView\Debug\getListView.dll" (ByVal index As Long) As Long
    10. Private Declare Function getItemCount Lib "C:\Program Files\Microsoft Visual Studio\MyProjects\getListView\Debug\getListView.dll" () As Long
    11. Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" ( _
    12.     ByVal lpString1 As String, _
    13.     ByVal lpString2 As Long _
    14. ) As Long
    15. Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" ( _
    16.     ByVal lpString As Long _
    17. ) As Long
    18.  
    19.  
    20. Private Sub Command1_Click()
    21.     Dim lvHwnd As Long 'handle to listview
    22.     Dim res As Integer
    23.     res = installHook(lvHwnd)
    24.     PostMessage lvHwnd, WM_CHAR, 0, 0 'this triggers dll to load
    25. End Sub
    26.  
    27. Private Sub Command2_Click()
    28.     Dim lpString As Long
    29.     Dim strVal As String
    30.     Dim lenstr As Long
    31.     Dim itemCount As Long
    32.    
    33.     itemCount = getItemCount() 'to get total item in listview
    34.    
    35.     lpString = getItemText(0) 'to get text of item; 0-based
    36.     lenstr = lstrlen(lpString) + 1
    37.     strVal = String(lenstr, 0)
    38.     lstrcpy strVal, lpString
    39.     MsgBox (strVal)
    40.    
    41. End Sub
    Attached Files Attached Files

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Re: Working example of getting external listview items/text

    Thanks again for all your effort, but it just doesnt seem to work.. it doesnt even return the amount in the listview (returns 0)

  21. #21
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Working example of getting external listview items/text

    can you post the vb code that you are running?

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Re: Working example of getting external listview items/text

    Exact same vb code you posted, except i put:


    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim lvHwnd As Long 'handle to listview
    3.  
    4. lvHwnd = whatever the handle is
    5.  
    6.     Dim res As Integer
    7.     res = installHook(lvHwnd)
    8.     PostMessage lvHwnd, WM_CHAR, 0, 0 'this triggers dll to load
    9. End Sub

    And of course changed path to the dll!

  23. #23
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Working example of getting external listview items/text

    I attached a vb project that should let you understand how to make it work. Try it on a windows explorer folder and you'll see it works.(Make sure you drag that icon to the "listview" window and not just the parent window) If you use this program and still not get the listview items, then that listview class your trying to access is different from what i've been trying to access.
    Attached Files Attached Files

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Re: Working example of getting external listview items/text

    Whoooo hoooooooooooo after some messing got it to work!!!! wow I have to say a huge well done to you, I never thought I would see this working (getting listview items)

    Heres a few more questions... (do forgive me)

    Ok firstly how can I make the dll smaller?

    Also, is there a away to get the tooltip text of the listview item as well as the text???

    Can you get the listviews item image index ?


    Thanksyou so much for all your time and effort and skill!!!!!

  25. #25
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Working example of getting external listview items/text

    I have a question. What exactly are you trying to make?

  26. #26

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Re: Working example of getting external listview items/text

    Nothing really, I have just been looking into this for a year.. just would like to see how much you can actually do/get from a listview

  27. #27
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Working example of getting external listview items/text

    well, if you want to know more about it, then you may want to look into this site:

    http://msdn.microsoft.com/library/de...titemcount.asp

    and about the dll, I don't know of a way to make it smaller.

  28. #28

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Re: Working example of getting external listview items/text

    Ok, once thing I noticed is if the listview updates/has more/less items.. your code grabs the old items even if u restart ur program

  29. #29
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Working example of getting external listview items/text

    Yes, that's a bug that i've noticed. If you restart your computer, it goes back to normal. Can't really tell why, but as long as you get the idea of getting the item's text.

  30. #30
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Working example of getting external listview items/text

    Nice work Ben!
    Here is how you can do it without the dll.
    Attached Files Attached Files

  31. #31

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Re: Working example of getting external listview items/text

    Thanks for that moeur but having problems with the hex part

  32. #32
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Working example of getting external listview items/text

    how are you getting the hWnd of your listview?
    If you are using spy++ then the hWnd is in Hex you can just enter that number.

  33. #33

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Re: Working example of getting external listview items/text

    Sorry moeur just used spy++ and it works great, the above bug doesnt happen either (not getting new items), great code and thankyou. Do you know much about the lvmgettooltips? I did a search and only found one post about it (a post I made a while ago), just wondering how to get the items tooltip text and even get the icon it uses maybe.

  34. #34
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Working example of getting external listview items/text

    I tried both the programs posted, they both work for me.. I was wondering if theres anyway to use them on other applications that have things that look like listboxes, but apparently they arent... they dont have the class SystemListView or whatever, and niether code workson them..

  35. #35
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Working example of getting external listview items/text

    The classname of the 'listbox' i want this to work on is "Afx:400000:28:10011:0:0" if that helps

  36. #36
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Working example of getting external listview items/text

    I think 'listbox' is different from 'listview'.

  37. #37
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    399

    Re: Working example of getting external listview items/text

    Sorry I meant listview I was probably just using the wrong term.. how can I tell what it is for sure?

  38. #38

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    205

    Re: Working example of getting external listview items/text

    thread hijaCK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  39. #39
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: Working example of getting external listview items/text

    Quote Originally Posted by bail3yz
    The classname of the 'listbox' i want this to work on is "Afx:400000:28:10011:0:0" if that helps
    Afx controls were created before Windows existed. Instead of using a Windows text property, the text is owner-drawn by the control. As far as Windows is concerned, the control has no text--just a picture that happens to look to you like text. I've seen attempts to monitor windows messages to try to detect what characters are being written, but nothing successful. As far as I know, the only way to read the text of an Afx control is using mouse or keyboard events to raise a copy event in the Afx control so that you can get the text from the clipboard or through optical character recognition.

  40. #40
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: Working example of getting external listview items/text

    Quote Originally Posted by benmartin101
    I attached a vb project that should let you understand how to make it work. Try it on a windows explorer folder and you'll see it works.(Make sure you drag that icon to the "listview" window and not just the parent window) If you use this program and still not get the listview items, then that listview class your trying to access is different from what i've been trying to access.
    Many thanks for sharing this code. I tried to drag the icon to external listview to obtain the list but i get the follwoing error:



    and it is pointing to

    Code:
    Screen.MouseIcon = LoadPicture("C:\Program Files\Microsoft Visual Studio\VB98\protune.ico")
    I be happy if u tell me how to fix it.Thanks

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width