简体   繁体   中英

Function “SendMessage” of User32.dll does not work in Windows 8

I am developing a piece of software to extract some information from a external desktop application, the thing is that i am working in Windows 7 using C# and I have got a workable application that uses the stuff of here:

How can I retrieve the values from window of class "ThunderRT6ListBox" using user32.dll in c#

But the problem comes when I test the same software against the same external application in Windows 8, it doesn't work in the same way.

I can copy the handler id of the controls but when I use the SendMessage function, for example, to put a new text to a edit control (textbox), nothing is happened.

    public static void SetText(IntPtr HWnd, string strTextToSet)
    {
        var text = new StringBuilder(strTextToSet);
        SendMessage(HWnd, WM_SETTEXT, IntPtr.Zero, text);
    }

Is there any consideration in Windows 8 when it used this stuff?

UPDATE:

Still it does not work. GetText works, but SetText doesn't.

My code:

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]        
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    const uint WM_SETTEXT = 0x000C;

    public static string GetText(IntPtr hwnd)
    {
        var text = new StringBuilder(1024);
        if (SendMessageTimeoutText(hwnd, 0xd, 1024, text, 0x2, 1000, 0) != 0)
        {
            return text.ToString();
        }

        return "";
    }

    public static void SetText(IntPtr HWnd, string strTextToSet)
    {
        //var text = new StringBuilder(strTextToSet);
        IntPtr text = System.Runtime.InteropServices.Marshal.StringToCoTaskMemUni(strTextToSet);
        SendMessage(HWnd, WM_SETTEXT, IntPtr.Zero, text);
    }

UPDATE 2:

Error code:

    ModApi.SetText(hwEdit, "foo_namE");

    //The textbox does not change

    int error_code = Marshal.GetLastWin32Error();

    //error_code is 5

解决方案是关于UAC(用户访问控制),我需要以管理模式(或在构建可执行文件时)启动Visual Studio。

Per this ( SendMessage WM_SETTEXT to TextBox doesn't trigger TextChanged Event ) other question on SO, the following works on my system in a Windows 8 forms application:

//Declarations
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg,
            IntPtr wParam, IntPtr lParam);

        private const uint WM_SETTEXT = 0x000C;


//SetText function
        public static void SetText(IntPtr HWnd, string strTextToSet)
        {
            IntPtr text = System.Runtime.InteropServices.Marshal.StringToCoTaskMemUni(strTextToSet);
            SendMessage(HWnd, WM_SETTEXT, IntPtr.Zero, text);
            System.Runtime.InteropServices.Marshal.FreeCoTaskMem(text);

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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