jeudi 25 mai 2017

How to get process name, process id and tittle of activ window?

I need yo get process name, process id and tittle of active window(not for all active windows).

LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    //while(is_working_)
    if (nCode < 0)
        return CallNextHookEx(NULL, nCode, wParam, lParam);
    switch (nCode)
    {
    case  HCBT_ACTIVATE:
        break;
    case HCBT_CREATEWND: 
        break;
    case HCBT_DESTROYWND:
        break;
    case HCBT_MINMAX:
        break;
    case HCBT_MOVESIZE:
        break;
    case HCBT_SETFOCUS:
        break;
    case HCBT_SYSCOMMAND:
        break;
    default:
        break;
    }
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

I have two functions. First(Get window tittle) and Second(Get window process id and process name). First:

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    hwnd = GetForegroundWindow(); //get handle of currently active window
    if (IsWindowVisible(hwnd)) // check whether window is visible
    {
        char wnd_title[256];
        GetWindowText(hwnd, wnd_title, sizeof(wnd_title));
        std::cout << wnd_title << std::endl;
    }
    return true; // function must return true if you want to continue enumeration
}

Second:

void PrintProcessNameAndID(DWORD processID)
{
    TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");

    // Get a handle to the process.

    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
        PROCESS_VM_READ,
        FALSE, processID);

    // Get the process name.

    if (NULL != hProcess)
    {
        HMODULE hMod;
        DWORD cbNeeded;

        if (EnumProcessModules(hProcess, &hMod, sizeof(hMod),
            &cbNeeded))
        {
            GetModuleBaseName(hProcess, hMod, szProcessName,
                sizeof(szProcessName) / sizeof(TCHAR));
        }
    }

    // Print the process name and identifier.

    _tprintf(TEXT("%s  (PID: %u)\n"), szProcessName, processID);

    // Release the handle to the process.

    CloseHandle(hProcess);
}

Is it possible to make one function?

Aucun commentaire:

Enregistrer un commentaire