Skip to content

Commit

Permalink
Fixed #26 - SendMessage ignoring classname
Browse files Browse the repository at this point in the history
  • Loading branch information
tig committed Feb 8, 2022
1 parent a83c752 commit 4bdfbb6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Installer/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.9.12
2.2.9.22
4 changes: 4 additions & 0 deletions src/Commands/CommandInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ public static CommandInvoker Create(string userCommandsFile, string currentVersi

if (cmd.Enabled) {
if (cmd.UserDefined) {
#if DEBUG
Logger.Instance.Log4.Debug($"{commands.GetType().Name}: User defined command enabled: '{cmd}'");
#else
Logger.Instance.Log4.Debug($"{commands.GetType().Name}: User defined command enabled: '****'");
#endif
}
else {
Logger.Instance.Log4.Debug($"{commands.GetType().Name}: Builtin command enabled: '{cmd}'");
Expand Down
36 changes: 24 additions & 12 deletions src/Commands/SendMessageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Linq;
using Microsoft.Win32.Security;

namespace MCEControl {
Expand All @@ -31,10 +32,11 @@ public class SendMessageCommand : Command {
private int wParam;
[XmlAttribute("wparam")] public int WParam { get => wParam; set => wParam = value; }

[XmlAttribute("className")]
public String ClassName { get; set; }
[XmlAttribute("windowname")]
public String WindowName { get; set; }
private String className;
[XmlAttribute("classname")] public String ClassName { get => className; set => className = value; }

private String windowName;
[XmlAttribute("windowname")] public String WindowName { get => windowName; set => windowName = value; }

public static new List<Command> BuiltInCommands {
get => new List<Command>() {
Expand Down Expand Up @@ -69,26 +71,36 @@ public override bool Execute() {
}

try {
if (ClassName != null) {
if (!string.IsNullOrWhiteSpace(ClassName)) {
var procs = Process.GetProcessesByName(ClassName);
if (procs.Length > 0) {
var h = procs[0].MainWindowHandle;
var win = procs[0];

Logger.Instance.Log4.Info($"{this.GetType().Name}: SendMessage {ToString()}");
Win32.SendMessage(h, (DWORD)Msg, (DWORD)WParam, (DWORD)LParam);
if (!string.IsNullOrWhiteSpace(WindowName)) {
// Find MainWindowTitle matching WindowName
win = procs.FirstOrDefault(w => w.MainWindowTitle.Equals(WindowName));
}
if (win == null) {
Logger.Instance.Log4.Error($"{this.GetType().Name}: Could not find a window of class '{ClassName}' captioned with '{WindowName}'");
}
else {
Logger.Instance.Log4.Info($"{this.GetType().Name}: SendMessage(\"{win.MainWindowTitle}\", {Msg}, {WParam}, {LParam}) - {ToString()}");
Win32.SendMessage(win.MainWindowHandle, (DWORD)Msg, (DWORD)WParam, (DWORD)LParam);
}
}
else {
Logger.Instance.Log4.Info($"{this.GetType().Name}: GetProcessByName for {ClassName} failed");
Logger.Instance.Log4.Error($"{this.GetType().Name}: GetProcessByName for class '{ClassName}' failed");
}
}
else {
else
{
var h = Win32.GetForegroundWindow();
Logger.Instance.Log4.Info($"{this.GetType().Name}: SendMessage (forground window): {ToString()}");
Logger.Instance.Log4.Info($"{this.GetType().Name}: SendMessage(<forground window>, {Msg}, {WParam}, {LParam}) - {ToString()}");
Win32.SendMessage(h, (DWORD)Msg, (DWORD)WParam, (DWORD)LParam);
}
}
catch (Exception e) {
Logger.Instance.Log4.Error($"{this.GetType().Name}: Failed for {ClassName} with error: {e.Message}");
Logger.Instance.Log4.Error($"{this.GetType().Name}: Failed for '{ClassName}' with error: {e.Message}");
return true;
}
return false;
Expand Down
1 change: 1 addition & 0 deletions src/MCEControl.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\Installer\Installer.tt = ..\Installer\Installer.tt
Installer\license.txt = Installer\license.txt
..\Installer\logging.nsh = ..\Installer\logging.nsh
Win32\Readme.md = Win32\Readme.md
README.md = README.md
..\Installer\version.txt = ..\Installer\version.txt
EndProjectSection
Expand Down
12 changes: 12 additions & 0 deletions src/Win32/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
**Win32Security**

This is a fork of a the Win32Security library from https://github.com/woanware/Win32Security. Originally written by
Renaud Paquay (is/was an Microsoft employee). woanwar modified it to include some service related access masks. https:://github.com/tig further modified it to work with modern .NET tools and versions of Windows, for use in https://github.com/tig/mcec.

**Description**

A C# library containing wrapper classes for ACL, ACE, Security descriptors, Security Attributes, Access tokens, etc.

The original library was located here (no longer valid):

http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=e6098575-dda0-48b8-9abf-e0705af065d9

0 comments on commit 4bdfbb6

Please sign in to comment.