Loading ...
Sorry, an error occurred while loading the content.

81Retrieving ListView text

Expand Messages
  • paulbt2000
    Apr 12, 2002
      Here is some code that will retrieve the text from a ListView box.

      Paul

      SV*
      GetListViewHelper(HWND hwnd, int index)
      {
      /* some code adapted from Jeffrey Richter's September 1997
      Q&A article in the Microsoft Systems Journal */

      SV* sv = 0;
      HANDLE hProcess;
      LV_ITEM* plvi;
      char newlvi[255];
      int nIndex = 0;
      LV_ITEM lvi;


      /* Get the count of items in the ListView control */
      int nCount = ListView_GetItemCount(hwnd);

      /* Open a handle to the remote process's kernel object */
      DWORD dwProcessId;
      GetWindowThreadProcessId(hwnd, &dwProcessId);
      hProcess = OpenProcess( PROCESS_VM_OPERATION |
      PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, dwProcessId);

      if (hProcess == NULL) {
      printf("Could not communicate with process");
      }

      /* Allocate memory in the remote process's address space */
      plvi = (LV_ITEM*) VirtualAllocEx(hProcess, NULL, 4096,
      MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);

      newlvi[0] = '\0';

      /* Initialize a local LV_ITEM structure */
      lvi.mask = LVIF_TEXT;
      lvi.iItem = index;
      lvi.iSubItem = 0;
      /* NOTE: The text data immediately follows the LV_ITEM
      structure
      in the memory block allocated in the remote
      process.*/
      lvi.pszText = (LPTSTR) (plvi + 1);
      lvi.cchTextMax = 100;

      /* Write the local LV_ITEM structure to the remote memory
      block */
      WriteProcessMemory(hProcess, plvi, &lvi, sizeof(lvi), NULL);

      /* Tell the ListView control to fill the remote LV_ITEM
      structure */
      ListView_GetItem(hwnd, plvi);

      /* Read the remote text string into the end of our buffer */
      ReadProcessMemory(hProcess, plvi+1, &newlvi, 255, NULL);

      /*printf("%s\n", newlvi);*/

      if (newlvi != 0) {
      sv = newSVpv(newlvi, strlen(newlvi));
      }

      /* Free the memory in the remote process's address space */
      VirtualFreeEx(hProcess, plvi, 0, MEM_RELEASE);

      CloseHandle(hProcess);

      return sv;
      }



      void
      GetListViewContents(hwnd)
      HWND hwnd;
      PPCODE:
      int nelems = ListView_GetItemCount(hwnd);
      int i;
      for (i = 0; i < nelems; i++) {
      XPUSHs(sv_2mortal(GetListViewHelper(hwnd, i)));
      }


      SV*
      GetListViewText(hwnd, index)
      HWND hwnd;
      int index
      CODE:
      RETVAL = GetListViewHelper(hwnd, index);
      OUTPUT:
      RETVAL
    • Show all 4 messages in this topic