gpt4 book ai didi

c# - 从 user32.dll EnumDisplayDevices 取回坏字符串

转载 作者:可可西里 更新时间:2023-11-01 11:23:10 26 4
gpt4 key购买 nike

我正在尝试从 user32 获取我的监视器的名称。到目前为止,我所得到的都是汉字。我不确定为什么,我们将不胜感激。

    public static SafeNativeMethods.DISPLAY_DEVICE GetDevices()
{
SafeNativeMethods.DISPLAY_DEVICE d = new SafeNativeMethods.DISPLAY_DEVICE();
d.Initialize();
if (SafeNativeMethods.EnumDisplayDevices(null, 1, ref d, 0))
return d;
else
throw new InvalidOperationException(GetLastError());
}

public static class SafeNativeMethods
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DISPLAY_DEVICE
{
[MarshalAs(UnmanagedType.U4)]
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
[MarshalAs(UnmanagedType.U4)]
public int StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
public void Initialize()
{
this.DeviceName = new string(new char[32]);
this.DeviceString = new string(new char[128]);
this.DeviceID = new string(new char[128]);
this.DeviceKey = new string(new char[128]);
this.cb = (ushort)Marshal.SizeOf(this);
}
}



[DllImport("User32.dll", SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
public static extern Boolean EnumDisplayDevices(
[param: MarshalAs(UnmanagedType.LPTStr)]
String lpDevice,
[param: MarshalAs(UnmanagedType.U4)]
Int32 iDevNum,
[In, Out]
ref DISPLAY_DEVICE lpDisplayDevice,
[param: MarshalAs(UnmanagedType.U4)]
Int32 iHaveNoIdea
);
}

此代码返回设备名称䱉䰮䥄䁓䅌㉙ 任何人都可以解释为什么以及如何从中获得正常名称吗?

最佳答案

“汉字”usually means you tried to interpret a ASCII string as Unicode (UTF-16)。

尝试将 CharSet 属性添加到您的 EnumDisplayDevices 声明中。

关于c# - 从 user32.dll EnumDisplayDevices 取回坏字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55779016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com