How to get the name of current application in C# - c#

I want to get the name of current application that it's running on windows.
For example, if i work with i.e, this function give "Internet explorer" or if i work with Google chrome, give Chrome...

System.AppDomain.CurrentDomain.FriendlyName
or maybe
System.Reflection.Assembly.GetExecutingAssembly()
or if you mean that you want the name of the active window then you should look at;
How do I get the title of the current active window using c#?

You can use Assembly.FullName
System.Reflection.Assembly.GetEntryAssembly().FullName;
Please also take a look at this question:
How do I get the name of the current executable in C#?

Try this, If you have used GetWindowThreadProcessId.
public String GetActiveFileNameTitle()
{
IntPtr hWnd = GetForegroundWindow();
uint procId = 0;
GetWindowThreadProcessId(hWnd, out procId);
var proc = Process.GetProcessById((int)procId);
if (proc != null)
{
return proc.MainModule.FileVersionInfo.ProductName;
}
}

Related

How to get windows application text control value using control id

Using SPY++ I got window handle and using it I can get text control value from my code, but when I trying to find this window handle by application parent handle and control id through GetDlgItem(handle, ButtonId) function it throws error.
const int TextCtrlId = 0x0000E910;
IntPtr handle = IntPtr.Zero;
Process[] localAll = Process.GetProcesses();
foreach (Process p in localAll)
{
if (p.MainWindowHandle != IntPtr.Zero)
{
ProcessModule pm = GetModule(p);
if (pm != null && p.MainModule.FileName == fn)
handle = p.MainWindowHandle;
}
}
if (handle == IntPtr.Zero)
{
Console.WriteLine("Not found");
return;
}
GetText getText = new GetText();
Console.WriteLine("{0:X}", handle);
IntPtr hWndButton = GetDlgItem(handle, TextCtrlId);
string myText = getText.GetControlText(hWndButton);
Console.WriteLine("Error Code = {0}", GetLastError());
Console.WriteLine("Iput Window Text !!! = {0}", myText);
error code is
ERROR_CONTROL_ID_NOT_FOUND
1421 (0x58D)
Control ID not found.
but when I use direct handle (got it from SPY++) from function GetDlgItem(handle) I got same control id which in SPY++.
Control ID is different from Windows Handle. What you have is a Windows Handle (HWND).
Historically you could call GetWindowText() providing the handle to get the text of the control. But limitations have been imposed on this call (Since Windows XP if I remember well) for security reasons to counter password sniffing. Modern control inspection software depend on advanced techniques such as DLL injection to access controls in other processes with no restrictions (inject code in target process to read the control).

MacOs active window title using C#

So I have a C# application using Mono and Gtk+2 running on Mac
Is there some way to get active application window title?
https://stackoverflow.com/a/37368813/3794943 says that I need CGWindowListCopyWindowInfo (I already have the rest of their recipe, like process identifier, front most application, etc.).
Where can I get CGWindowListCopyWindowInfo or something similar from? Or is there some other way to get active window title when using mono on mac os?
Ok, finally found it here: https://forums.xamarin.com/discussion/comment/95429/#Comment_95429
At first you import that function like this:
[DllImport(#"/System/Library/Frameworks/QuartzCore.framework/QuartzCore")]
static extern IntPtr CGWindowListCopyWindowInfo(CGWindowListOption option, uint relativeToWindow);
Then you call it like this:
string result = null;
IntPtr windowInfo = CGWindowListCopyWindowInfo(CGWindowListOption.OnScreenOnly, 0);
NSArray values = (MonoMac.Foundation.NSArray)Runtime.GetNSObject(windowInfo);
for (ulong i = 0, len = values.Count; i < len; i++)
{
NSObject window = Runtime.GetNSObject(values.ValueAt(i));
NSString key = new NSString("kCGWindowOwnerPID");
NSNumber value = (MonoMac.Foundation.NSNumber)window.ValueForKey(key);
// and so on
}
P.S. MonoMac package must be added using NuGet

Obtaining tab title/text using process ID

I don't want to use SetForegroundWindow(), sending keyboard keys or similar techniques, because that can cause issues (unexpected behaviour) in my software.
I have tried to find the title using Cheat Engine program (but haven't found anything useful as Google Chrome seems to work "upside-down").
So I went step ahead, using Process Hacker program I have realized that there is a parent (chrome.exe) process with a valid window handle to the current active tab and all other chrome processes are children of it a.k.a. background processes (with invalid window handle).
By browsing deeper into windows of chrome.exe (parent process), I have found the class name of the window handle being "Chrome_WidgetWin_1" and current active tab's title/text.
Here's a picture of Google Chrome's Task Manager.
I'm looking for a function in C# or C or C++ that will take an integer (process ID) and return a string (tab title/text).
static string GetChromeTabTitle(uint processId)
{
// Assuming I call this function with valid process identifier (PID).
// What do I do next, here??
}
The best way I have found is by using the System.Windows.Automation library. It allows interacting with an application (primarily for accessibility purposes), but you can use it for other purposes like getting Chrome tabs.
Note that this will only work when the Chrome windows is not minimized.
The process is not exactly simple, if you want you can look how I did it in my own project, though it's not something you can just copy it paste, you'll find what you need in the ChromeTabsFinder: https://github.com/christianrondeau/GoToWindow/blob/master/GoToWindow.Plugins.ExpandBrowsersTabs/Chrome/ChromeTabsFinder.cs
Here's the code (you'll need the automation librairies):
public IEnumerable<ITab> GetTabsOfWindow(IntPtr hWnd)
{
var cacheRequest = new CacheRequest();
cacheRequest.Add(AutomationElement.NameProperty);
cacheRequest.Add(AutomationElement.LocalizedControlTypeProperty);
cacheRequest.Add(SelectionItemPattern.Pattern);
cacheRequest.Add(SelectionItemPattern.SelectionContainerProperty);
cacheRequest.TreeScope = TreeScope.Element;
AutomationElement tabBarElement;
using (cacheRequest.Activate())
{
var chromeWindow = AutomationElement.FromHandle(hWnd);
var mainElement = chromeWindow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
if (mainElement == null)
yield break;
tabBarElement = mainElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "tab"));
}
if(tabBarElement == null)
yield break;
var tabElements = tabBarElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "tab item"));
for (var tabIndex = 0; tabIndex < tabElements.Count; tabIndex++)
{
yield return "Tab: " + tabElements[tabIndex].Current.Name + ", Index: " + tabIndex + 1;
}
}

