简体   繁体   English

从64位的SysListView32获取文本

[英]Getting Text from SysListView32 in 64bit

here is my code : 这是我的代码:

   public static string ReadListViewItem(IntPtr lstview, int item)
    {
        const int dwBufferSize = 1024;

        int dwProcessID;
        LV_ITEM lvItem;
        string retval;
        bool bSuccess;
        IntPtr hProcess = IntPtr.Zero;
        IntPtr lpRemoteBuffer = IntPtr.Zero;
        IntPtr lpLocalBuffer = IntPtr.Zero;
        IntPtr threadId = IntPtr.Zero;

        try
        {
            lvItem = new LV_ITEM();
            lpLocalBuffer = Marshal.AllocHGlobal(dwBufferSize);
            // Get the process id owning the window
            threadId = GetWindowThreadProcessId(lstview, out dwProcessID);
            if ((threadId == IntPtr.Zero) || (dwProcessID == 0))
                throw new ArgumentException("hWnd");

            // Open the process with all access
            hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, dwProcessID);
            if (hProcess == IntPtr.Zero)
                throw new ApplicationException("Failed to access process");

            // Allocate a buffer in the remote process
            lpRemoteBuffer = VirtualAllocEx(hProcess, IntPtr.Zero, dwBufferSize, MEM_COMMIT,
              PAGE_READWRITE);
            if (lpRemoteBuffer == IntPtr.Zero)
                throw new SystemException("Failed to allocate memory in remote process");

            // Fill in the LVITEM struct, this is in your own process
            // Set the pszText member to somewhere in the remote buffer,
            // For the example I used the address imediately following the LVITEM stuct
            lvItem.mask = LVIF_TEXT;

            lvItem.iItem = item;
            lvItem.iSubItem = 2;
            lvItem.pszText = (IntPtr)(lpRemoteBuffer.ToInt32() + Marshal.SizeOf(typeof(LV_ITEM)));
            lvItem.cchTextMax = 50;

            // Copy the local LVITEM to the remote buffer
            bSuccess = WriteProcessMemory(hProcess, lpRemoteBuffer, ref lvItem,
              Marshal.SizeOf(typeof(LV_ITEM)), IntPtr.Zero);
            if (!bSuccess)
                throw new SystemException("Failed to write to process memory");

            // Send the message to the remote window with the address of the remote buffer
            SendMessage(lstview, LVM_GETITEMText, 0, lpRemoteBuffer);

            // Read the struct back from the remote process into local buffer
            bSuccess = ReadProcessMemory(hProcess, lpRemoteBuffer, lpLocalBuffer, dwBufferSize,IntPtr.Zero);
            if (!bSuccess)
                throw new SystemException("Failed to read from process memory");

            // At this point the lpLocalBuffer contains the returned LV_ITEM structure
            // the next line extracts the text from the buffer into a managed string
            retval = Marshal.PtrToStringAnsi((IntPtr)(lpLocalBuffer +
              Marshal.SizeOf(typeof(LV_ITEM))));
        }
        finally
        {
            if (lpLocalBuffer != IntPtr.Zero)
                Marshal.FreeHGlobal(lpLocalBuffer);
            if (lpRemoteBuffer != IntPtr.Zero)
                VirtualFreeEx(hProcess, lpRemoteBuffer, 0, MEM_RELEASE);
            if (hProcess != IntPtr.Zero)
                CloseHandle(hProcess);
        }
        return retval;
    }

no matter what i do retval returns empty, although lpLocalBuffer doesnt . 无论我做什么retval返回空,虽然lpLocalBuffer没有。

here is the def of ListItem : 这是ListItem的def:

   [StructLayout(LayoutKind.Sequential)]
    private struct LV_ITEM
    {
        public int mask;
        public int iItem;
        public int iSubItem;
        public int state;
        public int stateMask;
        public IntPtr pszText;
        public int cchTextMax;
        public int iImage;
        internal int lParam;
        internal int iIndent;
    }

i tried compiling for 86x , 64bit, any cpu , nothing seems to work at all ! 我尝试编译86x,64bit,任何cpu,似乎没有任何工作!

any idea why this might be happening ? 知道为什么会这样吗?

C# + .net4 , windows 7 64bit. C#+ .net4,windows 7 64位。

Here's a different approach to doing this - use UI Automation . 这是一种不同的方法 - 使用UI自动化 It does the cross-process, cross-bitness work for you, and will work against listviews, listboxes, or pretty much any other standard Windows UI. 它为您进行跨进程,跨位工作,并且可以对列表视图,列表框或几乎任何其他标准Windows UI起作用。 Here's a sample app that will get the HWND from the listview under the mouse pointer, and dump the items in it. 这是一个示例应用程序,它将从鼠标指针下的列表视图中获取HWND,并将项目转储到其中。 It dumps just the name of each item; 它只转储每个项目的名称; with Listviews, I think you can recurse into the fields in each item if you want. 使用Listviews,我想你可以根据需要递归到每个项目的字段中。

// Compile using: csc ReadListView.cs /r:UIAutomationClient.dll

using System;
using System.Windows.Automation;
using System.Runtime.InteropServices;

