c# - How get monitor's friendly name with winapi? -
c# - How get monitor's friendly name with winapi? -
i utilize win32_desktopmonitor class of wmi. not homecoming monitor name. when utilize everest(aida64) show me it. think app's work winapi. find method getmonitorinfo, not understand how utilize monitor name.
edited excuse me worst question. want friend name of monitor win api. watch thread url msdn find solution in c language.
the image on left shows looking for. image on right shows device name retrieved getmonitorinfo
.
this programme shows how phone call getmonitorinfo
using p/invoke , extract device name.
using system; using system.runtime.interopservices; namespace consoleapplication1 { class programme { [structlayout(layoutkind.sequential)] struct rect { public int left; public int top; public int right; public int bottom; } [structlayout(layoutkind.sequential, charset = charset.unicode)] struct monitorinfoex { public int size; public rect monitor; public rect workarea; public uint flags; [marshalas(unmanagedtype.byvaltstr, sizeconst = 32)] public string devicename; } [dllimport("user32.dll", charset = charset.unicode)] static extern bool getmonitorinfo(intptr hmonitor, ref monitorinfoex lpmi); delegate bool monitorenumdelegate(intptr hmonitor, intptr hdcmonitor, ref rect lprcmonitor, intptr dwdata); [dllimport("user32.dll")] static extern bool enumdisplaymonitors(intptr hdc, intptr lprcclip, monitorenumdelegate lpfnenum, intptr dwdata); static bool monitorenumproc(intptr hmonitor, intptr hdcmonitor, ref rect lprcmonitor, intptr dwdata) { monitorinfoex mi = new monitorinfoex(); mi.size = marshal.sizeof(typeof(monitorinfoex)); if (getmonitorinfo(hmonitor, ref mi)) console.writeline(mi.devicename); homecoming true; } static void main(string[] args) { enumdisplaymonitors(intptr.zero, intptr.zero, monitorenumproc, intptr.zero); } } }
according update, getmonitorinfo
not yield info need. programme based on querydisplayconfig
does. note querydisplayconfig
introduced in windows 7.
using system; using system.runtime.interopservices; using system.componentmodel; namespace consoleapplication3 { class programme { public const int error_success = 0; public enum query_device_config_flags: uint { qdc_all_paths = 0x00000001, qdc_only_active_paths = 0x00000002, qdc_database_current = 0x00000004 } public enum displayconfig_video_output_technology: uint { displayconfig_output_technology_other = 0xffffffff, displayconfig_output_technology_hd15 = 0, displayconfig_output_technology_svideo = 1, displayconfig_output_technology_composite_video = 2, displayconfig_output_technology_component_video = 3, displayconfig_output_technology_dvi = 4, displayconfig_output_technology_hdmi = 5, displayconfig_output_technology_lvds = 6, displayconfig_output_technology_d_jpn = 8, displayconfig_output_technology_sdi = 9, displayconfig_output_technology_displayport_external = 10, displayconfig_output_technology_displayport_embedded = 11, displayconfig_output_technology_udi_external = 12, displayconfig_output_technology_udi_embedded = 13, displayconfig_output_technology_sdtvdongle = 14, displayconfig_output_technology_miracast = 15, displayconfig_output_technology_internal = 0x80000000, displayconfig_output_technology_force_uint32 = 0xffffffff } public enum displayconfig_scanline_ordering: uint { displayconfig_scanline_ordering_unspecified = 0, displayconfig_scanline_ordering_progressive = 1, displayconfig_scanline_ordering_interlaced = 2, displayconfig_scanline_ordering_interlaced_upperfieldfirst = displayconfig_scanline_ordering_interlaced, displayconfig_scanline_ordering_interlaced_lowerfieldfirst = 3, displayconfig_scanline_ordering_force_uint32 = 0xffffffff } public enum displayconfig_rotation: uint { displayconfig_rotation_identity = 1, displayconfig_rotation_rotate90 = 2, displayconfig_rotation_rotate180 = 3, displayconfig_rotation_rotate270 = 4, displayconfig_rotation_force_uint32 = 0xffffffff } public enum displayconfig_scaling: uint { displayconfig_scaling_identity = 1, displayconfig_scaling_centered = 2, displayconfig_scaling_stretched = 3, displayconfig_scaling_aspectratiocenteredmax = 4, displayconfig_scaling_custom = 5, displayconfig_scaling_preferred = 128, displayconfig_scaling_force_uint32 = 0xffffffff } public enum displayconfig_pixelformat : uint { displayconfig_pixelformat_8bpp = 1, displayconfig_pixelformat_16bpp = 2, displayconfig_pixelformat_24bpp = 3, displayconfig_pixelformat_32bpp = 4, displayconfig_pixelformat_nongdi = 5, displayconfig_pixelformat_force_uint32 = 0xffffffff } public enum displayconfig_mode_info_type : uint { displayconfig_mode_info_type_source = 1, displayconfig_mode_info_type_target = 2, displayconfig_mode_info_type_force_uint32 = 0xffffffff } public enum displayconfig_device_info_type : uint { displayconfig_device_info_get_source_name = 1, displayconfig_device_info_get_target_name = 2, displayconfig_device_info_get_target_preferred_mode = 3, displayconfig_device_info_get_adapter_name = 4, displayconfig_device_info_set_target_persistence = 5, displayconfig_device_info_get_target_base_type = 6, displayconfig_device_info_force_uint32 = 0xffffffff } [structlayout(layoutkind.sequential)] public struct luid { public uint lowpart; public int highpart; } [structlayout(layoutkind.sequential)] public struct displayconfig_path_source_info { public luid adapterid; public uint id; public uint modeinfoidx; public uint statusflags; } [structlayout(layoutkind.sequential)] public struct displayconfig_path_target_info { public luid adapterid; public uint id; public uint modeinfoidx; displayconfig_video_output_technology outputtechnology; displayconfig_rotation rotation; displayconfig_scaling scaling; displayconfig_rational refreshrate; displayconfig_scanline_ordering scanlineordering; public bool targetavailable; public uint statusflags; } [structlayout(layoutkind.sequential)] public struct displayconfig_rational { public uint numerator; public uint denominator; } [structlayout(layoutkind.sequential)] public struct displayconfig_path_info { public displayconfig_path_source_info sourceinfo; public displayconfig_path_target_info targetinfo; public uint flags; } [structlayout(layoutkind.sequential)] public struct displayconfig_2dregion { public uint cx; public uint cy; } [structlayout(layoutkind.sequential)] public struct displayconfig_video_signal_info { public ulong pixelrate; public displayconfig_rational hsyncfreq; public displayconfig_rational vsyncfreq; public displayconfig_2dregion activesize; public displayconfig_2dregion totalsize; public uint videostandard; public displayconfig_scanline_ordering scanlineordering; } [structlayout(layoutkind.sequential)] public struct displayconfig_target_mode { public displayconfig_video_signal_info targetvideosignalinfo; } [structlayout(layoutkind.sequential)] public struct pointl { int x; int y; } [structlayout(layoutkind.sequential)] public struct displayconfig_source_mode { public uint width; public uint height; public displayconfig_pixelformat pixelformat; public pointl position; } [structlayout(layoutkind.explicit)] public struct displayconfig_mode_info_union { [fieldoffset(0)] public displayconfig_target_mode targetmode; [fieldoffset(0)] public displayconfig_source_mode sourcemode; } [structlayout(layoutkind.sequential)] public struct displayconfig_mode_info { public displayconfig_mode_info_type infotype; public uint id; public luid adapterid; public displayconfig_mode_info_union modeinfo; } [structlayout(layoutkind.sequential)] public struct displayconfig_target_device_name_flags { public uint value; } [structlayout(layoutkind.sequential)] public struct displayconfig_device_info_header { public displayconfig_device_info_type type; public uint size; public luid adapterid; public uint id; } [structlayout(layoutkind.sequential, charset = charset.unicode)] public struct displayconfig_target_device_name { public displayconfig_device_info_header header; public displayconfig_target_device_name_flags flags; public displayconfig_video_output_technology outputtechnology; public ushort edidmanufactureid; public ushort edidproductcodeid; public uint connectorinstance; [marshalas(unmanagedtype.byvaltstr, sizeconst = 64)] public string monitorfriendlydevicename; [marshalas(unmanagedtype.byvaltstr, sizeconst = 128)] public string monitordevicepath; } [dllimport("user32.dll")] public static extern int getdisplayconfigbuffersizes( query_device_config_flags flags, out uint numpatharrayelements, out uint nummodeinfoarrayelements ); [dllimport("user32.dll")] public static extern int querydisplayconfig( query_device_config_flags flags, ref uint numpatharrayelements, [out] displayconfig_path_info[] pathinfoarray, ref uint nummodeinfoarrayelements, [out] displayconfig_mode_info[] modeinfoarray, intptr currenttopologyid ); [dllimport("user32.dll")] public static extern int displayconfiggetdeviceinfo( ref displayconfig_target_device_name devicename ); public static string monitorfriendlyname(luid adapterid, uint targetid) { displayconfig_target_device_name devicename = new displayconfig_target_device_name(); devicename.header.size = (uint)marshal.sizeof(typeof(displayconfig_target_device_name)); devicename.header.adapterid = adapterid; devicename.header.id = targetid; devicename.header.type = displayconfig_device_info_type.displayconfig_device_info_get_target_name; int error = displayconfiggetdeviceinfo(ref devicename); if (error != error_success) throw new win32exception(error); homecoming devicename.monitorfriendlydevicename; } static void main(string[] args) { uint pathcount, modecount; int error = getdisplayconfigbuffersizes(query_device_config_flags.qdc_only_active_paths, out pathcount, out modecount); if (error != error_success) throw new win32exception(error); displayconfig_path_info[] displaypaths = new displayconfig_path_info[pathcount]; displayconfig_mode_info[] displaymodes = new displayconfig_mode_info[modecount]; error = querydisplayconfig(query_device_config_flags.qdc_only_active_paths, ref pathcount, displaypaths, ref modecount, displaymodes, intptr.zero); if (error != error_success) throw new win32exception(error); (int = 0; < modecount; i++) { if (displaymodes[i].infotype == displayconfig_mode_info_type.displayconfig_mode_info_type_target) { console.writeline(monitorfriendlyname(displaymodes[i].adapterid, displaymodes[i].id)); } } console.readline(); } } }
i've not tested @ extensively , it's quite plausible there mistakes. should on way though.
c# winapi pinvoke
Comments
Post a Comment