【Qt 学习之路】Qt/C++调用微软接口

24 篇文章 1 订阅
21 篇文章 0 订阅

目录

1、调用微软接口杀死进程

2、调用微软系统键盘

3、调用微软接口查看是否某进程正在运行

4、调用微软接口运行某进程


1、调用微软接口杀死进程

有时遇到特殊杀死不了的进程,我们还是要调用系统接口的,下面我将调用微软接口杀死进程代码贴一下:


#include <Windows.h>

#include <tlhelp32.h>



static bool waitCloseProgram(QString proName)

{

#ifdef Q_OS_WIN

    QString name = proName;

    HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

    PROCESSENTRY32* processInfo=new PROCESSENTRY32;

    processInfo->dwSize=sizeof(PROCESSENTRY32);

    int index=0;

    int ID = 0;

    while(Process32Next(hSnapShot,processInfo)!=FALSE)

    {

        index++;

        int size=WideCharToMultiByte(CP_ACP,0,processInfo->szExeFile,-1,NULL,0,NULL,NULL);

        char *ch=new char[size+1];

        if(WideCharToMultiByte(CP_ACP,0,processInfo->szExeFile,-1,ch,size,NULL,NULL)){

            if(strstr(ch,name.toUtf8())) {//进程还在

                ID = processInfo->th32ProcessID;

                HANDLE hProcess;

                // 现在我们用函数 TerminateProcess()终止进程:

                // 这里我们用PROCESS_ALL_ACCESS

                hProcess=OpenProcess(PROCESS_ALL_ACCESS,TRUE,ID);

                if(hProcess==NULL){

                    qDebug()<<"ID ="<<ID;

                }

                TerminateProcess(hProcess,0);

                CloseHandle(hProcess);

                delete[] ch;

                CloseHandle(hSnapShot);

                delete processInfo;

                return true;

            }

        }

        delete[] ch;

    }

    CloseHandle(hSnapShot);

    delete processInfo;

#endif

    return true;//如果进程没查到

}

2、调用微软系统键盘

#include <QProcess>
#ifdef Q_OS_WIN32
#include "Windows.h"
#include <ShellAPI.h>
#endif

void windowsTabTip()
{
    bool ret = false;
    OSVERSIONINFO osvi;
    ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&osvi);
    char* windir = getenv("windir");
    char winroot = 'c';
    if (!windir || strcmp(windir, "")) {
        winroot = windir[0];
    }
    char tabtip[260];
    char tabtipworksp[260];
    if (osvi.dwMajorVersion <= 5) {// XP
        sprintf(tabtip, "%c:\\Windows\\System32\\osk.exe ", winroot);
        sprintf(tabtipworksp, "%c:\\Windows\\System32", winroot);
        ret = QProcess::startDetached(tabtip, QStringList(), tabtipworksp);
    } else if (osvi.dwMajorVersion >= 6) {
        if (osvi.dwMinorVersion >= 2) { // win8, win8.1, Windows Server 2012
            PVOID OldValue;
            BOOL bRet = Wow64DisableWow64FsRedirection(&OldValue);
            ShellExecuteA(NULL, "open", "osk.exe", 0, 0, SW_SHOW);
            if (bRet) {
                Wow64RevertWow64FsRedirection(OldValue);
            }
            return ;
        }
        // vista win7
        sprintf(tabtip, "%c:\\Program Files\\Common Files\\Microsoft Shared\\ink\\TabTip.exe ", winroot);
        sprintf(tabtipworksp, "%c:\\Program Files\\Common Files\\Microsoft Shared\\ink", winroot);
        ret = QProcess::startDetached(tabtip, QStringList(), tabtipworksp);
        if (!ret) {
            sprintf(tabtip, "%c:\\Program Files (x86)\\Common Files\\Microsoft Shared\\ink\\TabTip32.exe ", winroot);
            sprintf(tabtipworksp, "%c:\\Program Files (x86)\\Common Files\\Microsoft Shared\\ink", winroot);
            ret = QProcess::startDetached(tabtip, QStringList(), tabtipworksp);
            if (!ret) {
                HWND hTabTip = ::FindWindowA("IPTip_Main_Window", 0);
                if (hTabTip) {
                    ShowWindow(hTabTip, SW_NORMAL);
                    DWORD WM_DESKBAND_CLICKED = ::RegisterWindowMessage(L"TabletInputPanelDeskBandClicked");
                    ::PostMessage(hTabTip, WM_DESKBAND_CLICKED, 0, 0);
                } else {
                    sprintf(tabtip, "%c:\\Windows\\System32\\osk.exe ", winroot);
                    sprintf(tabtipworksp, "%c:\\Windows\\System32", winroot);
                    ret = QProcess::startDetached(tabtip, QStringList(), tabtipworksp);
                }
            }
        }
    }
}

3、调用微软接口查看是否某进程正在运行

#include <QObject>
#ifdef  Q_OS_WIN
#include <Windows.h>
#include <tlhelp32.h>
#include <ShellAPI.h>
#if QT_VERSION>=0x050000  //window中会重新定义min,所以马上undef min
#undef min
#endif
#endif

static HANDLE win32FindHandle(QString proName);

HANDLE WindowsApi::win32FindHandle(QString proName)
{
    QString name = proName;
    HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    PROCESSENTRY32* processInfo=new PROCESSENTRY32;
    processInfo->dwSize=sizeof(PROCESSENTRY32);
    int index=0;
    int ID = 0;
    while(Process32Next(hSnapShot,processInfo)!=FALSE)
    {
        index++;
        int size=WideCharToMultiByte(CP_ACP,0,processInfo->szExeFile,-1,NULL,0,NULL,NULL);
        char *ch=new char[size+1];
        if(WideCharToMultiByte(CP_ACP,0,processInfo->szExeFile,-1,ch,size,NULL,NULL)){
            if(strstr(ch,name.toUtf8())) {//进程还在
                ID = processInfo->th32ProcessID;
                HANDLE hProcess;
                hProcess=OpenProcess(PROCESS_ALL_ACCESS,TRUE,ID);
                delete[] ch;
                CloseHandle(hSnapShot);
                delete processInfo;
                return hProcess;
            }
        }
        delete[] ch;
    }
    CloseHandle(hSnapShot);
    delete processInfo;
    return 0;
}

4、调用微软接口运行某进程

#include <QObject>
#ifdef  Q_OS_WIN
#include <Windows.h>
#include <tlhelp32.h>
#include <ShellAPI.h>
#if QT_VERSION>=0x050000  //window中会重新定义min,所以马上undef min
#undef min
#endif
#endif
#include <QFile>

static bool runExe(const QString &path);

bool WindowsApi::runExe(const QString &path)
{
    Q_ASSERT(QFile::exists(path));
    bool run = false;
#if defined(Q_OS_WIN)
    SHELLEXECUTEINFO sei={sizeof(SHELLEXECUTEINFO)};
    sei.lpVerb=TEXT("runas");
    WCHAR wfile[256];
    memset(wfile,0, sizeof(wfile));
    path.toWCharArray(wfile);
    sei.lpFile=wfile;//add  application  which you want to run as administrator here
    sei.nShow=SW_SHOWNORMAL;//without this,the windows will be hiden
    run = ShellExecuteEx(&sei);
#else
    run = QProcess::startDetached(path);
#endif
    return run;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沙振宇

你的鼓励将是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值