programmatically minimize an application using c#

I have the following code snippet
List<String> sensitiveApps = testConnection.SelectSensitive();
foreach (string sensitiveApp in sensitiveApps)
{
Console.Write(sensitiveApp);
// retrieve applications to minimize handle (connect to database and systematically minimize all applications?)
IntPtr hwnd = UnsafeNativeMethods.FindWindow(sensitiveApp, null);
StringBuilder stringBuilder = new StringBuilder(256);
UnsafeNativeMethods.GetWindowText(hwnd, stringBuilder, stringBuilder.Capacity);
Console.WriteLine(stringBuilder.ToString());
if (!hwnd.Equals(IntPtr.Zero))
{
// SW_SHOWMAXIMIZED to maximize the window
// SW_SHOWMINIMIZED to minimize the window
// SW_SHOWNORMAL to make the window be normal size
ShowWindowAsync(hwnd, SW_SHOWMINIMIZED);
}
}
Where sensitiveApps is a list containing the strings "Notepad", "Recuva" and "VLC media player 2.0.3".
However, the only application that can be minimized using this code is Notepad. Debugging the program finds that
Console.WriteLine(stringBuilder.ToString());
would not return any value for the last 2 programs, but would return a Untitled - Notepad.
Is there anything I'm doing wrong?
Try using Spy++ and check the FindWindow names are correct.
MS Word is OpusApp and VLC is QWidget.

Webservice on IIS

I have a webservice and a webform. A button invokes the webservice which reads a given process name from pid. This works fine in VS2008 but when I publish my project I dont get the name? How can I configure IIS to allow me to do so? or is there an alternative way i.e. wcf or wwf?
Edit:
using System.Diagnostics;
[WebMethod]
public string GetProcessName()
{
Process Process = Process.GetProcessById(1428);
string ProcessTitle = Process.MainWindowTitle;
return ProcessTitle;
}
I used tasklist.exe /v to get the pid of a process which has a window title.
Given your code:
how do you know there's a process with PID = 1428 ??
what happens if that process doesn't exist?
If you check out this line of code:
Process process = Process.GetProcessById(1428);
will return NULL, and then you grab the .MainWindowTitle from...... NULL..... that's guaranteed to cause an exception.....
using System.Diagnostics;
[WebMethod]
public string GetProcessName()
{
string processTitle = string.Empty;
Process process = Process.GetProcessById(1428);
if(process != null)
{
processTitle = process.MainWindowTitle;
}
return processTitle;
}
And even so - again: how do you know a process with ID = 1428 exists??
I can't believe that when I deployed my project to Abyss web server it works like a charm.
Thanks all anyways.

Categories

Resources