I cannot test on Windows Server, but on Windows 10, this works for me, as Administrator (Manifest)
(from Accessing Device Instance SPDRP_Xxx Properties, it should work on Windows Server...)
For the test, this changes a Friendly Name "Test" into "New Name" =>
DEVPKEY_Device_FriendlyName = new DEVPROPKEY();
DEVPKEY_Device_FriendlyName.fmtid = new Guid("A45C254E-DF1C-4EFD-8020-67D146A850E0");
DEVPKEY_Device_FriendlyName.pid = 14;
string sOldName = "Test";
string sNewName = "New Name";
IntPtr hDeviceInfo = SetupDiGetClassDevs(ref GUID_DEVCLASS_NET, IntPtr.Zero, IntPtr.Zero, 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)
{
Console.WriteLine("Device Description : {0}", sbDeviceName.ToString());
}
}
nNumberOfDevicesPresent++;
}
}
Declarations :
public const int DIGCF_PRESENT = 0x00000002;
public const int DIGCF_DEVICEINTERFACE = 0x00000010;
public const int ERROR_INSUFFICIENT_BUFFER = 122;
public static Guid GUID_DEVCLASS_NET = new Guid("4d36e972-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);
[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 SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, uint Property,
uint PropertyRegDataType, StringBuilder PropertyBuffer, uint PropertyBufferSize, IntPtr RequiredSize);
[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);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DEVPROPKEY
{
public Guid fmtid;
public uint pid;
}
DEVPROPKEY DEVPKEY_Device_FriendlyName;
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)
#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;
}