对一个程序启动 到 在控件中自动输入

#include "stdafx.h"
#include<iostream>
#include<string>
#include<Windows.h>
#include<ShlObj.h>
#include<Shlwapi.h>
#include<cstring>
#include<io.h>
#include<fstream>
#include<cassert>
#include<vector>
#include<atlconv.h>

using namespace std;

HWND   Ghwnd[100];
int i = 0;

 

string  getDesktopPath()
{
    LPITEMIDLIST pidl;
    LPMALLOC pShellMalloc;
    char szDir[200];
    if (SUCCEEDED(SHGetMalloc(&pShellMalloc)))
    {
        if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl))) {
            // 如果成功返回true  
            SHGetPathFromIDListA(pidl, szDir);
            pShellMalloc->Free(pidl);
        }
        pShellMalloc->Release();
    }

    return string(szDir);
}

void getLinkPath(wchar_t *lpszLink, wchar_t *szPath, wchar_t *szParam)
{
    HRESULT   hres;
    IShellLink*   psl;
    wchar_t   szGotPath[MAX_PATH];
    wchar_t   szArguement[MAX_PATH];
    WIN32_FIND_DATA   wfd;

    *szPath = 0;   //   assume   failure   
    CoInitialize(0);
    //Get a pointer to the IShellLink interface.   
    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID  *)&psl);

    if (SUCCEEDED(hres)) {

        IPersistFile*   ppf;
        //Get a pointer to the IPersistFile interface.   
        hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);

        if (SUCCEEDED(hres)) {

            //WCHAR   wsz[MAX_PATH];   
            //Ensure   that   the   string   is   Unicode.   
            //MultiByteToWideChar(CP_ACP,   0,   lpszLink,   -1,   wsz,   
            //    MAX_PATH);   
            //   Load   the   shortcut.   
            hres = ppf->Load(lpszLink, STGM_READ);
            if (SUCCEEDED(hres)) {

                //Resolve   the   link.   
                hres = psl->Resolve(0, 0);
                if (SUCCEEDED(hres)) {

                    //Get the path to the link target.   
                    hres = psl->GetPath(szGotPath, MAX_PATH, (WIN32_FIND_DATA   *)&wfd, SLGP_SHORTPATH);

                    if (SUCCEEDED(hres))
                        lstrcpy(szPath, szGotPath);

                    hres = psl->GetArguments(szArguement, 256);

                    if (SUCCEEDED(hres))
                        lstrcpy(szParam, szArguement);

                }
            }else {
                cout << "找不到文件:" << lpszLink;
            }
            //Release   the   pointer   to   the   IPersistFile   interface.   
            ppf->Release();
        }
        //Release   the   pointer   to   the   IShellLink   interface.   
        psl->Release();
    }

    if (hres)
        lstrcpy(szPath, lpszLink);
    CoUninitialize();
}
void findAllFile(const char * path, const char * format,char *lnkpath)
{
    string strpath;
    char newpath[1024];
    strcpy(newpath, path);
    strcat(newpath, "\\*.*");    // 在目录后面加上"\\*.*"进行第一次搜索
    int handle;
    _finddata_t findData;
    handle = _findfirst(newpath, &findData);
    if (handle == -1)        // 检查是否成功
        return ;
    while (_findnext(handle, &findData) == 0) {
        if (findData.attrib & _A_SUBDIR) {
            if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
                continue;
            strcpy(newpath, path);
            strcat(newpath, "\\");
            strcat(newpath, findData.name);
            findAllFile(newpath, format, lnkpath);
        }
        else {
            if (strstr(findData.name, format)) {     //判断是不是txt文件
                //cout << findData.name << "\t" << path << "\t" << findData.size << " bytes.\n";
                string temp(path);
                if (temp.find("Desktop") != string::npos) {
                    temp += "\\" + string(format);
                    memcpy(lnkpath, temp.c_str(), temp.size());
                    return ;
                }
            }
        }
    }
    _findclose(handle);    // 关闭搜索句柄
    return ;
}


wstring stringToWstring(string& str)
{
    LPCSTR pszSrc = str.c_str();
    int nLen = MultiByteToWideChar(CP_ACP, 0, pszSrc, -1, NULL, 0);
    if (nLen == 0)
        return std::wstring(L"");

    wchar_t* pwszDst = new wchar_t[nLen];
    if (!pwszDst)
        return std::wstring(L"");

    MultiByteToWideChar(CP_ACP, 0, pszSrc, -1, pwszDst, nLen);
    std::wstring wstr(pwszDst);
    delete[] pwszDst;
    pwszDst = NULL;

    return wstr;
}
void readTxt(string file,string &allstr)
{
    char S[1024];
    ifstream fin;
    fin.open(file);
    if (!fin) {
        
        cout << "读取失败!"<<endl;
        return ;
    }
    while (!fin.eof())
    {
        fin.getline(S, 1024);
        //cout << S << endl;
        
        allstr += string(S);
        allstr += "|";
    }
    fin.close();
    getchar();
    return ;

}
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) 
{
    
    LPWSTR lpString = (LPWSTR)malloc(1024 * sizeof(WCHAR));
    LPWSTR ClassString = (LPWSTR)malloc(1024 * sizeof(WCHAR));
    GetWindowText(hwnd, lpString, 1024);
    GetClassName(hwnd, ClassString, 1024);
    if (wcscmp(ClassString, _T("Edit"))==0) {

        Ghwnd[i] = hwnd;
        i++;
    }
    //wprintf(L"%s\n", lpString);
    return true;
}

