• c\c++ windows自动打开cmd并进入mysql


    💂 个人主页:pp不会算法v
    🤟 版权: 本文由【pp不会算法v】原创、在CSDN首发、需要转载请联系博主
    💬 如果文章对你有帮助、欢迎关注、点赞、收藏(一键三连)和订阅专栏哦

    奇思妙想系列文章

    一、c\c++ windows自动打开cmd并进入mysql
    二、c++\windows实现qq消息连发,群发器

    每次不用可视化工具查看数据库的时候饭都要win+r->cmd->mysql -u root -p->密码
    虽然不麻烦但是多少也得消耗你几秒钟,秉承着时间就是金钱的观念

    我决定通过windows模拟输入实现快速通过命令行查看mysql数据库

    涉及到的知识:
    windows拉起进程,windows模拟输入,全部用的C/C++

    上代码:

    #include 
    #include 
    #include 
    
    void RunExe()
    {
        TCHAR cmdPath[] = _T("C:\\Windows\\System32\\cmd.exe");
        STARTUPINFO strStartupInfo;
        memset(&strStartupInfo, 0, sizeof(strStartupInfo));
        strStartupInfo.cb = sizeof(strStartupInfo);
        PROCESS_INFORMATION szProcessInformation;
        memset(&szProcessInformation, 0, sizeof(szProcessInformation));
        TCHAR szCommandLine[] = _T("mysql -u root -p");
    
        int iRet = CreateProcess(
            cmdPath,
            NULL,
            NULL,
            NULL,
            false,
            CREATE_NEW_CONSOLE,
            NULL,
            NULL,
            &strStartupInfo,
            &szProcessInformation
        );
    
        if (iRet)
        {
            // 创建成功
            printf_s("Create Success iRet = %d\n", iRet);
    
            // 等待命令行进程启动完毕
            WaitForInputIdle(szProcessInformation.hProcess, 5000);
            INPUT input;
           
            // 模拟输入回车
         
            memset(&input, 0, sizeof(input));
            input.type = INPUT_KEYBOARD;
            input.ki.wVk = VK_RETURN;
            input.ki.dwFlags = 0;
            SendInput(1, &input, sizeof(INPUT));
    
            // 等待命令行处理完回车
            Sleep(1000);
    
            //模拟输入"mysql -u root -p"
            TCHAR szToMysql[] = _T("mysql -u root -p");
            for (int i = 0; i < _tcslen(szToMysql); i++)
            {
                memset(&input, 0, sizeof(input));
                input.type = INPUT_KEYBOARD;
                input.ki.wVk = 0;
                input.ki.dwFlags = KEYEVENTF_UNICODE;
                input.ki.wScan = szToMysql[i];
                SendInput(1, &input, sizeof(INPUT));
                input.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
                SendInput(1, &input, sizeof(INPUT));
            }
    
          //  Sleep(1000);
    
            // 模拟输入回车
    
            memset(&input, 0, sizeof(input));
            input.type = INPUT_KEYBOARD;
            input.ki.wVk = VK_RETURN;
            input.ki.dwFlags = 0;
            SendInput(1, &input, sizeof(INPUT));
    
            // 等待命令行处理完回车
           // Sleep(1000);
    
            // 模拟输入密码 
            TCHAR szPassword[] = _T("你的数据库的密码");
            for (int i = 0; i < _tcslen(szPassword); i++)
            {
                memset(&input, 0, sizeof(input));
                input.type = INPUT_KEYBOARD;
                input.ki.wVk = 0;
                input.ki.dwFlags = KEYEVENTF_UNICODE;
                input.ki.wScan = szPassword[i];
                SendInput(1, &input, sizeof(INPUT));
    
                input.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
                SendInput(1, &input, sizeof(INPUT));
            }
    
            // 模拟输入回车
            memset(&input, 0, sizeof(input));
            input.type = INPUT_KEYBOARD;
            input.ki.wVk = VK_RETURN;
            input.ki.dwFlags = KEYEVENTF_UNICODE;
            input.ki.wScan = VK_RETURN;
            SendInput(1, &input, sizeof(INPUT));
    
          //  Sleep(1000);
    
            // 关闭进程句柄和线程句柄
            CloseHandle(szProcessInformation.hProcess);
            CloseHandle(szProcessInformation.hThread);
        }
        else
        {
            printf_s("Create Failure iRet = %d\n", iRet);
            printf_s("Error code = %d\n", GetLastError());
        }
    }
    
    int main()
    {
        printf("This is Bingo\n");
        RunExe();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116

    演示效果:
    在这里插入图片描述

  • 相关阅读:
    [计算机入门] 设置日期和时间
    思维模型 棘轮效应
    vue nextTick源码分析
    Java的封装
    本科论文数据匹配不上有没有朝阳区群众zhidian
    linux(ARM)架构下QT连接mysql
    【ARK UI】HarmonyOS ETS 资源管理基本使用
    SAP 智能机器人流程自动化(iRPA)解决方案分享
    42. 泛型编程和模板(类模板)
    开关电源测试解决方案之浪涌电流测试 -纳米软件
  • 原文地址:https://blog.csdn.net/weixin_73548574/article/details/132871076