0

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

chrisoo
  • 17
  • 1
  • 6
  • You could add the platform you're using. If you're running on windows, this might help: http://stackoverflow.com/questions/4593493/windows-apis-which-will-show-the-running-processes – MatthiasB Mar 19 '14 at 14:21
  • Do you want to enumerate all process names given at a certain moment OR do you want to be **notified* when the user creates a process? (e.g.: User A open Notepad.exe --> callback function results in message to your own application). – BeschBesch Mar 19 '14 at 14:40
  • I want to be notified when a user creates a process – chrisoo Mar 19 '14 at 14:44

3 Answers3

0

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;
}
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • I don't want to know the name of my own application. Or is the name of every application I use/start passed to main()? – chrisoo Mar 19 '14 at 14:41
  • argv contains the command-line passed by Shell/cmd.exe/something that started the application. For WinMain() it's the third parameter (LPSTR lpCmdStr). Per standard the first argument [argv[0]] contains the name of the application. – BeschBesch Mar 19 '14 at 14:51
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;
}
hyun
  • 2,135
  • 2
  • 18
  • 20
0

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.

BeschBesch
  • 373
  • 2
  • 11
  • is it possible to erase the callbacks when I reach 64 or is this some kind of security limit? I will store the application infos somewhere else then – chrisoo Mar 19 '14 at 14:56
  • are there any examples? – chrisoo Mar 19 '14 at 15:32
  • actually I found a question that might provide all answers you need: [here](http://stackoverflow.com/questions/3489501/how-to-recognize-that-an-application-intends-to-execute-run-a-file). It even can be found on stackoverflow! – BeschBesch Mar 19 '14 at 17:21