HWND FindTextBox(HWND hWnd)
{
    HWND hEdit = NULL;
    hEdit = FindWindowExW(hWnd, hEdit, L"Edit",0);
    if (hEdit != NULL) return hEdit;

    HWND hChild = NULL;
    for (;;) {
        wchar_t windowname[1024 * 2];
        hChild = FindWindowExW(hWnd, hChild, 0,0);
        if (hChild == NULL)
            break;
        EnumChildWindows(hChild, EnumChildProc, NULL);
        hEdit = FindTextBox(hChild);
        if (hEdit != NULL) {
            break;
        }
    }
    return hEdit;
}

void encryption(string& c, int a[]) {
    for (int i = 0, j = 0; c[j]; j++, i = (i + 1) % 7) {

        c[j] += a[i];

        if (c[j] > 122) c[j] -= 90;
    }
}
void decode(string& c, int a[]) {
    for (int i = 0, j = 0; c[j]; j++, i = (i + 1) % 7) {

        c[j] -= a[i];

        if (c[j] < 32) c[j] += 90;
    }
}
//根据特定符号分离字符串
void supersplit(const string& s,vector<string>& v, const string& c)
{
    string::size_type pos1, pos2;
    size_t len = s.length();
    pos2 = s.find(c);
    pos1 = 0;
    while (string::npos != pos2)
    {
        v.emplace_back(s.substr(pos1, pos2 - pos1));

        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if (pos1 != len)
        v.emplace_back(s.substr(pos1));
}

int main()
{
    HINSTANCE hNewExe;
    //string pathdesk = getDesktopPath();
    int SelectNum = 0;
    string account;
    string password;
    HWND hWnd;
    PROCESS_INFORMATION pro_info; //进程信息  
    STARTUPINFO sti; //启动信息  
                     //......  
                     // 初始化两个结构体  
    ZeroMemory(&pro_info, sizeof(PROCESS_INFORMATION));
    ZeroMemory(&sti, sizeof(STARTUPINFO));
    while (1) {

        string readText;
  
        cin >> SelectNum;
        switch (SelectNum)
        {

        case 1:
            {
                vector<string> vall;
                vector<string> vtempcunt;
                vector<string> vtempw;
                string FilePath;
                //cout << "请输入文件路径(输入0为读取默认文件):";
                //cin >> FilePath;
                //if (FilePath == "0")
                FilePath = "./test.txt";
                readTxt(FilePath, readText);
                supersplit(readText, vall, "|");
                supersplit(vall.at(0), vtempcunt, ":");
                account = vtempcunt.at(1);
                supersplit(vall.at(1), vtempw, ":");
                password = vtempw.at(1);
            }
            break;
        case 2:
            {
                
                string lnkStr;
                wchar_t exePath[700] = { 0 };
                wstring param;
                char temppath[1024] = { 0 };
                //cout << "请输入要运行的快捷方式:(例:腾讯QQ)";
                //cin >> lnkStr;
                lnkStr = "TeamViewer 14";
                lnkStr += ".lnk";
                //获取lnk快捷方式
                findAllFile("C:\\Users", lnkStr.c_str(), temppath);
                string PathDesk(temppath);
                wstring lnkPath = stringToWstring(PathDesk);
                //获取exe的绝对路径
                getLinkPath(const_cast<wchar_t *>(lnkPath.c_str()), exePath, const_cast<wchar_t *>(param.c_str()));
                //运行exe
                CreateProcess(exePath, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &sti, &pro_info);
                //hNewExe=ShellExecute(NULL,_T("open"), exePath,NULL,NULL, SW_SHOWNORMAL);
                if ((DWORD)hNewExe <= 32){
                    printf("return value:%d\n", (DWORD)hNewExe);
                }
                else{
                    printf("successed!\n");
                }
                printf("GetLastError: %d\n", GetLastError());

            }
            break;
        case 3:
            { 
                int a[] = { 4, 9, 6, 2, 8, 7, 3 };
                wchar_t windowname[1024*2];
                hWnd = FindWindowW(0, L"TeamViewer");
                if (hWnd == 0) {
                    puts("查找窗口失败!");
                    return 1;
                }
                GetWindowText(hWnd, windowname,200);
                HWND hEdit = NULL;
                hEdit = FindTextBox(hWnd);
                GetWindowText(hEdit, windowname, 200);
                if (hEdit != NULL) {
                    USES_CONVERSION;
                    //encryption(account,a);
                    
                    SendMessageW(hEdit, WM_SETTEXT, 0, (LPARAM)A2W(account.c_str()));
    
                    //encryption(password, a);
                    //SendMessageW(Ghwnd[1], ES_PASSWORD, 0, 0);
                    //SendMessageW(Ghwnd[1], EM_SETPASSWORDCHAR, 0, (LPARAM)L"*");
                    SendMessageW(Ghwnd[1], WM_SETTEXT, 0, (LPARAM)A2W(password.c_str()));
                    //SendMessageW(Ghwnd[1], EM_SETPASSWORDCHAR, 0, (LPARAM) L"*");
                }
                else {
                    puts("获取编辑框失败!");
                    return 1;
                }

            }
            break;
        case 4:
            {    
                hWnd = FindWindowW(0, L"TeamViewer");
                if (hWnd == 0) {
                    puts("查找窗口失败!");
                    return 1;
                }
                SendMessageW(hWnd, WM_CLOSE, 0, 0);
                DWORD exitCode; //退出码  
                                //.........  
                GetExitCodeProcess(pro_info.hProcess, &exitCode); //获取退出码  
                TerminateProcess(pro_info.hProcess, exitCode);
                // 关闭句柄  
                CloseHandle(pro_info.hThread);
                CloseHandle(pro_info.hProcess);
            }
            break;
        case 5:
            system("cls");
            break;
        case 6:
            cout << "退出" << endl;
            return 0;
        }
        
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值