{"id":369,"date":"2014-09-07T13:48:43","date_gmt":"2014-09-07T11:48:43","guid":{"rendered":"http:\/\/www.tapper-ware.net\/blog\/?p=369"},"modified":"2014-09-07T14:05:53","modified_gmt":"2014-09-07T12:05:53","slug":"commandline-changing-resolution","status":"publish","type":"post","link":"https:\/\/www.tapper-ware.net\/blog\/commandline-changing-resolution\/","title":{"rendered":"Commandline: Changing resolution"},"content":{"rendered":"<p>Just had a common issue this morning that would usually require installing an application, but is very easy to solve using the batch file (<a href=\"https:\/\/gist.github.com\/hansschmucker\/820d3bdeca34c665b77a\">GIST<\/a>) from <a href=\"https:\/\/www.tapper-ware.net\/blog\/?p=349\">Thursday&#8217;s post<\/a>:<\/p>\n<p>Changing the resolution from a batch file. Specifically, I wanted to lower my display&#8217;s resolution whenever I connect via VNC. The first part is simple: Attach a task to the System Event generated by TightVNC Server (Ok, not that easy&#8230; this actually involves using Microsoft&#8217;s bizarre XPath subset, since TightVNC&#8217;s events are not quite as specific as they should be), then set this task to run a batch file.<\/p>\n<p>Now, for some reason, Microsoft doesn&#8217;t include anything to do something as simple as setting the resolution by any other means than calling into USER32.DLL directly&#8230; and that call is too complex for little old RunDLL32.exe. .NET can&#8217;t do it either without calling into USER32.dll. But at least it makes doing so pretty straightforward.<\/p>\n<p>Declare a struct that matches Windows display properties (no need to declare all fields, I just use dummy byte arrays for any fields that I&#8217;m not interested in), then call EnumDisplaySettings to retrieve the current settings into that struct. Change the resolution of the retrieved information and pass it back to ChangeDisplaySettings and voil\u00e0.<\/p>\n<p>This is also a good example of how to use arguments with C#.CMD. Just don&#8217;t. Save them to environment variables instead and retrieve them via System.Environment.GetEnvironmentVariable . SETLOCAL\/ENDLOCAL will keep these environment variables from leaking into other parts of your script.<\/p>\n<p>(<a href=\"https:\/\/gist.github.com\/hansschmucker\/b0586be4ccd63f6c9712\">GIST<\/a>)<\/p>\n<pre>@ECHO OFF\r\nSETLOCAL\r\nSET RES_X=%1\r\nSET RES_Y=%2\r\necho @^\r\n\u00a0\u00a0 \u00a0using System.Runtime.InteropServices;^\r\n\u00a0\u00a0 \u00a0[StructLayout(LayoutKind.Sequential)]^\r\n\u00a0\u00a0 \u00a0public struct DispSet {^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0[MarshalAs(UnmanagedType.ByValArray,SizeConst=106)]^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0byte[] padding0;^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0public int width, height;^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0[MarshalAs(UnmanagedType.ByValArray,SizeConst=40)]^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0byte[] padding1;^\r\n\u00a0\u00a0 \u00a0};^\r\n\u00a0\u00a0 \u00a0public class App {^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0[DllImport(\"user32.dll\")] public static extern^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0int EnumDisplaySettings(string a, int b, ref DispSet c);^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0[DllImport(\"user32.dll\")] public static extern^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0int ChangeDisplaySettings(ref DispSet a, int b);^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0public static void Main() {^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0var disp = new DispSet();^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if ( EnumDisplaySettings(null, -1, ref disp) == 0)^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return;^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0disp.width=int.Parse(System.Environment^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0.GetEnvironmentVariable(\"RES_X\"));^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0disp.height=int.Parse(System.Environment.^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0GetEnvironmentVariable(\"RES_Y\"));^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0ChangeDisplaySettings(ref disp, 1);^\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}^\r\n\u00a0\u00a0 \u00a0}^\r\n\u00a0|c#\r\nENDLOCAL<\/pre>\n<p>&nbsp;<\/p>\n<p>Assuming you have C#.CMD somewhere in your path, you can now simply call this batch file with horizontal resolution as first argument and vertical as second.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just had a common issue this morning that would usually require installing an application, but is very easy to solve using the batch file (GIST) from Thursday&#8217;s post: Changing the resolution from a batch file. Specifically, I wanted to lower my display&#8217;s resolution whenever I connect via VNC. The first part is simple: Attach a &hellip; <a href=\"https:\/\/www.tapper-ware.net\/blog\/commandline-changing-resolution\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Commandline: Changing resolution<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[29,28,31,30,32],"_links":{"self":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts\/369"}],"collection":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/comments?post=369"}],"version-history":[{"count":5,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts\/369\/revisions"}],"predecessor-version":[{"id":374,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/posts\/369\/revisions\/374"}],"wp:attachment":[{"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/media?parent=369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/categories?post=369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tapper-ware.net\/blog\/wp-json\/wp\/v2\/tags?post=369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}