❔ 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
ero17mo ago
that's crazy, you've been told to use SendInput instead of keydb_event and you still didn't change it
어부지망생
어부지망생17mo 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
ero17mo ago
what's your program's purpose?
어부지망생
어부지망생17mo ago
ah my program is to control my computer remotely
어부지망생
어부지망생17mo ago
lot of red lines
ero
ero17mo 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
Denis17mo 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#
어부지망생
어부지망생17mo ago
yhea so i tried inputs = new INPUT[4]; first, but it doesnt work
Denis
Denis17mo ago
In what way doesn't it work?
어부지망생
어부지망생17mo 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
ero17mo ago
ZeroMemory is probably invalid, SendInput is invalid (without a class that contains it), ARRAYSIZE is invalid
어부지망생
어부지망생17mo ago
ah..
ero
ero17mo ago
just think about it a bit before you use it
어부지망생
어부지망생17mo ago
okok
ero
ero17mo ago
it's PInvoke.SendInput(input.Length, input, Marshal.SizeOf<INPUT>());
어부지망생
어부지망생17mo ago
actually i didn't understand this message could you tell me more specific?
ero
ero17mo ago
can you say exactly what you don't understand?
어부지망생
어부지망생17mo ago
i didnt understand "without a class that contains it"
ero
ero17mo ago
SendInput doesn't just exist on its own it's in the class PInvoke
어부지망생
어부지망생17mo ago
ah thanks i understand what u means
어부지망생
어부지망생17mo ago
i thinked why it has redline but i cannot find solution..
Denis
Denis17mo ago
When you hover over the red line, what error message do you see?
어부지망생
어부지망생17mo ago
Compiler Error CS1503 it says cannot change int to uint
Denis
Denis17mo ago
what do you think that means? Not to be annoying, but you need to learn to read and fix error messages
어부지망생
어부지망생17mo ago
i have to change int to uint?
Denis
Denis17mo ago
exactly
어부지망생
어부지망생17mo ago
yhea exactly what i need but i didn't know how to change int to uint
Denis
Denis17mo ago
ah, to do that use casting
ero
ero17mo ago
nevermind, the first declaration works
어부지망생
어부지망생17mo ago
casting? ok i'll try it and come back
Denis
Denis17mo ago
you can cast a type using the following syntax: (uint)myIntValue
ero
ero17mo ago
this should actually just work. cswin32 generates an overload that takes a Span, and calls the overload with 3 parameters
Denis
Denis17mo ago
I think you might have to elaborate on that...
ero
ero17mo ago
what's there to elaborate on
Denis
Denis17mo 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
ero17mo ago
all that matters is that it should work the rest is irrelevant
어부지망생
어부지망생17mo ago
what is span?
Denis
Denis17mo ago
Given your previous attempts at creating an array, do you happen to have experience with C++?
ero
ero17mo ago
어부지망생
어부지망생17mo ago
okok calm down sry 😦 i have experienced with c# java python not with c++
Denis
Denis17mo 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
ero17mo 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
Denis17mo 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
ero17mo ago
it likely doesn't work because of the project type (windows service)
어부지망생
어부지망생17mo ago
ok 😄 so is there any way to solve this problem?
ero
ero17mo ago
no clue
어부지망생
어부지망생17mo ago
And just wondering are u working on it industry? Like softwafe developer? Ah... what should i do...
Denis
Denis17mo 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?
어부지망생
어부지망생17mo ago
Nono i want to control media like play pause music
Denis
Denis17mo ago
And by Service you literally mean a Windows service running in the background, or a Console/WinForms/WPF... application?
어부지망생
어부지망생17mo ago
I meam service running in background
Accord
Accord17mo 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
More Posts