C
Join ServerC#
help
❔ Allow service to interact with desktop
어어부지망생3/1/2023
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
EEro3/1/2023
that's crazy, you've been told to use SendInput instead of keydb_event and you still didn't change it
어어부지망생3/1/2023
What does it mean?
어어부지망생3/1/2023
Ah i tried them
어어부지망생3/1/2023
But it doesnt work
어어부지망생3/1/2023
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>());
어어부지망생3/1/2023
i installed cswin32 package
어어부지망생3/1/2023
and i used send input
어어부지망생3/1/2023
so code doesnt have red lines but it doesnt work
EEro3/1/2023
what's your program's purpose?
어어부지망생3/1/2023
ah my program is to control my computer remotely
EEro3/1/2023
that's not how you call SendInput anyway
어어부지망생3/1/2023
lot of red lines

EEro3/1/2023
that's what happens when you don't think for yourself
EEro3/1/2023
and just copy and paste
EEro3/1/2023
that array declaration is obviously not valid c# syntax
DDeno3/1/2023
that is because of
INPUT inputs[4] = {}
, that is syntactically incorrect c#DDeno3/1/2023
INPUT[] inputs = new INPUT[4];
DDeno3/1/2023
That is the correct way to declare an empty array in C#
어어부지망생3/1/2023
yhea so i tried inputs = new INPUT[4]; first, but it doesnt work
DDeno3/1/2023
In what way doesn't it work?
어어부지망생3/1/2023
yep
어어부지망생3/1/2023
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>());
어어부지망생3/1/2023
this was i tried first
EEro3/1/2023
ZeroMemory is probably invalid, SendInput is invalid (without a class that contains it), ARRAYSIZE is invalid
어어부지망생3/1/2023
ah..
EEro3/1/2023
just think about it a bit before you use it
어어부지망생3/1/2023
okok
EEro3/1/2023
it's
PInvoke.SendInput(input.Length, input, Marshal.SizeOf<INPUT>());
어어부지망생3/1/2023
actually i didn't understand this message could you tell me more specific?
EEro3/1/2023
can you say exactly what you don't understand?
어어부지망생3/1/2023
i didnt understand "without a class that contains it"
EEro3/1/2023
SendInput
doesn't just exist on its ownEEro3/1/2023
it's in the class
PInvoke
어어부지망생3/1/2023
ah
어어부지망생3/1/2023
thanks i understand what u means
어어부지망생3/1/2023
i thinked why it has redline but i cannot find solution..

DDeno3/1/2023
When you hover over the red line, what error message do you see?
어어부지망생3/1/2023
Compiler Error CS1503
어어부지망생3/1/2023
it says cannot change int to uint
DDeno3/1/2023
what do you think that means?
DDeno3/1/2023
Not to be annoying, but you need to learn to read and fix error messages
어어부지망생3/1/2023
i have to change int to uint?
DDeno3/1/2023
exactly
어어부지망생3/1/2023
yhea exactly what i need
어어부지망생3/1/2023
but i didn't know how to change int to uint
DDeno3/1/2023
ah, to do that use casting
EEro3/1/2023
nevermind, the first declaration works
어어부지망생3/1/2023
casting?
어어부지망생3/1/2023
ok i'll try it and come back
DDeno3/1/2023
you can cast a type using the following syntax:
(uint)myIntValue
EEro3/1/2023
this should actually just work. cswin32 generates an overload that takes a
Span
, and calls the overload with 3 parametersDDeno3/1/2023
I think you might have to elaborate on that...
EEro3/1/2023
what's there to elaborate on
DDeno3/1/2023
I'm not sure whether @어부지망생 will know what is and how to use a
Span
DDeno3/1/2023
given that casting is a new concept for them
EEro3/1/2023
all that matters is that it should work
EEro3/1/2023
the rest is irrelevant
어어부지망생3/1/2023
what is span?
DDeno3/1/2023
Given your previous attempts at creating an array, do you happen to have experience with C++?
EEro3/1/2023
they didn't "attempt" it
EEro3/1/2023
they copied it from the doc page
어어부지망생3/1/2023
okok calm down sry 😦
어어부지망생3/1/2023
i have experienced with c# java python not with c++
DDeno3/1/2023
Ok. Asking to get a perspective on how to explain span to you
DDeno3/1/2023
Not really sure how to do that in simple terms 😄
EEro3/1/2023
it's an array
EEro3/1/2023
that's really all you need to know
EEro3/1/2023
at your level anyway
EEro3/1/2023
anything else will overwhelm you
EEro3/1/2023
Span and array are implicitly convertible even
EEro3/1/2023
so the call they do here is completely valid
DDeno3/1/2023
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
EEro3/1/2023
it likely doesn't work because of the project type (windows service)
어어부지망생3/1/2023
ok 😄
어어부지망생3/1/2023
so is there any way to solve this problem?
EEro3/1/2023
no clue
어어부지망생3/1/2023
And just wondering are u working on it industry?
어어부지망생3/1/2023
Like softwafe developer?
어어부지망생3/1/2023
Ah... what should i do...
DDeno3/1/2023
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?
- you have a windows service project
- you want it to access th current Windows desktop and
- type something into, e.g., notepad?
어어부지망생3/1/2023
Nono i want to control media like play pause music
DDeno3/1/2023
And by Service you literally mean a Windows service running in the background, or a Console/WinForms/WPF... application?
어어부지망생3/1/2023
I meam service running in background
AAccord3/2/2023
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.