Question

I want to get the application name of any application the user starts but I don't know how to achieve this and I want to do this in an console application.

Does I need to hook in the newly started application to get their name or get it out of the taskmanager?

edit: The platform is windows

Was it helpful?

Solution

If you want to set up a kind of callback whenever one process is created, you should have a look at PsSetCreateProcessNotifyRoutineEx and the userCreateProcessNotifyEx. The PS_CREATE_NOTIFY_INFO-struct contains information about the app-name (ImageFileName) or even its path (see Link for more information).

To remove the callback simply set the second parameter of PsSetCreateProcessNotifyRoutineEx to TRUE.

Up-sides: Besides the installation of WDK not much to implement.

Down-sides:

You need the Windows Driver Kit (WDK) and a copy of VisualC++. (header-files)
You can only install a certain amount of hooks (64) --> should be enough
Use of a callback attached at driver level for filename.

Alternatives:

EnumWindow()-call in with worker-thread/timer-function.

OTHER TIPS

The name of the application is passed to main() as the first value of argv[]:

int main(int argc, char** argv) {
    std::cout << "app name is: " << argv[0] << std::endl;
    return 0;
}

If your goal is just showing the list of running applications in desk top window. (so not include system processes)

EnumWindows function can be a good method.

Here is a little sample code.

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <iostream>

using namespace std;

#pragma comment(lib, "user32.lib")

int window_num1=0;

BOOL CALLBACK MyEnumProc(HWND hWnd, LPARAM lParam)
{
    TCHAR title[256] = {0,};
    if (IsWindowVisible(hWnd) && GetWindowTextLength(hWnd) > 0)
    {
        window_num1++;
        GetWindowText(hWnd, title, _countof(title));
        _tprintf(_T("Value is %d, %s\n"), window_num1, title);
    }
    return TRUE;
}

int main() 
{
    EnumWindows(MyEnumProc, 0);

    getchar();
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top