C
C#2y ago
Astrologic

❔ ✅ Help getting remote launch to perform task (display off)

I've got an application that just turns off a monitor, nothing super complicated about it:
internal class Program
{
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);



private static int WM_SYSCOMMAND = 0x0112;
private static uint SC_MONITORPOWER = 0xF170;

public static void Main(string[] args)
{
SendMessage(GetConsoleWindow(), WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)(2));
}
}
internal class Program
{
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);



private static int WM_SYSCOMMAND = 0x0112;
private static uint SC_MONITORPOWER = 0xF170;

public static void Main(string[] args)
{
SendMessage(GetConsoleWindow(), WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)(2));
}
}
The problem is that this application exists on a laptop which is all but dead. The keyboard barely works and if I press too hard the wifi will stop working until restart, the trackpad doesn't work, the usbs don't work, the power button barely works. I can remote into the system using SSH, but when I run the application using either start process.exe, psexec -i -d -u user -p "pass" "process.exe", or runas /user:user process.exe, it simply doesn't work. It'll tell me things like that the process has been created, but it won't turn the monitor off as it's supposed to. If I slam on the keyboard enough to run the app manually, it'll turn off the display, but that causes the wifi to stop working, so I'd really like to know how I can make the application run even when remotely executed using SSH.
11 Replies
LPeter1997
LPeter19972y ago
Did you check return codes/global error codes set by WinAPI?
Astrologic
AstrologicOP2y ago
I didn't.
LPeter1997
LPeter19972y ago
Let that be the first thing you do. SendMessage returns LRESULT, you have GetLastError
Astrologic
AstrologicOP2y ago
Okay, I'll have to save LRESULT to a txt file or something, then read it.
LPeter1997
LPeter19972y ago
I'd also read this carefully: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessage#remarks Especially
When a message is blocked by UIPI the last error, retrieved with GetLastError, is set to 5 (access denied).
Astrologic
AstrologicOP2y ago
Alright, I've modified the code. I'll have to scp it onto the remote system, then try the 3 different run methods. After that, I'll update you with the error codes. Under all run conditions, the errorcode returned is 0. Seems like it's running as expected. I have a feeling the issue is with the "GetConsoleWindow()". I'm thinking ssh is not allowing it to get the console window's handle, so I'm going to try a different approach.
LPeter1997
LPeter19972y ago
Well, check the error code for that
Astrologic
AstrologicOP2y ago
I kind of cheated. I wrote a powershell script to create a scheduled task which starts the C# app, then I just ran the scheduled task from ssh. It works. I guess it doesn't really matter how I get to point B, as long as I get there. I'm not a huge fan of requiring my apps to be run from task scheduler any time I need to run something using ssh, so I figure I'll probably create a C# application which can run any other application, and then put that in the taskscheduler, then I'll create another C# application that can run that task with the parameters for easier execution. It's a bit complicated, but I think that's going to be the best way to go about things. Thank you by the way, for attempting to help diagnose the issue. Am I supposed to close this channel or something?
LPeter1997
LPeter19972y ago
That is a very good question! Maybe try !close
Astrologic
AstrologicOP2y ago
!close
Accord
Accord2y ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server