How to call C# code in uipath to set the resolution

I am new here.

I want to pass height and width of screen resolution as parameter. How can i use this code in UiPath to set screen resolution.

My Code is:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE1
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;

public short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;

public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string dmFormName;
public short dmLogPixels;
public short dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;

public int dmDisplayFlags;
public int dmDisplayFrequency;

public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;

public int dmPanningWidth;
public int dmPanningHeight;

};

class User_32
{
[DllImport(“user32.dll”)]
public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode);
[DllImport(“user32.dll”)]
public static extern int ChangeDisplaySettings(ref DEVMODE1 devMode, int flags);

public const int ENUM_CURRENT_SETTINGS = -1;
public const int CDS_UPDATEREGISTRY = 0x01;
public const int CDS_TEST = 0x02;
public const int DISP_CHANGE_SUCCESSFUL = 0;
public const int DISP_CHANGE_RESTART = 1;
public const int DISP_CHANGE_FAILED = -1;

}

namespace Resolution
{
class CResolution
{

    public CResolution(int a, int b)
    {
        try
        {
            Screen screen = Screen.PrimaryScreen;


            int iWidth = a;
            int iHeight = b;


            DEVMODE1 dm = new DEVMODE1();
            dm.dmDeviceName = new String(new char[32]);
            dm.dmFormName = new String(new char[32]);
            dm.dmSize = (short)Marshal.SizeOf(dm);
             
            if (0 != User_32.EnumDisplaySettings(null, User_32.ENUM_CURRENT_SETTINGS, ref dm))
            {

                dm.dmPelsWidth = iWidth;
                dm.dmPelsHeight = iHeight;

                int iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_TEST);

                if (iRet == User_32.DISP_CHANGE_FAILED)
                {
                    MessageBox.Show("Unable to process your request");
                    MessageBox.Show("Description: Unable To Process Your Request. Sorry For This Inconvenience.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_UPDATEREGISTRY);

                    switch (iRet)
                    {
                        case User_32.DISP_CHANGE_SUCCESSFUL:
                            {
                                break;

                                //successfull change
                            }
                        case User_32.DISP_CHANGE_RESTART:
                            {

                                MessageBox.Show("Description: You Need To Reboot For The Change To Happen.\n If You Feel Any Problem After Rebooting Your Machine\nThen Try To Change Resolution In Safe Mode.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                break;
                                //windows 9x series you have to restart
                            }
                        default:
                            {

                                MessageBox.Show("Description: Failed To Change The Resolution.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                break;
                                //failed to change
                            }
                    }
                }

            }
        }

        catch (Exception exception)
        {

        }

    }

}
}
1 Like

Hi @pankajs3

Please refer the below threads for the same :-

Mark as solution and like it if this helps you :slight_smile:

Happy Automation :raised_hands:

Best Regards
Er Pratik Wavhal :robot::man_technologist:t4: :computer:

2 Likes

You’re going through a lot just to change the system’s resolution, dealing with Interop Services and unmanaged code is hard to maintain and requires more effort to debug. I suggest an alternative approach if you would like.
You can use QRes which is an application for your matter that can be called through CMD. It gives you more implementation choices. If you for any reason choosed calling CMD in a way which it pops up for short time, there are also ways to hide the CMD window like you can use HStart.

Hope it helps.

@atoi can tell me the to download of QRes. I am unable to download it from here.

@Pratik_Wavhal
I have tried by your way by creating custom activity but after installing .nupkg package it is displaying in project session but activity is not adding in activities session. can you suggest me.

Try this link.. Here’s a brief starting guide: itechtics.com/…

@pankajs3

Hello Pankaj,
great code, thanks for sharing.

You can use your code seamlessly in UiPath with dotNETRunner.

Change the type of the constructor parameters, it must be strings, so your constructor looks like this:

namespace Resolution {
  class CResolution {
    public CResolution(string a, string b) {
      try {
        Screen screen = Screen.PrimaryScreen;

        int iWidth = int.Parse(a);
        int iHeight = int.Parse(b);

The parameters a and b are now strings and it will be converted to int.

I tried it and it works perfect :star_struck:

image

image

Great snippet to switch the resolution of the screen on the fly inside an automation workflow :smiley:

Best regards
Stefan

Hi @pankajs3

So after creating the nuget Package i.e., .nupkg you have to place that package inside the below location :-

C:\Users\Your_System_User_Name\AppData\Local\UiPath\app-20.6.0-beta0093\Packages

Then it gets display in the activities Panel

Note :- This path is for Community Version

Or else Go to settings and then Manage Sources as shown below :-

image

You can create your own new path anywhere and then add that path in this settings → Manage Sources

Mark as solution and like it if this helps you :slight_smile:

Happy Automation :raised_hands:

Best Regards
Er Pratik Wavhal :robot::man_technologist:t4: :computer: