Why does the code line under ❶ Nothing found fire when I call the below C# .dll like so:
WindowNameGetter::GetWindowName("MyAppName.exe");
(...and "MyAppName.exe" really is up and running!)
C# .dll code:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class WindowNameGetter
{
// Import the FindWindow function from the user32.dll library
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
// Import the GetWindowText function from the user32.dll library
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, out string lpString, int nMaxCount);
// Define a public static method named GetWindowName that takes an application name as a parameter and returns a string
public static string GetWindowName(string applicationName)
{
// Initialize an empty string variable to store the window name
string windowName = "";
// Get an array of processes with the specified application name
Process[] processes = Process.GetProcessesByName(applicationName);
// Check if there is at least one process with the specified application name
if (processes.Length > 0)
{
// Get the window handle of the first process in the array
IntPtr hWnd = processes[0].MainWindowHandle;
// Call the GetWindowText function to retrieve the window name associated with the window handle
GetWindowText(hWnd, out windowName, 256);
// Return the retrieved window name
return windowName;
}
else
{
// ❶ Nothing found:
return "Error: no process called " + applicationName + " found!";
}
}
}