window下C++开发常用函数

个人记录常用函数总结。
环境要求window。在vs2013运行通过。

/*
常用函数总结。
环境要求window。
在vs2013运行通过。

作者:chenlin
日期:2018-09-05
*/


#include <string>
#include <string.h>
#include <windows.h>
#include <TLHELP32.H>
#include "commonFunction.h"


/*多字节转宽字节*/
std::wstring MBytesToWString(const char* lpcszString)
{
    int len = strlen(lpcszString);
    int unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, lpcszString, strlen(lpcszString), NULL, 0);
    wchar_t* pUnicode = new wchar_t[unicodeLen + 1];
    memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
    ::MultiByteToWideChar(CP_ACP, 0, lpcszString, strlen(lpcszString), (LPWSTR)pUnicode, unicodeLen);
    std::wstring wString = (wchar_t*)pUnicode;
    delete[] pUnicode;
    return wString;
}

/*宽字节转多字节*/
std::string WStringToMBytes(const wchar_t* lpwcszWString)
{
    char* pElementText;
    int iTextLen;
    // wide char to multi char
    iTextLen = ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, wcslen(lpwcszWString), NULL, 0, NULL, NULL);
    pElementText = new char[iTextLen + 1];
    memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));
    ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, wcslen(lpwcszWString), pElementText, iTextLen, NULL, NULL);
    std::string strReturn(pElementText);
    delete[] pElementText;
    return strReturn;
}

//查找进程:成功返回进程id
DWORD GetProcessidFromName(LPCTSTR name)
{
    PROCESSENTRY32 pe;
    DWORD id = 0;
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    pe.dwSize = sizeof(PROCESSENTRY32);
    if (!Process32First(hSnapshot, &pe))
        return 0;
    while (1)
    {
        pe.dwSize = sizeof(PROCESSENTRY32);
        if (Process32Next(hSnapshot, &pe) == FALSE)
            break;
        if (wcscmp(pe.szExeFile, name) == 0)
        {
            id = pe.th32ProcessID;

            break;
        }


    }
    CloseHandle(hSnapshot);
    return id;
}


//根据进程名称关闭进程,找到进程并且关闭返回true
BOOL closeProcessFromName(LPCTSTR strProcessName, DWORD dwTH32ProcessID)
{
    BOOL bResult = FALSE;
    if (NULL == strProcessName)
    {
        return FALSE;
    }
    do 
    {
        if (NULL != dwTH32ProcessID)//如果已经知道进程ID,直接结束进程
        {
            HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, dwTH32ProcessID);
            bResult = TerminateProcess(hProcess, 0);
            break;
        }

        HANDLE handle32Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        if (INVALID_HANDLE_VALUE == handle32Snapshot)
        {
            break;
        }
        PROCESSENTRY32 pEntry;
        pEntry.dwSize = sizeof(PROCESSENTRY32);
        //Search for all the process and terminate it
        if (Process32First(handle32Snapshot, &pEntry))
        {
            BOOL bFound = FALSE;
            if (!wcscmp(pEntry.szExeFile, strProcessName))
            {
                bFound = TRUE;
            }

            while ((!bFound) && Process32Next(handle32Snapshot, &pEntry))
            {
                if (!wcscmp(pEntry.szExeFile, strProcessName))
                {
                    bFound = TRUE;
                }
            }
            if (bFound)
            {
                CloseHandle(handle32Snapshot);
                HANDLE handLe = OpenProcess(PROCESS_TERMINATE, FALSE, pEntry.th32ProcessID);
                bResult = TerminateProcess(handLe, 0);  //结束进程
                break;
            }
        }
        CloseHandle(handle32Snapshot);
    } while (0);
    Sleep(500);
    return bResult;
}

//获取应用程序目录

std::wstring getAppDir()
{
    int iResult = 1;
    wchar_t szapipath[MAX_PATH] = {0}; 
    GetModuleFileName(NULL, szapipath, MAX_PATH); 
    std::wstring strFileName = szapipath;
    int pos = strFileName.find_last_of(L"\\"); 
    if (pos != -1){
        iResult = 0;
        szapipath[pos + 1] = 0;
        strFileName = szapipath;
        //wcscpy_s(szDir, szapipath);
    } 
    return strFileName;
}

猜你喜欢

转载自blog.csdn.net/a013152/article/details/82421771