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

每次不用可视化工具查看数据库的时候饭都要win+r->cmd->mysql -u root -p->密码

虽然不麻烦但是多少也得消耗你几秒钟,秉承着时间就是金钱的观念

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

涉及到的知识:

windows拉起进程,windows模拟输入,全部用的C/C++

上代码:

cpp 复制代码
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

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;
}

演示效果:

相关推荐
WZF-Sang4 分钟前
计算机网络基础——1
网络·c++·git·学习·计算机网络·智能路由器
默凉20 分钟前
C++ 虚函数(多态,多重继承,菱形继承)
开发语言·c++
秋说33 分钟前
【PTA数据结构 | C语言版】在顺序表 list 的第 i 个位置上插入元素 x
c语言·数据结构·list
小庞在加油1 小时前
《重构项目》基于Apollo架构设计的项目重构方案(多种地图、多阶段、多任务、状态机管理)
c++·重构·apollo架构
楼田莉子1 小时前
数据学习之队列
c语言·开发语言·数据结构·学习·算法
石头wang1 小时前
如何在idea里快速地切换Windows CMD、git bash、powershell
windows·git·bash·intellij-idea
秋说2 小时前
【PTA数据结构 | C语言版】返回单链表 list 中第 i 个元素值
c语言·数据结构·list
让我们一起加油好吗2 小时前
【基础算法】贪心 (四) :区间问题
c++·算法·贪心算法·洛谷
双叶8362 小时前
(C++)任务管理系统(正式版)(迭代器)(list列表基础教程)(STL基础知识)
c语言·开发语言·数据结构·c++·list
七七七七072 小时前
类与对象【下篇】-- 关于类的其它语法
c语言·开发语言·c++