原创

Qt/C++调用微软接口

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://shazhenyu.blog.csdn.net/article/details/52412464

调用微软接口杀死进程

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


#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;//如果进程没查到

}

调用微软系统键盘

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

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

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

调用微软接口运行某进程

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

 

文章最后发布于: 2016-09-02 11:30:27
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 代码科技 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览