2016-04-15 88 views
-1

正如我的标题所述,我试图从我正在编写的C++应用程序中移动数据,并将它输入到Windows中的任何桌面应用程序的字段(特别是用户名和密码字段)中。它需要适用于所有应用程序。将数据粘贴到任何应用程序输入字段中,而不模拟Ctrl + V。 Windows C++

现在我已经写了一个小程序,它将数据复制到剪贴板,然后模拟Ctrl + V键盘按下来粘贴数据。然而,这样做感觉像一个非常丑陋的方式来做到这一点。我的问题是,有没有更好的方法?

Ps。从我做的一切似乎都要求你以某种方式修改接收应用程序。不幸的是这个选项对我来说不可用。因此,任何涉及调整接收应用程序的解决方案都不会有帮助。

谢谢你的帮助!

+0

应该有几个例子,告诉你如何做到这一点:http://stackoverflow.com/questions/2113950/how-to-send-keystrokes-to-a-window。如果您需要设置输入字段的焦点,我确信有办法做到这一点 – Marged

+0

谢谢!这绝对看起来更好,当我第一次遇到这种情况时,我的印象是,这不适用于所有应用程序。但似乎我错了。我现在试着去实现它。 此外,我是新来的stackoverflow,我现在做的正确的事情是什么?关闭这个问题?以某种方式向上推动你? – SledoMalset

+0

@Marged:这要求,目的地确实是一个本地窗口(可通过它的'HWND'访问)。通常情况并非如此,解决方案将无法工作。记录和支持的用于自动化UI的解决方案是[UI自动化](https://msdn.microsoft.com/en-us/library/windows/desktop/ee684009.aspx)。 – IInspectable

回答

0

将键击发送到另一个应用程序不是一个好的解决方案。有很多潜在的问题,例如C# sendkeys to other application to particular textfield。更好的解决方案是更直接地与其他程序进行交互。它需要对Windows的工作方式有更多的技术理解。其中许多优点之一是,您可以像编写它一样轻松地阅读其他应用程序中的文本。

有关示例,请参阅我的Clicking a Button in Another Application,但使用C#。我希望这个解释至少是有帮助的。可以使用相同的技术将数据放入文本框或文本框中,然后单击按钮。 WM_SETTEXT message将用于将数据放入另一个应用程序的文本框中。以下是将文本放入记事本的示例控制台程序。

#include "stdafx.h" 

struct pidandhwnd { 
    DWORD dwProcessId; 
    HWND hwnd; 
}; 

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) 
{ 
    pidandhwnd *ppnh = (pidandhwnd *)lParam; 
    DWORD dwProcessId; 
    GetWindowThreadProcessId(hwnd, &dwProcessId); 
    if (ppnh->dwProcessId == dwProcessId) 
    { 
     ppnh->hwnd = hwnd; 
     return FALSE; 
    } 
    return TRUE; 
} 

int main() 
{ 
    TCHAR szCmdline[] = TEXT("Notepad.exe"); 
    PROCESS_INFORMATION piProcInfo; 
    STARTUPINFO siStartInfo; 
    BOOL bSuccess = FALSE; 

    ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION)); 
    ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); 
    siStartInfo.cb = sizeof(STARTUPINFO); 
    siStartInfo.hStdError = NULL; 
    siStartInfo.hStdOutput = NULL; 
    siStartInfo.hStdInput = NULL; 

    LPARAM lParam = NULL; 
    pidandhwnd pnh; 

    const int ControlId = 15; // Edit control in Notepad 
    HWND hEditWnd; 

    bSuccess = CreateProcess(NULL, 
     szCmdline,  // command line 
     NULL,   // process security attributes 
     NULL,   // primary thread security attributes 
     TRUE,   // handles are inherited 
     0,    // creation flags 
     NULL,   // use parent's environment 
     NULL,   // use parent's current directory 
     &siStartInfo, // STARTUPINFO pointer 
     &piProcInfo); // receives PROCESS_INFORMATION 
    if (!bSuccess) { 
     std::cout << "Process not started\n"; 
     return 0; 
     } 
    std::cout << piProcInfo.dwProcessId << " Notepad Process Id\n"; 

    WaitForInputIdle(piProcInfo.hProcess, 1000); 

    pnh.dwProcessId = piProcInfo.dwProcessId; 
    pnh.hwnd = NULL; 
    EnumDesktopWindows(NULL, EnumWindowsProc, (LPARAM)&pnh); 
    if (pnh.hwnd == NULL) 
    { 
     std::cout << "Notepad not found\n"; 
     return 0; 
    } 
    //std::cout << "Notepad found\n"; 

    // Get the edit box on Notepad 
    hEditWnd = GetDlgItem(pnh.hwnd, ControlId); 
    // Send the text 
    SendMessage(hEditWnd, WM_SETTEXT, NULL, (LPARAM)_T("This is from somewhere else.")); 

    return 0; 
} 
+0

非常感谢您的详细解答!(对于我的反应迟缓,感到抱歉)如果没有问题,我有几个后续问题。 点击一个按钮的例子似乎可能对我非常有用,但我仍然不是100%的操作系统知识。是否有应用程序不受间谍++的影响?也就是说,是否有特定的应用程序类别,我无法访问文本框(在我的情况下是用户名/密码),还是操作系统使得这些字段总是可以被窥探到? – SledoMalset

+0

我很抱歉没有看到这更快。这不适用于使用较新技术的软件,但我不知道是否有相应的解决方案。我也不知道应用程序能够如何保护自己免受这种事情的影响。他们可以但可能不完全。你可以创建一个新的问题来询问具体细节。 – user34660

相关问题