如何在go中调用user32.dll的EnumDisplaySettingsA

问题描述

我正在尝试通过Win32 API获取显示信息。到目前为止,我已经很好地管理了EnumdisplayDevicesA,但是EnumdisplaySettingsA给我带来了麻烦。

无论我如何设置前两个变量,该函数都将返回零(指示失败),并且没有关于为什么失败的更多信息。

这是我的代码的简化版本,其中仅包含相关功能

TypeError: func.apply is not a function
HTMLUnkNownElement.callCallback
C:/asdasd/node_modules/react-dom/cjs/react-dom.development.js:188
  185 |     window.event = windowEvent;
  186 |   }
  187 | 
> 188 |   func.apply(context,funcArgs);
      | ^  189 |   didError = false;
  190 | } // Create a global error event handler. We use this to capture the value
  191 | // that was thrown. It's possible that this error handler will fire more

我的来源:

解决方法

此处的代码存在多个问题。

首先,您对devMode的类型定义是针对DEVMODEW的,但您正在调用EnumDisplaySettingsA。但是,您不应该首先调用它(它是ANSI版本),所以请改用EnumDisplaySettingsW(UNICODE)。

接下来,EnumDisplaySettingsA / EnumDisplaySettingsW的第二个参数是DWORD(uint32),但是,您没有传递值,而是将地址传递给了它。

所以替换

var iModeNum uint32 = 4294967295
enumCurrentSettings := uintptr(unsafe.Pointer(&iModeNum))

只要

iModeNum := uintptr(4294967295)

它应该都可以正常工作。