我有一个脚本,它使用 powershell 中的 C# 代码来设置我正在为其开发 Windows 图像的设备的屏幕分辨率和方向。
由于各种原因,主要与屏幕方向有关,我选择不使用 unattend.xml 中的设置。
在任何目标设备上安装映像后,当我启动设备并使用我的 unattend.xml 应答文件让 OOBE 完成时,我拥有的脚本会运行,但返回一个结果表明它无法处理我的请求。
如果我随后获取完成的单元并在其上运行 sysprep 以将其设置为再次运行 OOBE,则脚本将在第二次运行时成功。
为什么会这样?
这是 powershell/C# 脚本的代码。
using System;
using System.Runtime.InteropServices;
namespace Resolution
{
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public int dmDisplayOrientation;
public int dmDisplayFixedOutput;
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 NativeMethods
{
[DllImport("user32.dll")]
public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
[DllImport("user32.dll")]
public static extern int ChangeDisplaySettings(ref DEVMODE 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;
public const int DISP_CHANGE_BADMODE = -2;
public const int DISP_CHANGE_NOTUPDATED = -3;
public const int DISP_CHANGE_BADFLAGS = -4;
public const int DISP_CHANGE_BADPARAM = -5;
public const int DISP_CHANGE_BADDUALVIEW = -6;
public const int DMDO_DEFAULT = 0;
public const int DMDO_90 = 1;
public const int DMDO_180 = 2;
public const int DMDO_270 = 3;
}
public class PrmaryScreenResolution
{
static public string SetDevAResolution() //Landscape oriented device
{
return ChangeResolution(1024, 768, NativeMethods.DMDO_DEFAULT);
}
static public string SetDevBResolution() //High resolution portrait device
{
return ChangeResolution(800, 1280, NativeMethods.DMDO_90);
}
static public string SetDevCResolution() //Portrait oriented device
{
return ChangeResolution(768, 1024, NativeMethods.DMDO_90);
}
static public string ChangeResolution(int width, int height, int orientation)
{
DEVMODE dm = GetDevMode();
if (0 != NativeMethods.EnumDisplaySettings(null, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm))
{
// Set Width and heigh
dm.dmPelsHeight = height;
dm.dmPelsWidth = width;
//dm.dmLogPixels = 96;
dm.dmDisplayOrientation = orientation;
int iRet = NativeMethods.ChangeDisplaySettings(ref dm, NativeMethods.CDS_TEST);
if (iRet == NativeMethods.DISP_CHANGE_FAILED)
{
return "Unable To Process Your Request. Sorry For This Inconvenience.";
}
else
{
iRet = NativeMethods.ChangeDisplaySettings(ref dm, NativeMethods.CDS_UPDATEREGISTRY);
switch (iRet)
{
case NativeMethods.DISP_CHANGE_SUCCESSFUL:
{
return "Success";
}
case NativeMethods.DISP_CHANGE_RESTART:
{
return "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.";
}
case NativeMethods.DISP_CHANGE_FAILED:
{
return "Display change failed.";
}
case NativeMethods.DISP_CHANGE_BADMODE:
{
return "Bad Mode";
}
case NativeMethods.DISP_CHANGE_NOTUPDATED:
{
return "Display not updated.";
}
case NativeMethods.DISP_CHANGE_BADFLAGS:
{
return "Bad Flags";
}
case NativeMethods.DISP_CHANGE_BADPARAM:
{
return "Bad Parameter";
}
case NativeMethods.DISP_CHANGE_BADDUALVIEW:
{
return "Bad Dual View";
}
default:
{
return "Failed To Change The Resolution (B)";
}
}
}
}
else
{
return "Failed To Change The Resolution. (A)";
}
}
private static DEVMODE GetDevMode()
{
DEVMODE dm = new DEVMODE();
dm.dmDeviceName = new String(new char[32]);
dm.dmFormName = new String(new char[32]);
dm.dmSize = (short)Marshal.SizeOf(dm);
return dm;
}
}
}
脚本第一次运行时(在 OOBE 尝试 1 期间),我执行了 SetDev*Resolution() 函数之一,并且“ChangeResolution()”函数返回:“无法处理您的请求。抱歉给您带来不便。”这告诉我,当我尝试调用“ChangeDisplaySettings()”时失败了。
这可能与运行此脚本时没有用户有关吗?如果是这种情况,是否有解决方法,还是我需要找出另一种方法?