我正在嘗試從 user32 獲取我的監(jiān)視器的名稱。到目前為止,我所得到的都是漢字。我不確定為什么,任何幫助將不勝感激。 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 );}此代碼返回設(shè)備名稱屜尮?偓??任何人都可以解釋為什么以及如何從中獲取正常名稱嗎?
添加回答
舉報(bào)
0/150
提交
取消