class ReadListView
{
    public static void Main()
    {
        Console.WriteLine("Place pointer over listview and hit return...");
        Console.ReadLine();

        // Get cursor position, then the window handle at that point...
        POINT pt;
        GetCursorPos(out pt);
        IntPtr hwnd = WindowFromPoint(pt);

        // Get the AutomationElement that represents the window handle...
        AutomationElement el = AutomationElement.FromHandle(hwnd);

        // Walk the automation element tree using content view, so we only see
        // list items, not scrollbars and headers. (Use ControlViewWalker if you
        // want to traverse those also.)
        TreeWalker walker = TreeWalker.ContentViewWalker;
        int i = 0;
        for( AutomationElement child = walker.GetFirstChild(el) ;
            child != null; 
            child = walker.GetNextSibling(child) )
        {
            // Print out the type of the item and its name
            Console.WriteLine("item {0} is a \"{1}\" with name \"{2}\"", i++, child.Current.LocalizedControlType, child.Current.Name);
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct POINT
    {
        public int x;
        public int y;
    };

    [DllImport("user32.dll")]
    private static extern IntPtr WindowFromPoint(POINT pt);

    [DllImport("user32.dll")]
    private static extern int GetCursorPos(out POINT pt);
}

I know this is old, but I found it while trying to solve my problem and hopefully this will help someone else. 我知道这是旧的,但我在试图解决我的问题时找到了它,希望这会帮助别人。

I used the recommendation in this question , that was in C++, and slightly modified the LV_ITEM structure to make it work with 64bit in VB.NET (I haven't tested in C# but I imagine the solution is quite similar.) 我使用了这个问题中的建议,即在C ++中,稍微修改了LV_ITEM结构,使其在VB.NET中使用64位(我没有在C#中测试过,但我想这个解决方案非常相似。)

Public Structure LV_ITEM64

    Public mask As Integer
    Public iItem As Integer
    Public iSubItem As Integer
    Public state As Integer
    Public stateMask As Integer
    Public placeholder1 As Integer
    Public pszText As Integer
    Public placeholder2 As Integer
    Public cchTextMax As Integer
    Public iImage As Integer

End Structure

Then, when declaring the instance of the structure, I used the following code to choose between 64 bit and 32 bit structures: 然后,在声明结构的实例时,我使用以下代码在64位和32位结构之间进行选择:

Dim lvi As Object

If IntPtr.Size = 4 Then
    lvi = New LV_ITEM
Else
    lvi = New LV_ITEM64
End If

You have clarified that you are trying to read items from a list view control in a 32 bit process into a different 64 bit process. 您已经明确表示您正在尝试将列表视图控件中的项目在32位进程中读取到另一个64位进程中。

I have seen many questions on this topic in various forums and not one ever seemed to achieve a successful outcome. 我在各种论坛上看到了很多关于这个主题的问题,而且似乎没有一个人能够取得成功。

I think your best option is to create a 32 bit executable which will be able to read out of the other program's list view. 我认为你最好的选择是创建一个32位可执行文件,它可以读出其他程序的列表视图。

There is at least one obstacle to overcome if your program is 32-bit and the target program is 64-bit. 如果您的程序是32位且目标程序是64位,则至少有一个障碍需要克服。 Or the other way around. 或者相反。 The LVITEM declaration will be wrong, IntPtr has the wrong number of bits. LVITEM声明是错误的,IntPtr的位数错误。 Which makes Marshal.SizeOf() return the wrong value. 这使得Marshal.SizeOf()返回错误的值。 Alignment is okay, I think, by accident. 我认为,对齐是可以的。 Changing the field to either int or long can fix the problem, depending on the bitness of the target program. 将字段更改为int或long可以解决问题,具体取决于目标程序的位数。 Which you can find out by looking at the Taskmgr.exe, Processes tab. 您可以通过查看Taskmgr.exe,进程选项卡找到它。 The process name is post-fixed with "*32" if it is a 32-bit process. 如果进程名称是32位进程,则使用“* 32”进行后固定。 Or simply stay out of trouble by setting your project's Target platform setting to match the target process (x86 or AnyCPU). 或者通过将项目的Target平台设置设置为与目标进程(x86或AnyCPU)匹配来避免麻烦。

Debug this by using Debug + Windows + Memory + Memory1. 使用Debug + Windows + Memory + Memory1进行调试。 Put "lpLocalBuffer" in the Address box and observe what you see vs what your code reads. 将“lpLocalBuffer”放在“地址”框中,观察您看到的内容与您的代码所读取的内容。 You should definitely be able to tell from the hex view that you got the string properly. 您绝对应该能够从十六进制视图中判断出您是否正确使用了字符串。 Note that if you see zeros between the string characters then the target process uses the Unicode version of the list view. 请注意,如果在字符串字符之间看到零,则目标进程使用列表视图的Unicode版本。 Marshal.PtrToStringUnicode is then required to read it. 然后需要Marshal.PtrToStringUnicode读取它。

Sorry my response is so late but I just came across the same issue. 对不起,我的回复太晚了,但我遇到了同样的问题。 Here is the structure I used for VB.NET which works on both 32 and 64 bit systems. 这是我用于VB.NET的结构,它适用于32位和64位系统。

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure LV_ITEM
    Public Mask As UInteger
    Public Index As Integer
    Public SubIndex As Integer
    Public State As Integer
    Public StateMask As IntPtr
    Public Text As String
    Public TextLength As Integer
    Public ImageIndex As Integer
    Public LParam As IntPtr
End Structure

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM