Click here to Skip to main content
16,017,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii..
I want to make and event to get the application name whenever user runs any application.
so far i've found this from
Md. Rashim uddin
C#
public partial class Form1 : Form
    {
        public System.Management.ManagementEventWatcher mgmtWtch;
 
        public Form1()
        {
            InitializeComponent();
            mgmtWtch = new System.Management.ManagementEventWatcher("Select * From Win32_ProcessStartTrace");
            mgmtWtch.EventArrived += new System.Management.EventArrivedEventHandler(mgmtWtch_EventArrived);
            mgmtWtch.Start();
        }
 
        private void mgmtWtch_EventArrived(object sender, System.Management.EventArrivedEventArgs e)
        {
            MessageBox.Show((string)e.NewEvent["ProcessName"]);
        }       
    }


this get the process name but i want the application name...
is there is any way to get that from process name..

thanks for your time.
Posted

Here is a C++ project that solves your problem:
Detecting Windows NT/2K process execution[^]

You can either create a C++/CLI project based on the code, or use the DllImportAttribute[^] to access the required native functions from c#

[Update]
C++
#include <cstdio>
#include <windows.h> 
#include <tlhelp32.h> 
 
int main( int, char *[] ) 
{ 
 PROCESSENTRY32 entry; 
 entry.dwSize = sizeof(PROCESSENTRY32); 
 
 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); 
 
 if (Process32First(snapshot, &entry) == TRUE) 
 { 
  while (Process32Next(snapshot, &entry) == TRUE) 
  {
   char buffer[512] = {0,};
   DWORD bufferSize = sizeof(buffer) - 1;
   HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); 
 
   QueryFullProcessImageName(hProcess,PROCESS_NAME_NATIVE,buffer,&bufferSize);
 
   CloseHandle(hProcess); 
  } 
 } 
 
 CloseHandle(snapshot); 
 
 return 0; 
} 

The szExeFile member of the PROCESSENTRY32 structure does not always contain the full path.

All of the functions are documented in MSDN Process and Thread Functions[^]

Tool Help Functions[^]

PsSetCreateProcessNotifyRoutine[^] is exported from NTOSKRNL and only available to drivers - the article refered to above explains how you can get a notification from your driver to a user process whenever a process is started.

Obviously it's easier to follow Mikas example - just use a timer and execute it every 10 seconds or so.

Best regards
Espen Harlinn
 
Share this answer
 
v5
Comments
XeeShaN AbbAs 6-Sep-11 18:26pm    
how could i use the DllimportAttribute...
i dont know about that...
can u tell me the specific dll and function for get application name from process
Sergey Alexandrovich Kryukov 6-Sep-11 19:05pm    
Google: "DllImportAttribute site:Microsoft.com" or press F1 in Visual Studio and perform Search...
--SA
Philippe Mori 6-Sep-11 19:27pm    
I think you already have all you need in the answer and related links. Functions anme are above and the corresponding DLL would be found in the documentation.
Sergey Alexandrovich Kryukov 6-Sep-11 19:05pm    
My 5.
--SA
Espen Harlinn 6-Sep-11 19:09pm    
Thank you Sergey!
Here is why you have this problem: for an arbitrary Windows application, there is no such concept as "Application name". You cannot learn something which does not exist.

Actually, it's .NET assembly that has some typically used attributes such as assembly name and product name, but none of them is called "application name". Assembly and application are different things. To find assembly attributes, you should load one and use Reflection. The problem is: only some of the processes are .NET applications.

As Mika already explained, there is also a main application window title. It may or may not exist and — trouble again — this is not "application name". No, you cannot learn something which does not exist.

—SA
 
Share this answer
 
Comments
Wendelius 7-Sep-11 1:08am    
Good answer, my 5
Sergey Alexandrovich Kryukov 7-Sep-11 1:15am    
Thank you, Mika.
--SA
Espen Harlinn 7-Sep-11 4:46am    
Good points :)
Sergey Alexandrovich Kryukov 7-Sep-11 9:28am    
Thank you, Espen.
--SA
XeeShaN AbbAs 7-Sep-11 8:19am    
thank you for your reply, I think I got what I need.. :)
With application name, do you mean the main title of a window. If that's the case and yo ualready know the process, you can use Process.MainWindowTitle Property[^].

If you know only the process id you can use Process.GetProcessById Method[^] to resolve the Process object.
 
Share this answer
 
Comments
XeeShaN AbbAs 6-Sep-11 17:34pm    
i knw the proccess name and i want the application name of corresponding process.
for example for process "chrome.exe" i want the name "Google Chrome". not main title of window...
thanks.
Wendelius 6-Sep-11 17:41pm    
Yes, I understood that. GetProcess methods won't launch the application, they find the already running process.

If you're using Visual Studio, it should have "devenv" as process name. In the debugger, try adding the following to the watches and it should bring the text from the title bar from visual studio to your watch window:

System.Diagnostics.Process.GetProcessesByName("devenv")[0].MainWindowTitle;

If that doesn't find the process you can try with some other process name you know that is running. The indexer 0 is used because you may have multiple visual studios open and all their process names are devenv. The example takes the first one.
XeeShaN AbbAs 6-Sep-11 17:53pm    
thanks for you reply that solve half of my problem but i dont want the main window title i want the application name for example for "devenv" i want "visual Studio 2010"
Wendelius 6-Sep-11 18:12pm    
That's a bit more tricky. I think you have two options.

One is that you read the product information from the file:

System.Diagnostics.Process.GetProcessesByName("devenv")[0].MainModule.FileVersionInfo.ProductName

or the other is that you take the file name from the MainModule (see previous example) and try to locate the shortcut in the start menu and take the name from the shortcut.
XeeShaN AbbAs 6-Sep-11 18:27pm    
thanks thats not specific thats little bit of generic for example for winword it show microsoft office 2010 but this will work for me thanks again...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900