❔ Allow service to interact with desktop

public const int KEYEVENTF_EXTENTEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_PREV_TRACK = 0xB1;

[DllImport("user32.dll")]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero); // PREV
public const int KEYEVENTF_EXTENTEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_PREV_TRACK = 0xB1;

[DllImport("user32.dll")]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero); // PREV
this code works properly in windows form but it doesn't in windows service so i found i need to allow service to interact with desktop so i tried but it doesn't work neither
53 Replies
ero
ero2y ago
that's crazy, you've been told to use SendInput instead of keydb_event and you still didn't change it
어부지망생
어부지망생OP2y ago
What does it mean? Ah i tried them But it doesnt work
var input = new INPUT[2];

input[0].type = INPUT_TYPE.INPUT_KEYBOARD;
input[0].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_NEXT_TRACK;

input[1].type = INPUT_TYPE.INPUT_KEYBOARD;
input[1].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_NEXT_TRACK;
input[1].Anonymous.ki.dwFlags = KEYBD_EVENT_FLAGS.KEYEVENTF_KEYUP;

PInvoke.SendInput(input, Marshal.SizeOf<INPUT>());
var input = new INPUT[2];

input[0].type = INPUT_TYPE.INPUT_KEYBOARD;
input[0].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_NEXT_TRACK;

input[1].type = INPUT_TYPE.INPUT_KEYBOARD;
input[1].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_NEXT_TRACK;
input[1].Anonymous.ki.dwFlags = KEYBD_EVENT_FLAGS.KEYEVENTF_KEYUP;

PInvoke.SendInput(input, Marshal.SizeOf<INPUT>());
i installed cswin32 package and i used send input so code doesnt have red lines but it doesnt work
ero
ero2y ago
what's your program's purpose?
어부지망생
어부지망생OP2y ago
ah my program is to control my computer remotely
어부지망생
어부지망생OP2y ago
lot of red lines
ero
ero2y ago
that's what happens when you don't think for yourself and just copy and paste that array declaration is obviously not valid c# syntax
Denis
Denis2y ago
that is because of INPUT inputs[4] = {}, that is syntactically incorrect c# INPUT[] inputs = new INPUT[4]; That is the correct way to declare an empty array in C#
어부지망생
어부지망생OP2y ago
yhea so i tried inputs = new INPUT[4]; first, but it doesnt work
Denis
Denis2y ago
In what way doesn't it work?
어부지망생
어부지망생OP2y ago
yep
var input = new INPUT[2];

input[0].type = INPUT_TYPE.INPUT_KEYBOARD;
input[0].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_NEXT_TRACK;

input[1].type = INPUT_TYPE.INPUT_KEYBOARD;
input[1].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_NEXT_TRACK;
input[1].Anonymous.ki.dwFlags = KEYBD_EVENT_FLAGS.KEYEVENTF_KEYUP;

PInvoke.SendInput(input, Marshal.SizeOf<INPUT>());
var input = new INPUT[2];

input[0].type = INPUT_TYPE.INPUT_KEYBOARD;
input[0].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_NEXT_TRACK;

input[1].type = INPUT_TYPE.INPUT_KEYBOARD;
input[1].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_NEXT_TRACK;
input[1].Anonymous.ki.dwFlags = KEYBD_EVENT_FLAGS.KEYEVENTF_KEYUP;

PInvoke.SendInput(input, Marshal.SizeOf<INPUT>());
this was i tried first
ero
ero2y ago
ZeroMemory is probably invalid, SendInput is invalid (without a class that contains it), ARRAYSIZE is invalid
어부지망생
어부지망생OP2y ago
ah..
ero
ero2y ago
just think about it a bit before you use it
어부지망생
어부지망생OP2y ago
okok
ero
ero2y ago
it's PInvoke.SendInput(input.Length, input, Marshal.SizeOf<INPUT>());
어부지망생
어부지망생OP2y ago
actually i didn't understand this message could you tell me more specific?
ero
ero2y ago
can you say exactly what you don't understand?
어부지망생
어부지망생OP2y ago
i didnt understand "without a class that contains it"
ero
ero2y ago
SendInput doesn't just exist on its own it's in the class PInvoke
어부지망생
어부지망생OP2y ago
ah thanks i understand what u means
어부지망생
어부지망생OP2y ago
i thinked why it has redline but i cannot find solution..
Denis
Denis2y ago
When you hover over the red line, what error message do you see?
어부지망생
어부지망생OP2y ago
Compiler Error CS1503 it says cannot change int to uint
Denis
Denis2y ago
what do you think that means? Not to be annoying, but you need to learn to read and fix error messages
어부지망생
어부지망생OP2y ago
i have to change int to uint?
Denis
Denis2y ago
exactly
어부지망생
어부지망생OP2y ago
yhea exactly what i need but i didn't know how to change int to uint
Denis
Denis2y ago
ah, to do that use casting
ero
ero2y ago
nevermind, the first declaration works
어부지망생
어부지망생OP2y ago
casting? ok i'll try it and come back
Denis
Denis2y ago
you can cast a type using the following syntax: (uint)myIntValue
ero
ero2y ago
this should actually just work. cswin32 generates an overload that takes a Span, and calls the overload with 3 parameters
Denis
Denis2y ago
I think you might have to elaborate on that...
ero
ero2y ago
what's there to elaborate on
Denis
Denis2y ago
I'm not sure whether @어부지망생 will know what is and how to use a Span given that casting is a new concept for them
ero
ero2y ago
all that matters is that it should work the rest is irrelevant
어부지망생
어부지망생OP2y ago
what is span?
Denis
Denis2y ago
Given your previous attempts at creating an array, do you happen to have experience with C++?
ero
ero2y ago
어부지망생
어부지망생OP2y ago
okok calm down sry 😦 i have experienced with c# java python not with c++
Denis
Denis2y ago
Ok. Asking to get a perspective on how to explain span to you Not really sure how to do that in simple terms 😄
ero
ero2y ago
it's an array that's really all you need to know at your level anyway anything else will overwhelm you Span and array are implicitly convertible even so the call they do here is completely valid
Denis
Denis2y ago
But don't hold on to that, as a span is much more than that. Just like @Ero says, view it as a collection of items, an array for now
ero
ero2y ago
it likely doesn't work because of the project type (windows service)
어부지망생
어부지망생OP2y ago
ok 😄 so is there any way to solve this problem?
ero
ero2y ago
no clue
어부지망생
어부지망생OP2y ago
And just wondering are u working on it industry? Like softwafe developer? Ah... what should i do...
Denis
Denis2y ago
So to clarify: - you have a windows service project - you want it to access th current Windows desktop and - type something into, e.g., notepad?
어부지망생
어부지망생OP2y ago
Nono i want to control media like play pause music
Denis
Denis2y ago
And by Service you literally mean a Windows service running in the background, or a Console/WinForms/WPF... application?
어부지망생
어부지망생OP2y ago
I meam service running in background
Accord
Accord2y ago
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