Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/sys/windows: error among CGO OR golang.org/x/sys/windows OR syscall #54188

Closed
IsaacDiane opened this issue Aug 2, 2022 · 3 comments
Closed
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@IsaacDiane
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.18.2 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH=amd64
GOOS=windows

go env Output
$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Admin\AppData\Local\go-build
set GOENV=C:\Users\Admin\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Go\pkg\mod
set GOOS=windows
set GOPATH=C:\Go
set GOROOT=C:\Program Files\Golang
set GOSUMDB=sum.golang.google.cn
set GOTMPDIR=
set GOMOD=C:\Go\src\robotgodemo\go.mod
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Admin\AppData\Local\Temp\go-build2101037137=/tmp/go-build -gno-record-gcc-switches

What did you do?

my code using syscall OR sys/windows :

var (
user32, _ = windows.LoadDLL("user32.dll")
sendInputProc = user32.MustFindProc("SendInput")
getInfoProc = user32.MustFindProc("GetMessageExtraInfo")
)

func scrollTest(scroll int) {
type mouseInput struct {
DwFlags uint32
Time uint32
MouseData uint32
Dx int32
Dy int32
DwExtraInfo uintptr
}
type Input struct {
InputType uint32
Mi mouseInput
}
x, y := robotgo.GetMousePos()

ret, _, _ := getInfoProc.Call()
fmt.Println(ret)
mi := mouseInput{
	Dx:          int32(x),
	Dy:          int32(y),
	MouseData:   uint32(scroll),
	DwFlags:     w32.MOUSEEVENTF_WHEEL,
	Time:        0,
	DwExtraInfo: ret,
}
input := Input{
	InputType: w32.INPUT_MOUSE,
	Mi:        mi,
}
ret, _, err := sendInputProc.Call(uintptr(1),
	uintptr(unsafe.Pointer(&input)),
	unsafe.Sizeof(input))
fmt.Println(ret)
fmt.Println(err)

} // a code to simulate mouse wheel rotation.

I also used w32 wrapper by AllenDang for some constant, and I have make sure that these constant are the same value as the ones in win32api. This code is able to run, and able to scroll my mouse. However, we should focus on the type of MouseData, it is uint type, means the value should not be negative. However, in win32api, it is a DWORD type value ,which means it is a int32 type in golang. The positive or negative of the value means the mouse wheel rotate towards user or forwards user. When I try to change the type into int32, the code seems not correct. I can compile and run it, but this code will leads to an unexpected screen lock. When I run the code, I observed the screen scrolls as expected, and then my screen shutdown and my computer seems to sleep. When I awake it, I found the code run successfully without error. I think this case may be similar to #31685, is the SendInput dll function cause the accident?

Then here is my CGO code, I think there may be something wrong with syscall or windows, so I turned to CGO.

UINT ScrollMouse(int scroll)
{
INPUT input;
POINT pos;
GetCursorPos(&pos);

input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_WHEEL;
//input.mi.time = NULL; //Windows will do the timestamp
input.mi.mouseData = (DWORD)scroll; //A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user. One wheel click is defined as WHEEL_DELTA, which is 120.
input.mi.dx = pos.x;
input.mi.dy = pos.y;
input.mi.dwExtraInfo = GetMessageExtraInfo();

return SendInput(1, &input, sizeof(INPUT));

}
*/
import "C"

Here is my code, which also leads to the same result. The code run successfully and scroll successfully but my computer sleep again, without any error.

Finally, I have to use the os/exec to run an exe file directly. And the code is just same as the one in CGO, I succeed this time. Why does the syscall leads to my computer sleeping? I confued if it is a bug or just my ignorance to something when coding. Sorry for that I have only one windows PC so I cannot try if it is the problem of my computer.

What did you expect to see?

The screen scrolled by the rotation input of mouse wheel which send by the go program.

What did you see instead?

The mouse wheel rotated, however, my computer sleep unexpected.

@IsaacDiane IsaacDiane changed the title affected/package: CGO golang.org/x/sys/windows syscall golang.org/x/sys/windows: error among CGO OR golang.org/x/sys/windows OR syscall Aug 2, 2022
@dmitshur dmitshur changed the title golang.org/x/sys/windows: error among CGO OR golang.org/x/sys/windows OR syscall x/sys/windows: error among CGO OR golang.org/x/sys/windows OR syscall Aug 2, 2022
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Aug 2, 2022
@gopherbot gopherbot added this to the Unreleased milestone Aug 2, 2022
@mknyszek
Copy link
Contributor

mknyszek commented Aug 3, 2022

Can you make this work in a pure C program? If so, it might not be a Go issue specifically. If it's not a Go issue, you may want to ask on a mailing list (like golang-nuts).

The reason the computer might be going to sleep is some PCs are configured with "hot corners" where moving the mouse position to a corner causes it to go to sleep. Maybe that's what's happening?

@mknyszek mknyszek added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Aug 3, 2022
@mknyszek mknyszek self-assigned this Aug 3, 2022
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@ZekeLu
Copy link
Contributor

ZekeLu commented Sep 4, 2022

The structure layout matters. According to https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-mouseinput, the mouseInput struct should be:

	type mouseInput struct {
		Dx          int32
		Dy          int32
		MouseData   uint32
		DwFlags     uint32
		Time        uint32
		DwExtraInfo uintptr
	}

Comparing to the one provided by the author:

	type mouseInput struct {
		DwFlags     uint32
		Time        uint32
		MouseData   uint32
		Dx          int32
		Dy          int32
		DwExtraInfo uintptr
	}

I would suggest the author to correct the layout first.

@golang golang locked and limited conversation to collaborators Sep 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants