Tweaked inter-process message posting (added safety checks to ignore too large data packets). Added LogDebug message when a profile is loaded via cmdline commands.

This commit is contained in:
mika-n 2019-07-01 13:34:36 +03:00
parent 3817b2e026
commit 7061a5c6c4
2 changed files with 28 additions and 22 deletions

View File

@ -68,6 +68,8 @@ namespace DS4Windows.Forms
private const string UPDATER_VERSION = "1.3.1"; private const string UPDATER_VERSION = "1.3.1";
private const int WM_QUERYENDSESSION = 0x11; private const int WM_QUERYENDSESSION = 0x11;
private const int WM_CLOSE = 0x10; private const int WM_CLOSE = 0x10;
public const int WM_COPYDATA = 0x004A;
internal string updaterExe = Environment.Is64BitProcess ? "DS4Updater.exe" : "DS4Updater_x86.exe"; internal string updaterExe = Environment.Is64BitProcess ? "DS4Updater.exe" : "DS4Updater_x86.exe";
[DllImport("user32.dll")] [DllImport("user32.dll")]
@ -86,7 +88,7 @@ namespace DS4Windows.Forms
private static extern uint GetModuleFileNameEx(IntPtr hWnd, IntPtr hModule, StringBuilder lpFileName, int nSize); private static extern uint GetModuleFileNameEx(IntPtr hWnd, IntPtr hModule, StringBuilder lpFileName, int nSize);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
public DS4Form(string[] args) public DS4Form(string[] args)
{ {
@ -1151,11 +1153,11 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question
systemShutdown = true; systemShutdown = true;
break; break;
} }
case Program.WM_COPYDATA: case WM_COPYDATA:
{ {
// Received InterProcessCommunication (IPC) message. Handle the requested command // Received InterProcessCommunication (IPC) message. DS4Win command is embedded as a string value in lpData buffer
Program.COPYDATASTRUCT cds = (Program.COPYDATASTRUCT)m.GetLParam(typeof(Program.COPYDATASTRUCT)); Program.COPYDATASTRUCT cds = (Program.COPYDATASTRUCT)m.GetLParam(typeof(Program.COPYDATASTRUCT));
if (cds.cbData >= 4) if (cds.cbData >= 4 && cds.cbData <= 256)
{ {
int tdevice = -1; int tdevice = -1;
@ -1173,24 +1175,25 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question
ServiceShutdown(true); ServiceShutdown(true);
else if (strData[0] == "shutdown") else if (strData[0] == "shutdown")
ScpForm_Closing(this, new FormClosingEventArgs(CloseReason.ApplicationExitCall, false)); ScpForm_Closing(this, new FormClosingEventArgs(CloseReason.ApplicationExitCall, false));
else if (strData[0] == "loadprofile" && strData.Length >= 3) else if ( (strData[0] == "loadprofile" || strData[0] == "loadtempprofile") && strData.Length >= 3)
{ {
// Command syntax: LoadProfile.device#.profileName (fex LoadProfile.1.GameSnake) // Command syntax: LoadProfile.device#.profileName (fex LoadProfile.1.GameSnake or LoadTempProfile.1.WebBrowserSet)
if(int.TryParse(strData[1], out tdevice)) tdevice--; if(int.TryParse(strData[1], out tdevice)) tdevice--;
if (tdevice >= 0 && tdevice < 4) if (tdevice >= 0 && tdevice < ControlService.DS4_CONTROLLER_COUNT && File.Exists(Global.appdatapath + "\\Profiles\\" + strData[2] + ".xml"))
{ {
ProfilePath[tdevice] = strData[2]; if (strData[0] == "loadprofile")
LoadProfile(tdevice, true, Program.rootHub); {
} ProfilePath[tdevice] = strData[2];
} LoadProfile(tdevice, true, Program.rootHub);
else if (strData[0] == "loadtempprofile" && strData.Length >= 3) }
{ else
// Command syntax: LoadTempProfile.device#.profileName (fex LoadTempProfile.1.GameSnake) {
if (int.TryParse(strData[1], out tdevice)) tdevice--; LoadTempProfile(tdevice, strData[2], true, Program.rootHub);
}
if (tdevice >= 0 && tdevice < 4) Program.rootHub.LogDebug(Properties.Resources.UsingProfile.Replace("*number*", (tdevice + 1).ToString()).Replace("*Profile name*", strData[2]));
LoadTempProfile(tdevice, strData[2], true, Program.rootHub); }
} }
} }
} }

View File

@ -15,9 +15,7 @@ namespace DS4Windows
private static extern IntPtr FindWindow(string sClass, string sWindow); private static extern IntPtr FindWindow(string sClass, string sWindow);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, ref COPYDATASTRUCT lParam); private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
public const int WM_COPYDATA = 0x004A;
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT public struct COPYDATASTRUCT
@ -89,8 +87,13 @@ namespace DS4Windows
IntPtr hWndDS4WindowsForm = IntPtr.Zero; IntPtr hWndDS4WindowsForm = IntPtr.Zero;
Global.FindConfigLocation(); Global.FindConfigLocation();
if (args[i].Length > 0 && !string.IsNullOrEmpty(Global.appdatapath) && System.IO.File.Exists(Global.appdatapath + "\\IPCClassName.dat")) if (args[i].Length > 0 && args[i].Length <= 256 && !string.IsNullOrEmpty(Global.appdatapath) && System.IO.File.Exists(Global.appdatapath + "\\IPCClassName.dat"))
{ {
// Find the main DS4Form window handle and post WM_COPYDATA inter-process message. IPCClasName.dat file was created by the main DS4Windows process
// and it contains the name of the DS4Form .NET window class name string (the hash key part of the string is dynamically generated by .NET engine and it may change,
// so that's why the main process re-creates the file if the window class name is changed by .NET framework). Finding the WND handle usig both class name and window name
// limits chances that WM_COPYDATA message is sent to a wrong window.
hWndDS4WindowsForm = FindWindow(System.IO.File.ReadAllText(Global.appdatapath + "\\IPCClassName.dat"), "DS4Windows"); hWndDS4WindowsForm = FindWindow(System.IO.File.ReadAllText(Global.appdatapath + "\\IPCClassName.dat"), "DS4Windows");
if (hWndDS4WindowsForm != IntPtr.Zero) if (hWndDS4WindowsForm != IntPtr.Zero)
{ {
@ -102,7 +105,7 @@ namespace DS4Windows
cds.dwData = IntPtr.Zero; cds.dwData = IntPtr.Zero;
cds.cbData = args[i].Length; cds.cbData = args[i].Length;
cds.lpData = Marshal.StringToHGlobalAnsi(args[i]); cds.lpData = Marshal.StringToHGlobalAnsi(args[i]);
SendMessage(hWndDS4WindowsForm, WM_COPYDATA, IntPtr.Zero, ref cds); SendMessage(hWndDS4WindowsForm, Forms.DS4Form.WM_COPYDATA, IntPtr.Zero, ref cds);
} }
finally finally
{ {