On my OS, Windows 10 21H1, I can change the graphic card name with SetupDI APIs
Test with my graphic card "Intel(R) HD Graphics", that I changed into "Intel(R) HD Graphics Test"
(I checked the new name in devmgmt.msc)
Must be executed as Admin (requireAdministrator in Manifest)
=>
{
string sOldName = "Intel(R) HD Graphics";
string sNewName = "Intel(R) HD Graphics Test";
IntPtr hDeviceInfo = SetupDiGetClassDevs(ref GUID_DEVINTERFACE_DISPLAY_ADAPTER, IntPtr.Zero, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if (hDeviceInfo != IntPtr.Zero)
{
var DeviceInfoData = new SP_DEVINFO_DATA() { cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA)) };
int nNumberOfDevicesPresent = 0;
while (SetupDiEnumDeviceInfo(hDeviceInfo, nNumberOfDevicesPresent, ref DeviceInfoData))
{
StringBuilder sbDeviceName = new StringBuilder(260);
sbDeviceName.Capacity = 260;
bool bRet = SetupDiGetDeviceRegistryProperty(hDeviceInfo, ref DeviceInfoData, SPDRP_FRIENDLYNAME, 0, sbDeviceName, (uint)sbDeviceName.Capacity, IntPtr.Zero);
if (bRet)
{
Console.WriteLine("Device Name : {0}", sbDeviceName.ToString());
if (sbDeviceName.ToString() == sOldName)
{
StringBuilder sbNewDeviceName = new StringBuilder(sNewName);
bRet = SetupDiSetDeviceRegistryProperty(hDeviceInfo, ref DeviceInfoData, SPDRP_FRIENDLYNAME, sbNewDeviceName, (uint)(sbNewDeviceName.Capacity * Marshal.SystemDefaultCharSize));
if (!bRet)
{
int nError = Marshal.GetLastWin32Error();
string sErrorMessage = new Win32Exception(nError).Message;
System.Windows.Forms.MessageBox.Show("Error : " + nError.ToString() + Environment.NewLine + sErrorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
else
{
bRet = SetupDiGetDeviceRegistryProperty(hDeviceInfo, ref DeviceInfoData, SPDRP_DEVICEDESC, 0, sbDeviceName, (uint)sbDeviceName.Capacity, IntPtr.Zero);
if (bRet)
{
// Device Description : Intel(R) HD Graphics
// Device Description : Pilote d’affichage de base Microsoft
Console.WriteLine("Device Description : {0}", sbDeviceName.ToString());
if (sbDeviceName.ToString() == sOldName)
{
StringBuilder sbNewDeviceName = new StringBuilder(sNewName);
bRet = SetupDiSetDeviceRegistryProperty(hDeviceInfo, ref DeviceInfoData, SPDRP_DEVICEDESC, sbNewDeviceName, (uint)(sbNewDeviceName.Capacity * Marshal.SystemDefaultCharSize));
if (!bRet)
{
int nError = Marshal.GetLastWin32Error();
string sErrorMessage = new Win32Exception(nError).Message;
System.Windows.Forms.MessageBox.Show("Error : " + nError.ToString() + Environment.NewLine + sErrorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
nNumberOfDevicesPresent++;
}
}
}
Declarations :
public const int MAX_PATH = 260;
public const int ANYSIZE_ARRAY = 1;
public const int DIGCF_PRESENT = 0x00000002;
public const int DIGCF_DEVICEINTERFACE = 0x00000010;
public const int ERROR_INSUFFICIENT_BUFFER = 122;
public static Guid GUID_DEVINTERFACE_DISK = new Guid("53f56307-b6bf-11d0-94f2-00a0c91efb8b");
public static Guid GUID_DEVINTERFACE_DISPLAY_ADAPTER = new Guid("5B45201D-F2F2-4F3B-85BB-30FF1F953599");
public static Guid GUID_DEVCLASS_DISPLAY = new Guid("4d36e968-e325-11ce-bfc1-08002be10318");
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hWndParent, int Flags);
#if WIN64
public const int PACK_SIZE = 8;
#else
public const int PACK_SIZE = 1;
#endif
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = PACK_SIZE)]
public struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public int DevInst;
public IntPtr Reserved;
}
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupDiGetDeviceProperty(
IntPtr deviceInfoSet,
[In] ref SP_DEVINFO_DATA DeviceInfoData,
[In] ref DEVPROPKEY propertyKey,
[Out] out uint propertyType,
IntPtr propertyBuffer,
uint propertyBufferSize,
out uint requiredSize,
uint flags);
public const int SPDRP_DEVICEDESC = (0x00000000); // DeviceDesc (R/W)
public const int SPDRP_FRIENDLYNAME = (0x0000000C); // FriendlyName (R/W)
public const int SPDRP_LOCATION_INFORMATION = (0x0000000D); // LocationInformation (R/W)
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, uint Property,
uint PropertyRegDataType, StringBuilder PropertyBuffer, uint PropertyBufferSize, IntPtr RequiredSize);
[StructLayout(LayoutKind.Sequential)]
public struct DEVPROPKEY
{
public Guid fmtid;
public uint pid;
}
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupDiSetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, uint Property, StringBuilder PropertyBuffer, uint PropertyBufferSize);
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, int MemberIndex, ref SP_DEVINFO_DATA DeviceInfoData);
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, ref int RequiredSize, ref SP_DEVINFO_DATA DeviceInfoData);
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr nSetupDiGetClassDevs, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, IntPtr Ptr, int DeviceInterfaceDetailDataSize, ref int RequiredSize, IntPtr PtrInfo);
[DllImport("Setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, IntPtr DeviceInfoData, ref Guid InterfaceClassGuid, int MemberIndex, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public Guid InterfaceClassGuid;
public int Flags;
public IntPtr Reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
public int cbSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string DevicePath;
}