❔ Allow service to interact with desktop
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
that's crazy, you've been told to use SendInput instead of keydb_event and you still didn't change it
What does it mean?
Ah i tried them
But it doesnt work
i installed cswin32 package
and i used send input
so code doesnt have red lines but it doesnt work
what's your program's purpose?
ah my program is to control my computer remotely
that's not how you call SendInput anyway
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput
lot of red lines
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
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#yhea so i tried inputs = new INPUT[4]; first, but it doesnt work
In what way doesn't it work?
yep
this was i tried first
ZeroMemory is probably invalid, SendInput is invalid (without a class that contains it), ARRAYSIZE is invalid
ah..
just think about it a bit before you use it
okok
it's
PInvoke.SendInput(input.Length, input, Marshal.SizeOf<INPUT>());
actually i didn't understand this message could you tell me more specific?
can you say exactly what you don't understand?
i didnt understand "without a class that contains it"
SendInput
doesn't just exist on its own
it's in the class PInvoke
ah
thanks i understand what u means
i thinked why it has redline but i cannot find solution..
When you hover over the red line, what error message do you see?
Compiler Error CS1503
it says cannot change int to uint
what do you think that means?
Not to be annoying, but you need to learn to read and fix error messages
i have to change int to uint?
exactly
yhea exactly what i need
but i didn't know how to change int to uint
ah, to do that use casting
nevermind, the first declaration works
casting?
ok i'll try it and come back
you can cast a type using the following syntax:
(uint)myIntValue
this should actually just work. cswin32 generates an overload that takes a
Span
, and calls the overload with 3 parametersI think you might have to elaborate on that...
what's there to elaborate on
I'm not sure whether @어부지망생 will know what is and how to use a
Span
given that casting is a new concept for themall that matters is that it should work
the rest is irrelevant
what is span?
Given your previous attempts at creating an array, do you happen to have experience with C++?
they didn't "attempt" it
they copied it from the doc page
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput#example
okok calm down sry 😦
i have experienced with c# java python not with c++
Ok. Asking to get a perspective on how to explain span to you
Not really sure how to do that in simple terms 😄
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
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
it likely doesn't work because of the project type (windows service)
ok 😄
so is there any way to solve this problem?
no clue
And just wondering are u working on it industry?
Like softwafe developer?
Ah... what should i do...
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?
Nono i want to control media like play pause music
And by Service you literally mean a Windows service running in the background, or a Console/WinForms/WPF... application?
I meam service running in background
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.