1
votes

I am integrating with some thirdparty software that runs with an .hta file inside of mshta.exe. My app is written in C#

I need to detect if the .hta file is already running so I can launch it before I start sending messages to it.

With past integrations with other third parties, I have checked for the exe in the process list, but I don't think that I can just look for mshta.exe because they could be running some other .hta file.

I also tried grabbing the process.MainWindowTitle from Process.GetProcesses(), but even though the mshta.exe window is showing a title, the MainWindowTitle property is blank.

Does anyone know a method where I could figure out that mshta.exe is running a specific hta file?

1
Check the command line of each mshta.exe, it should contain the path of the running hta.Teemu
Can you post the source code of your HTA ?Hackoo
Unfortunately, its a 3rd party HTA file so I can't share itspectacularbob

1 Answers

1
votes

Thanks to Teemu's comment, I found that the .hta path is indeed in the command line arguments. However, this information is not in the Process reference (even though there is an Arguments property on the process's StartInfo). You have to use the ManagementObjectSearcher like so:

//Call this extension on the mshta.exe process to get the path of the .hta file
public static string GetCommandLine(this Process process) {
    var commandLine = new StringBuilder(process.MainModule.FileName);

    commandLine.Append(" ");
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id)) {
        foreach (var commandLinePart in searcher.Get()) {
            commandLine.Append(commandLinePart["CommandLine"]);
            commandLine.Append(" ");
        }
    }

    return commandLine.ToString();
}