c# - Communicating with Windows7 Display API -


i have yet failed find decent(anything really) pinvoke wrapper around new win7 ccd api. api can found here: http://msdn.microsoft.com/en-us/library/windows/hardware/hh406259(v=vs.85).aspx

i've spend few hours converting structures/method calls, use c#.

here first "draft":

using system; using system.runtime.interopservices;  namespace converting_ccd {     /// <summary>     /// class takes care of wrapping "connecting , configuring displays(ccd) win32 api"     /// author erti-chris eelmaa || easter199 @ hotmail dot com     /// </summary>     public class ccdwrapper     {         [structlayout(layoutkind.sequential)]         public struct luid         {             public uint lowpart;             public uint highpart;         }          [flags]         public enum displayconfigvideooutputtechnology : uint         {             other = 4294967295, // -1             hd15 = 0,             svideo = 1,             compositevideo = 2,             componentvideo = 3,             dvi = 4,             hdmi = 5,             lvds = 6,             djpn = 8,             sdi = 9,             displayportexternal = 10,             displayportembedded = 11,             udiexternal = 12,             udiembedded = 13,             sdtvdongle = 14,             internal = 0x80000000,             forceuint32 = 0xffffffff         }          #region sdcflags enum          [flags]         public enum sdcflags : uint         {             0 = 0,              topologyinternal = 0x00000001,             topologyclone = 0x00000002,             topologyextend = 0x00000004,             topologyexternal = 0x00000008,             topologysupplied = 0x00000010,              usesupplieddisplayconfig = 0x00000020,             validate = 0x00000040,             apply = 0x00000080,             nooptimization = 0x00000100,             savetodatabase = 0x00000200,             allowchanges = 0x00000400,             pathpersistifrequired = 0x00000800,             forcemodeenumeration = 0x00001000,             allowpathorderchanges = 0x00002000,              usedatabasecurrent = topologyinternal | topologyclone | topologyextend | topologyexternal         }          [flags]         public enum displayconfigflags : uint         {             0 = 0x0,             pathactive = 0x00000001         }          [flags]         public enum displayconfigsourcestatus         {             0 = 0x0,             inuse = 0x00000001         }          [flags]         public enum displayconfigtargetstatus : uint         {             0 = 0x0,              inuse                         = 0x00000001,             forcible                       = 0x00000002,             forcedavailabilityboot       = 0x00000004,             forcedavailabilitypath       = 0x00000008,             forcedavailabilitysystem     = 0x00000010,         }          [flags]         public enum displayconfigrotation : uint         {             0 = 0x0,              identity = 1,             rotate90 = 2,             rotate180 = 3,             rotate270 = 4,             forceuint32 = 0xffffffff         }          [flags]         public enum displayconfigpixelformat : uint         {             0 = 0x0,              pixelformat8bpp = 1,             pixelformat16bpp = 2,             pixelformat24bpp = 3,             pixelformat32bpp = 4,             pixelformatnongdi = 5,             pixelformatforceuint32 = 0xffffffff         }          [flags]         public enum displayconfigscaling : uint         {             0 = 0x0,               identity = 1,             centered = 2,             stretched = 3,             aspectratiocenteredmax = 4,             custom = 5,             preferred = 128,             forceuint32 = 0xffffffff         }          [structlayout(layoutkind.sequential)]         public struct displayconfigrational         {             public uint numerator;             public uint denominator;         }          [flags]         public enum displayconfigscanlineordering : uint         {             unspecified = 0,             progressive = 1,             interlaced = 2,             interlacedupperfieldfirst = interlaced,             interlacedlowerfieldfirst = 3,             forceuint32 = 0xffffffff         }          [structlayout(layoutkind.sequential)]         public struct displayconfigpathinfo         {             public displayconfigpathsourceinfo sourceinfo;             public displayconfigpathtargetinfo targetinfo;             public uint flags;         }          [flags]         public enum displayconfigmodeinfotype : uint         {             0 = 0,              source = 1,             target = 2,             forceuint32 = 0xffffffff         }          [structlayout(layoutkind.explicit)]         public struct displayconfigmodeinfo         {             [fieldoffset((0))]             public displayconfigmodeinfotype infotype;              [fieldoffset(4)]             public uint id;              [fieldoffset(8)]             public luid adapterid;              [fieldoffset(16)]             public displayconfigtargetmode targetmode;              [fieldoffset(16)]             public displayconfigsourcemode sourcemode;         }          [structlayout(layoutkind.sequential)]         public struct displayconfig2dregion         {             public uint cx;             public uint cy;         }          [flags]         public enum d3dmdtvideosignalstandard : uint         {             uninitialized = 0,             vesadmt = 1,             vesagtf = 2,             vesacvt = 3,             ibm = 4,             apple = 5,             ntscm = 6,             ntscj = 7,             ntsc443 = 8,             palb = 9,             palb1 = 10,             palg = 11,             palh = 12,             pali = 13,             pald = 14,             paln = 15,             palnc = 16,             secamb = 17,             secamd = 18,             secamg = 19,             secamh = 20,             secamk = 21,             secamk1 = 22,             secaml = 23,             secaml1 = 24,             eia861 = 25,             eia861a = 26,             eia861b = 27,             palk = 28,             palk1 = 29,             pall = 30,             palm = 31,             other = 255         }          [structlayout(layoutkind.sequential)]         public struct displayconfigvideosignalinfo         {             public long pixelrate;             public displayconfigrational hsyncfreq;             public displayconfigrational vsyncfreq;             public displayconfig2dregion activesize;             public displayconfig2dregion totalsize;              public d3dmdtvideosignalstandard videostandard;             public displayconfigscanlineordering scanlineordering;         }          [structlayout(layoutkind.sequential)]         public struct displayconfigtargetmode         {             public displayconfigvideosignalinfo targetvideosignalinfo;         }          [structlayout(layoutkind.sequential)]         public struct pointl         {             public int x;             public int y;         }          [structlayout(layoutkind.sequential)]         public struct displayconfigsourcemode         {             public uint width;             public uint height;             public displayconfigpixelformat pixelformat;             public pointl position;         }          [structlayout(layoutkind.sequential)]         public struct displayconfigpathsourceinfo         {             public luid adapterid;             public uint id;             public uint modeinfoidx;              public displayconfigsourcestatus statusflags;         }          [structlayout(layoutkind.sequential)]         public struct displayconfigpathtargetinfo         {             public luid adapterid;             public uint id;             public uint modeinfoidx;             public displayconfigvideooutputtechnology outputtechnology;              public displayconfigrotation rotation;             public displayconfigscaling scaling;             public displayconfigrational refreshrate;             public displayconfigscanlineordering scanlineordering;              public bool targetavailable;             public displayconfigtargetstatus statusflags;         }          [flags]         public enum querydisplayflags : uint         {             0 = 0x0,              allpaths = 0x00000001,             onlyactivepaths = 0x00000002,             databasecurrent = 0x00000004         }          [flags]         public enum displayconfigtopologyid : uint         {             0 = 0x0,              internal = 0x00000001,             clone = 0x00000002,             extend = 0x00000004,             external = 0x00000008,             forceuint32 = 0xffffffff         }           #endregion          [dllimport("user32.dll")]         public static extern int setdisplayconfig(uint numpatharrayelements, out displayconfigpathinfo patharray,                                                   uint nummodeinfoarrayelements, out displayconfigmodeinfo modeinfoarray,                                                   sdcflags flags);          [dllimport("user32.dll")]         public static extern int querydisplayconfig(querydisplayflags flags, ref int numpatharrayelements,                                                 ref displayconfigpathinfo[] pathinfoarray,                                                  ref int modeinfoarrayelements,                                                 ref displayconfigmodeinfo[] modeinfoarray,                                                 intptr z);          [dllimport("user32.dll")]         public static extern int getdisplayconfigbuffersizes(querydisplayflags flags, out int numpatharrayelements, out int nummodeinfoarrayelements);     } } 

i done, , when it's finished, send pinvoke.net. there problem right now, namely when call querydisplayconfig(), null-reference exception thrown, doesn't tell me @ all. i've spent few hours rechecking everything, forward, forward back.

here actual code usage:

static void main(string[] args) {     int numpatharrayelements;     int nummodeinfoarrayelements;      // query active paths current computer.     if (ccdwrapper.getdisplayconfigbuffersizes(ccdwrapper.querydisplayflags.onlyactivepaths, out numpatharrayelements,                                                    out nummodeinfoarrayelements) == 0)     {         // 0 success.         var pathinfoarray = new ccdwrapper.displayconfigpathinfo[numpatharrayelements];         var modeinfoarray = new ccdwrapper.displayconfigmodeinfo[nummodeinfoarrayelements];         ccdwrapper.displayconfigtopologyid currenttopologyid; // don't use right now.          var first = marshal.sizeof(new ccdwrapper.displayconfigpathinfo());         var second = marshal.sizeof(new ccdwrapper.displayconfigmodeinfo());          var status = ccdwrapper.querydisplayconfig(ccdwrapper.querydisplayflags.onlyactivepaths,                                 ref numpatharrayelements, ref pathinfoarray, ref nummodeinfoarrayelements,                                  ref modeinfoarray, intptr.zero);      } } 

note structure sizes should proper:

var first = marshal.sizeof(new ccdwrapper.displayconfigpathinfo()); var second = marshal.sizeof(new ccdwrapper.displayconfigmodeinfo()); 

first => 72, second => 64,

and when check c++:

int first = sizeof(displayconfig_path_info); int second = sizeof(displayconfig_mode_info); 

it gives me same result.

your handling of arrays in setdisplayconfig , querydisplayconfig declarations wrong. try these changes:

[dllimport("user32.dll")] public static extern int setdisplayconfig(     uint numpatharrayelements,      [in] displayconfigpathinfo[] patharray,     uint nummodeinfoarrayelements,      [in] displayconfigmodeinfo[] modeinfoarray,     sdcflags flags );  [dllimport("user32.dll")] public static extern int querydisplayconfig(     querydisplayflags flags,      ref int numpatharrayelements,     [out] displayconfigpathinfo[] pathinfoarray,      ref int modeinfoarrayelements,     [out] displayconfigmodeinfo[] modeinfoarray,     intptr z ); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -