Does anyone know a way to detect if a USB device connected to a USB 3.0 host port is running at 3.0 or 2.0 using C#?
We are manufacturing USB 3.0 extension cables and we need to verify that all the pins have been soldered correctly. We would like to do this in software. We would like to connect a 3.0 thumb drive to the cable and check if the device is operating in USB 3.0 mode. If it is in 2.0 mode, we know their is a problem with 1 or more of the USB 3.0 lines.
I've managed to cook up a working demo with the help of some source code I found.
private static void Main(string[] args)
{
var hostCtrls = USB.GetHostControllers();
foreach (var hostCtrl in hostCtrls)
{
var hub = hostCtrl.GetRootHub();
foreach (var port in hub.GetPorts())
{
if (port.IsDeviceConnected && !port.IsHub)
{
var device = port.GetDevice();
Console.WriteLine("Serial: " + device.DeviceSerialNumber);
Console.WriteLine("Speed: " + port.Speed);
Console.WriteLine("Port: " + device.PortNumber + Environment.NewLine);
}
}
}
}
The application enumerates the USB Host Controllers. Then it gets the Root Hub and enumarates the ports belonging to it. If there is a device connected and it's not a hub then it displays the required information.
In your case you probably know which device you want to check so you can modify the source (both the above and the linked code) to specifically check only that device.
You'll need to create a method in the USB class to get a specific port from a specific hub by specifying port number and the path to the hub.
Something like:
GetDeviceSpeed(string hubPath, int portNumber) { ... }
and the call it with the appropriate values:
var hubPath = #"\\.\NUSB3#ROOT_HUB30#5&b235176&0#{f18a0e88-c30c-11d0-8815-00a0c906bed8}";
var portNumber = 2;
GetDeviceSpeed(hubPath, portNumber);
If you however are reluctant to do this then you can simply use the above code and make notice of the serial number of the device you want to test and only check the speed then:
if (device.DeviceSerialNumber == "xxxxxx")
Console.WriteLine("Speed: " + port.Speed);
If you are to use this in an application with a GUI you could just select the device you want to check in a dropdown.
Well... There are some thoughts and hopefully a working solution.
Addendum
For the sake of longevity I'll include the modified classes I used for the demo.
(Comments and empty lines removed due to SO limitations on 30000 characters):
public class USB
{
const int GENERIC_WRITE = 0x40000000;
const int FILE_SHARE_READ = 0x1;
const int FILE_SHARE_WRITE = 0x2;
const int OPEN_EXISTING = 0x3;
const int INVALID_HANDLE_VALUE = -1;
const int IOCTL_GET_HCD_DRIVERKEY_NAME = 0x220424;
const int IOCTL_USB_GET_ROOT_HUB_NAME = 0x220408;
const int IOCTL_USB_GET_NODE_INFORMATION = 0x220408;
const int IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX = 0x220448;
const int IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION = 0x220410;
const int IOCTL_USB_GET_NODE_CONNECTION_NAME = 0x220414;
const int IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME = 0x220420;
const int USB_DEVICE_DESCRIPTOR_TYPE = 0x1;
const int USB_STRING_DESCRIPTOR_TYPE = 0x3;
const int BUFFER_SIZE = 2048;
const int MAXIMUM_USB_STRING_LENGTH = 255;
const string GUID_DEVINTERFACE_HUBCONTROLLER = "3abf6f2d-71c4-462a-8a92-1e6861e6af27";
const string REGSTR_KEY_USB = "USB";
const int DIGCF_PRESENT = 0x2;
const int DIGCF_ALLCLASSES = 0x4;
const int DIGCF_DEVICEINTERFACE = 0x10;
const int SPDRP_DRIVER = 0x9;
const int SPDRP_DEVICEDESC = 0x0;
const int REG_SZ = 1;
enum USB_HUB_NODE
{
UsbHub,
UsbMIParent
}
enum USB_CONNECTION_STATUS
{
NoDeviceConnected,
DeviceConnected,
DeviceFailedEnumeration,
DeviceGeneralFailure,
DeviceCausedOvercurrent,
DeviceNotEnoughPower,
DeviceNotEnoughBandwidth,
DeviceHubNestedTooDeeply,
DeviceInLegacyHub
}
enum USB_DEVICE_SPEED : byte
{
UsbLowSpeed,
UsbFullSpeed,
UsbHighSpeed,
UsbSuperSpeed
}
[StructLayout(LayoutKind.Sequential)]
struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public IntPtr DevInst;
public IntPtr Reserved;
}
[StructLayout(LayoutKind.Sequential)]
struct SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public Guid InterfaceClassGuid;
public int Flags;
public IntPtr Reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
public int cbSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
public string DevicePath;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct USB_HCD_DRIVERKEY_NAME
{
public int ActualLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
public string DriverKeyName;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct USB_ROOT_HUB_NAME
{
public int ActualLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
public string RootHubName;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct USB_HUB_DESCRIPTOR
{
public byte bDescriptorLength;
public byte bDescriptorType;
public byte bNumberOfPorts;
public short wHubCharacteristics;
public byte bPowerOnToPowerGood;
public byte bHubControlCurrent;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public byte[] bRemoveAndPowerMask;
}
[StructLayout(LayoutKind.Sequential)]
struct USB_HUB_INFORMATION
{
public USB_HUB_DESCRIPTOR HubDescriptor;
public byte HubIsBusPowered;
}
[StructLayout(LayoutKind.Sequential)]
struct USB_NODE_INFORMATION
{
public int NodeType;
public USB_HUB_INFORMATION HubInformation;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct USB_NODE_CONNECTION_INFORMATION_EX
{
public int ConnectionIndex;
public USB_DEVICE_DESCRIPTOR DeviceDescriptor;
public byte CurrentConfigurationValue;
public byte Speed;
public byte DeviceIsHub;
public short DeviceAddress;
public int NumberOfOpenPipes;
public int ConnectionStatus;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct USB_DEVICE_DESCRIPTOR
{
public byte bLength;
public byte bDescriptorType;
public short bcdUSB;
public byte bDeviceClass;
public byte bDeviceSubClass;
public byte bDeviceProtocol;
public byte bMaxPacketSize0;
public short idVendor;
public short idProduct;
public short bcdDevice;
public byte iManufacturer;
public byte iProduct;
public byte iSerialNumber;
public byte bNumConfigurations;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct USB_STRING_DESCRIPTOR
{
public byte bLength;
public byte bDescriptorType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAXIMUM_USB_STRING_LENGTH)]
public string bString;
}
[StructLayout(LayoutKind.Sequential)]
struct USB_SETUP_PACKET
{
public byte bmRequest;
public byte bRequest;
public short wValue;
public short wIndex;
public short wLength;
}
[StructLayout(LayoutKind.Sequential)]
struct USB_DESCRIPTOR_REQUEST
{
public int ConnectionIndex;
public USB_SETUP_PACKET SetupPacket;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct USB_NODE_CONNECTION_NAME
{
public int ConnectionIndex;
public int ActualLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
public string NodeName;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct USB_NODE_CONNECTION_DRIVERKEY_NAME
{
public int ConnectionIndex;
public int ActualLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
public string DriverKeyName;
}
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
static extern IntPtr SetupDiGetClassDevs(
ref Guid ClassGuid,
int Enumerator,
IntPtr hwndParent,
int Flags
);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
static extern IntPtr SetupDiGetClassDevs(
int ClassGuid,
string Enumerator,
IntPtr hwndParent,
int Flags
);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SetupDiEnumDeviceInterfaces(
IntPtr DeviceInfoSet,
IntPtr DeviceInfoData,
ref Guid InterfaceClassGuid,
int MemberIndex,
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData
);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
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", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SetupDiGetDeviceRegistryProperty(
IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
int iProperty,
ref int PropertyRegDataType,
IntPtr PropertyBuffer,
int PropertyBufferSize,
ref int RequiredSize
);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SetupDiEnumDeviceInfo(
IntPtr DeviceInfoSet,
int MemberIndex,
ref SP_DEVINFO_DATA DeviceInfoData
);
[DllImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiDestroyDeviceInfoList(
IntPtr DeviceInfoSet
);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SetupDiGetDeviceInstanceId(
IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
StringBuilder DeviceInstanceId,
int DeviceInstanceIdSize,
out int RequiredSize
);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool DeviceIoControl(
IntPtr hDevice,
int dwIoControlCode,
IntPtr lpInBuffer,
int nInBufferSize,
IntPtr lpOutBuffer,
int nOutBufferSize,
out int lpBytesReturned,
IntPtr lpOverlapped
);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr CreateFile(
string lpFileName,
int dwDesiredAccess,
int dwShareMode,
IntPtr lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
IntPtr hTemplateFile
);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool CloseHandle(
IntPtr hObject
);
static public System.Collections.ObjectModel.ReadOnlyCollection<USBController> GetHostControllers()
{
List<USBController> HostList = new List<USBController>();
Guid HostGUID = new Guid(GUID_DEVINTERFACE_HUBCONTROLLER);
IntPtr h = SetupDiGetClassDevs(ref HostGUID, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
bool Success;
int i = 0;
do
{
USBController host = new USBController();
host.ControllerIndex = i;
SP_DEVICE_INTERFACE_DATA dia = new SP_DEVICE_INTERFACE_DATA();
dia.cbSize = Marshal.SizeOf(dia);
Success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref HostGUID, i, ref dia);
if (Success)
{
SP_DEVINFO_DATA da = new SP_DEVINFO_DATA();
da.cbSize = Marshal.SizeOf(da);
SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA();
didd.cbSize = 4 + Marshal.SystemDefaultCharSize;
int nRequiredSize = 0;
int nBytes = BUFFER_SIZE;
if (SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nBytes, ref nRequiredSize, ref da))
{
host.ControllerDevicePath = didd.DevicePath;
int RequiredSize = 0;
int RegType = REG_SZ;
if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref RegType, ptrBuf, BUFFER_SIZE, ref RequiredSize))
{
host.ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf);
}
if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref RegType, ptrBuf, BUFFER_SIZE, ref RequiredSize))
{
host.ControllerDriverKeyName = Marshal.PtrToStringAuto(ptrBuf);
}
}
HostList.Add(host);
}
i++;
} while (Success);
Marshal.FreeHGlobal(ptrBuf);
SetupDiDestroyDeviceInfoList(h);
}
return new System.Collections.ObjectModel.ReadOnlyCollection<USBController>(HostList);
}
public class USBController
{
internal int ControllerIndex;
internal string ControllerDriverKeyName, ControllerDevicePath, ControllerDeviceDesc;
public USBController()
{
ControllerIndex = 0;
ControllerDevicePath = "";
ControllerDeviceDesc = "";
ControllerDriverKeyName = "";
}
public int Index
{
get { return ControllerIndex; }
}
public string DevicePath
{
get { return ControllerDevicePath; }
}
public string DriverKeyName
{
get { return ControllerDriverKeyName; }
}
public string Name
{
get { return ControllerDeviceDesc; }
}
public USBHub GetRootHub()
{
IntPtr h, h2;
USBHub Root = new USBHub();
Root.HubIsRootHub = true;
Root.HubDeviceDesc = "Root Hub";
h = CreateFile(ControllerDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
int nBytesReturned;
USB_ROOT_HUB_NAME HubName = new USB_ROOT_HUB_NAME();
int nBytes = Marshal.SizeOf(HubName);
IntPtr ptrHubName = Marshal.AllocHGlobal(nBytes);
if (DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, ptrHubName, nBytes, ptrHubName, nBytes, out nBytesReturned, IntPtr.Zero))
{
HubName = (USB_ROOT_HUB_NAME)Marshal.PtrToStructure(ptrHubName, typeof(USB_ROOT_HUB_NAME));
Root.HubDevicePath = #"\\.\" + HubName.RootHubName;
}
h2 = CreateFile(Root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (h2.ToInt32() != INVALID_HANDLE_VALUE)
{
USB_NODE_INFORMATION NodeInfo = new USB_NODE_INFORMATION();
NodeInfo.NodeType = (int)USB_HUB_NODE.UsbHub;
nBytes = Marshal.SizeOf(NodeInfo);
IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(NodeInfo, ptrNodeInfo, true);
if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out nBytesReturned, IntPtr.Zero))
{
NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo, typeof(USB_NODE_INFORMATION));
Root.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered);
Root.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
}
Marshal.FreeHGlobal(ptrNodeInfo);
CloseHandle(h2);
}
Marshal.FreeHGlobal(ptrHubName);
CloseHandle(h);
}
return Root;
}
}
public class USBHub
{
internal int HubPortCount;
internal string HubDriverKey, HubDevicePath, HubDeviceDesc;
internal string HubManufacturer, HubProduct, HubSerialNumber, HubInstanceID;
internal bool HubIsBusPowered, HubIsRootHub;
public USBHub()
{
HubPortCount = 0;
HubDevicePath = "";
HubDeviceDesc = "";
HubDriverKey = "";
HubIsBusPowered = false;
HubIsRootHub = false;
HubManufacturer = "";
HubProduct = "";
HubSerialNumber = "";
HubInstanceID = "";
}
public int PortCount
{
get { return HubPortCount; }
}
public string DevicePath
{
get { return HubDevicePath; }
}
public string DriverKey
{
get { return HubDriverKey; }
}
public string Name
{
get { return HubDeviceDesc; }
}
public string InstanceID
{
get { return HubInstanceID; }
}
public bool IsBusPowered
{
get { return HubIsBusPowered; }
}
public bool IsRootHub
{
get { return HubIsRootHub; }
}
public string Manufacturer
{
get { return HubManufacturer; }
}
public string Product
{
get { return HubProduct; }
}
public string SerialNumber
{
get { return HubSerialNumber; }
}
public System.Collections.ObjectModel.ReadOnlyCollection<USBPort> GetPorts()
{
List<USBPort> PortList = new List<USBPort>();
IntPtr h = CreateFile(HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
int nBytes = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX));
IntPtr ptrNodeConnection = Marshal.AllocHGlobal(nBytes);
for (int i = 1; i <= HubPortCount; i++)
{
int nBytesReturned;
USB_NODE_CONNECTION_INFORMATION_EX NodeConnection = new USB_NODE_CONNECTION_INFORMATION_EX();
NodeConnection.ConnectionIndex = i;
Marshal.StructureToPtr(NodeConnection, ptrNodeConnection, true);
if (DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ptrNodeConnection, nBytes, ptrNodeConnection, nBytes, out nBytesReturned, IntPtr.Zero))
{
NodeConnection = (USB_NODE_CONNECTION_INFORMATION_EX)Marshal.PtrToStructure(ptrNodeConnection, typeof(USB_NODE_CONNECTION_INFORMATION_EX));
USBPort port = new USBPort();
port.PortPortNumber = i;
port.PortHubDevicePath = HubDevicePath;
USB_CONNECTION_STATUS Status = (USB_CONNECTION_STATUS)NodeConnection.ConnectionStatus;
port.PortStatus = Status.ToString();
USB_DEVICE_SPEED Speed = (USB_DEVICE_SPEED)NodeConnection.Speed;
port.PortSpeed = Speed.ToString();
port.PortIsDeviceConnected = (NodeConnection.ConnectionStatus == (int)USB_CONNECTION_STATUS.DeviceConnected);
port.PortIsHub = Convert.ToBoolean(NodeConnection.DeviceIsHub);
port.PortDeviceDescriptor = NodeConnection.DeviceDescriptor;
PortList.Add(port);
}
}
Marshal.FreeHGlobal(ptrNodeConnection);
CloseHandle(h);
}
return new System.Collections.ObjectModel.ReadOnlyCollection<USBPort>(PortList);
}
}
public class USBPort
{
internal int PortPortNumber;
internal string PortStatus, PortHubDevicePath, PortSpeed;
internal bool PortIsHub, PortIsDeviceConnected;
internal USB_DEVICE_DESCRIPTOR PortDeviceDescriptor;
public USBPort()
{
PortPortNumber = 0;
PortStatus = "";
PortHubDevicePath = "";
PortSpeed = "";
PortIsHub = false;
PortIsDeviceConnected = false;
}
public int PortNumber
{
get { return PortPortNumber; }
}
public string HubDevicePath
{
get { return PortHubDevicePath; }
}
public string Status
{
get { return PortStatus; }
}
public string Speed
{
get { return PortSpeed; }
}
public bool IsHub
{
get { return PortIsHub; }
}
public bool IsDeviceConnected
{
get { return PortIsDeviceConnected; }
}
public USBDevice GetDevice()
{
if (!PortIsDeviceConnected)
{
return null;
}
USBDevice Device = new USBDevice();
Device.DevicePortNumber = PortPortNumber;
Device.DeviceHubDevicePath = PortHubDevicePath;
Device.DeviceDescriptor = PortDeviceDescriptor;
IntPtr h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
int nBytesReturned;
int nBytes = BUFFER_SIZE;
string NullString = new string((char)0, BUFFER_SIZE / Marshal.SystemDefaultCharSize);
if (PortDeviceDescriptor.iManufacturer > 0)
{
USB_DESCRIPTOR_REQUEST Request = new USB_DESCRIPTOR_REQUEST();
Request.ConnectionIndex = PortPortNumber;
Request.SetupPacket.wValue = (short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iManufacturer);
Request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(Request));
Request.SetupPacket.wIndex = 0x409;
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(NullString);
Marshal.StructureToPtr(Request, ptrRequest, true);
if (DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes, ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(Request));
USB_STRING_DESCRIPTOR StringDesc = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(ptrStringDesc, typeof(USB_STRING_DESCRIPTOR));
Device.DeviceManufacturer = StringDesc.bString;
}
Marshal.FreeHGlobal(ptrRequest);
}
if (PortDeviceDescriptor.iProduct > 0)
{
USB_DESCRIPTOR_REQUEST Request = new USB_DESCRIPTOR_REQUEST();
Request.ConnectionIndex = PortPortNumber;
Request.SetupPacket.wValue = (short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iProduct);
Request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(Request));
Request.SetupPacket.wIndex = 0x409;
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(NullString);
Marshal.StructureToPtr(Request, ptrRequest, true);
if (DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes, ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(Request));
USB_STRING_DESCRIPTOR StringDesc = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(ptrStringDesc, typeof(USB_STRING_DESCRIPTOR));
Device.DeviceProduct = StringDesc.bString;
}
Marshal.FreeHGlobal(ptrRequest);
}
if (PortDeviceDescriptor.iSerialNumber > 0)
{
USB_DESCRIPTOR_REQUEST Request = new USB_DESCRIPTOR_REQUEST();
Request.ConnectionIndex = PortPortNumber;
Request.SetupPacket.wValue = (short)((USB_STRING_DESCRIPTOR_TYPE << 8) + PortDeviceDescriptor.iSerialNumber);
Request.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(Request));
Request.SetupPacket.wIndex = 0x409;
IntPtr ptrRequest = Marshal.StringToHGlobalAuto(NullString);
Marshal.StructureToPtr(Request, ptrRequest, true);
if (DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes, ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
{
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(Request));
USB_STRING_DESCRIPTOR StringDesc = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(ptrStringDesc, typeof(USB_STRING_DESCRIPTOR));
Device.DeviceSerialNumber = StringDesc.bString;
}
Marshal.FreeHGlobal(ptrRequest);
}
USB_NODE_CONNECTION_DRIVERKEY_NAME DriverKey = new USB_NODE_CONNECTION_DRIVERKEY_NAME();
DriverKey.ConnectionIndex = PortPortNumber;
nBytes = Marshal.SizeOf(DriverKey);
IntPtr ptrDriverKey = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(DriverKey, ptrDriverKey, true);
if (DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME, ptrDriverKey, nBytes, ptrDriverKey, nBytes, out nBytesReturned, IntPtr.Zero))
{
DriverKey = (USB_NODE_CONNECTION_DRIVERKEY_NAME)Marshal.PtrToStructure(ptrDriverKey, typeof(USB_NODE_CONNECTION_DRIVERKEY_NAME));
Device.DeviceDriverKey = DriverKey.DriverKeyName;
Device.DeviceName = GetDescriptionByKeyName(Device.DeviceDriverKey);
Device.DeviceInstanceID = GetInstanceIDByKeyName(Device.DeviceDriverKey);
}
Marshal.FreeHGlobal(ptrDriverKey);
CloseHandle(h);
}
return Device;
}
public USBHub GetHub()
{
if (!PortIsHub)
{
return null;
}
USBHub Hub = new USBHub();
IntPtr h, h2;
Hub.HubIsRootHub = false;
Hub.HubDeviceDesc = "External Hub";
h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
int nBytesReturned;
USB_NODE_CONNECTION_NAME NodeName = new USB_NODE_CONNECTION_NAME();
NodeName.ConnectionIndex = PortPortNumber;
int nBytes = Marshal.SizeOf(NodeName);
IntPtr ptrNodeName = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(NodeName, ptrNodeName, true);
if (DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_NAME, ptrNodeName, nBytes, ptrNodeName, nBytes, out nBytesReturned, IntPtr.Zero))
{
NodeName = (USB_NODE_CONNECTION_NAME)Marshal.PtrToStructure(ptrNodeName, typeof(USB_NODE_CONNECTION_NAME));
Hub.HubDevicePath = #"\\.\" + NodeName.NodeName;
}
h2 = CreateFile(Hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (h2.ToInt32() != INVALID_HANDLE_VALUE)
{
USB_NODE_INFORMATION NodeInfo = new USB_NODE_INFORMATION();
NodeInfo.NodeType = (int)USB_HUB_NODE.UsbHub;
nBytes = Marshal.SizeOf(NodeInfo);
IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes);
Marshal.StructureToPtr(NodeInfo, ptrNodeInfo, true);
if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out nBytesReturned, IntPtr.Zero))
{
NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo, typeof(USB_NODE_INFORMATION));
Hub.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered);
Hub.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
}
Marshal.FreeHGlobal(ptrNodeInfo);
CloseHandle(h2);
}
USBDevice Device = GetDevice();
Hub.HubInstanceID = Device.DeviceInstanceID;
Hub.HubManufacturer = Device.Manufacturer;
Hub.HubProduct = Device.Product;
Hub.HubSerialNumber = Device.SerialNumber;
Hub.HubDriverKey = Device.DriverKey;
Marshal.FreeHGlobal(ptrNodeName);
CloseHandle(h);
}
return Hub;
}
}
public class USBDevice
{
internal int DevicePortNumber;
internal string DeviceDriverKey, DeviceHubDevicePath, DeviceInstanceID, DeviceName;
internal string DeviceManufacturer, DeviceProduct, DeviceSerialNumber;
internal USB_DEVICE_DESCRIPTOR DeviceDescriptor;
public USBDevice()
{
DevicePortNumber = 0;
DeviceHubDevicePath = "";
DeviceDriverKey = "";
DeviceManufacturer = "";
DeviceProduct = "Unknown USB Device";
DeviceSerialNumber = "";
DeviceName = "";
DeviceInstanceID = "";
}
public int PortNumber
{
get { return DevicePortNumber; }
}
public string HubDevicePath
{
get { return DeviceHubDevicePath; }
}
public string DriverKey
{
get { return DeviceDriverKey; }
}
public string InstanceID
{
get { return DeviceInstanceID; }
}
public string Name
{
get { return DeviceName; }
}
public string Manufacturer
{
get { return DeviceManufacturer; }
}
public string Product
{
get { return DeviceProduct; }
}
public string SerialNumber
{
get { return DeviceSerialNumber; }
}
}
static string GetDescriptionByKeyName(string DriverKeyName)
{
string ans = "";
string DevEnum = REGSTR_KEY_USB;
IntPtr h = SetupDiGetClassDevs(0, DevEnum, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
string KeyName;
bool Success;
int i = 0;
do
{
SP_DEVINFO_DATA da = new SP_DEVINFO_DATA();
da.cbSize = Marshal.SizeOf(da);
Success = SetupDiEnumDeviceInfo(h, i, ref da);
if (Success)
{
int RequiredSize = 0;
int RegType = REG_SZ;
KeyName = "";
if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref RegType, ptrBuf, BUFFER_SIZE, ref RequiredSize))
{
KeyName = Marshal.PtrToStringAuto(ptrBuf);
}
if (KeyName == DriverKeyName)
{
if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref RegType, ptrBuf, BUFFER_SIZE, ref RequiredSize))
{
ans = Marshal.PtrToStringAuto(ptrBuf);
}
break;
}
}
i++;
} while (Success);
Marshal.FreeHGlobal(ptrBuf);
SetupDiDestroyDeviceInfoList(h);
}
return ans;
}
static string GetInstanceIDByKeyName(string DriverKeyName)
{
string ans = "";
string DevEnum = REGSTR_KEY_USB;
IntPtr h = SetupDiGetClassDevs(0, DevEnum, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
string KeyName;
bool Success;
int i = 0;
do
{
SP_DEVINFO_DATA da = new SP_DEVINFO_DATA();
da.cbSize = Marshal.SizeOf(da);
Success = SetupDiEnumDeviceInfo(h, i, ref da);
if (Success)
{
int RequiredSize = 0;
int RegType = REG_SZ;
KeyName = "";
if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref RegType, ptrBuf, BUFFER_SIZE, ref RequiredSize))
{
KeyName = Marshal.PtrToStringAuto(ptrBuf);
}
if (KeyName == DriverKeyName)
{
int nBytes = BUFFER_SIZE;
StringBuilder sb = new StringBuilder(nBytes);
SetupDiGetDeviceInstanceId(h, ref da, sb, nBytes, out RequiredSize);
ans = sb.ToString();
break;
}
}
i++;
} while (Success);
Marshal.FreeHGlobal(ptrBuf);
SetupDiDestroyDeviceInfoList(h);
}
return ans;
}
}
Related
I am facing issue while creating SSL certificate using crypt api in C#, CertCreateSelfSignCertificate is returning null with error code 87(Incorrect paramter).
Not sure what is missed.
Win32Native.CRYPTOAPI_BLOB SubjectIssuerBlob = new Win32Native.CRYPTOAPI_BLOB();
GCHandle asnNameHandle = GCHandle.Alloc(pbEncoded, GCHandleType.Pinned);
// SubjectIssuerBlob.pbData = Marshal.AllocHGlobal(pbEncoded.Length);
//Marshal.Copy(pbEncoded, 0, SubjectIssuerBlob.pbData, pbEncoded.Length);
SubjectIssuerBlob.pbData = asnNameHandle.AddrOfPinnedObject();
SubjectIssuerBlob.cbData = cbEncoded;
Win32Native.CRYPT_KEY_PROV_INFO providerInfo = new Win32Native.CRYPT_KEY_PROV_INFO();
providerInfo.pwszContainerName = hostname;
providerInfo.pwszProvName = null;
providerInfo.dwProvType = PROV_RSA_FULL;
providerInfo.dwFlags = CRYPT_MACHINE_KEYSET;
providerInfo.cProvParam = 0;
providerInfo.rgProvParam = IntPtr.Zero;
providerInfo.dwKeySpec = AT_SIGNATURE;
Win32Native.CRYPT_ALGORITHM_IDENTIFIER algorithmID = new Win32Native.CRYPT_ALGORITHM_IDENTIFIER();
algorithmID.pszObjId = "1.2.840.113549.1.1.5"; //szOID_RSA_SHA1RSA
pContext = Win32Native.CertCreateSelfSignCertificate(IntPtr.Zero, ref SubjectIssuerBlob, 0, ref providerInfo, ref algorithmID, null, endtime, IntPtr.Zero);
win32api call,
[DllImport("Crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern IntPtr CertCreateSelfSignCertificate(
IntPtr providerHandle,
ref CRYPTOAPI_BLOB subjectIssuerBlob,
uint flags,
ref CRYPT_KEY_PROV_INFO pKeyProvInfo,
ref CRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm,
SystemTime startTime,
SystemTime endTime,
IntPtr extensions);
A test with CertCreateSelfSignCertificate on Windows 10, VS 2015 =>
Dialog box (french) :
//Guid guidContainerName = Guid.NewGuid();
Guid guidContainerName = Guid.Empty;
int nRet = UuidCreate(out guidContainerName);
if (nRet == 0)
{
IntPtr pContainerName = new IntPtr();
nRet = UuidToString(ref guidContainerName, ref pContainerName);
string sContainerName;
sContainerName = Marshal.PtrToStringUni(pContainerName);
string sProviderName = MS_DEF_PROV;
IntPtr hProv = IntPtr.Zero;
bool bRet = CryptAcquireContext(ref hProv, sContainerName, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET);
IntPtr hKey = IntPtr.Zero;
bRet = CryptGenKey(hProv, AT_KEYEXCHANGE, RSA1024BIT_KEY | CRYPT_EXPORTABLE, ref hKey);
string sSubject = "CN=TEST,OU=TESTOU";
byte[] encodedSubjectName = null;
uint nNameLenght = 0;
if (CertStrToName(X509_ASN_ENCODING, sSubject, CERT_X500_NAME_STR, IntPtr.Zero, null, ref nNameLenght, IntPtr.Zero))
{
encodedSubjectName = new byte[nNameLenght];
CertStrToName(X509_ASN_ENCODING, sSubject, CERT_X500_NAME_STR, IntPtr.Zero, encodedSubjectName, ref nNameLenght, IntPtr.Zero);
}
CERT_NAME_BLOB subjectblob = new CERT_NAME_BLOB();
subjectblob.pbData = Marshal.AllocHGlobal(encodedSubjectName.Length);
Marshal.Copy(encodedSubjectName, 0, subjectblob.pbData, encodedSubjectName.Length);
subjectblob.cbData = encodedSubjectName.Length;
CRYPT_KEY_PROV_INFO pKeyProvInfo = new CRYPT_KEY_PROV_INFO();
pKeyProvInfo.pwszContainerName = sContainerName;
pKeyProvInfo.pwszProvName = sProviderName;
pKeyProvInfo.dwProvType = PROV_RSA_FULL;
pKeyProvInfo.rgProvParam = IntPtr.Zero;
pKeyProvInfo.dwKeySpec = AT_KEYEXCHANGE;
IntPtr pcCertContext = CertCreateSelfSignCertificate(hProv, ref subjectblob, 0, ref pKeyProvInfo, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
if (pcCertContext != IntPtr.Zero)
{
X509Certificate cert = new X509Certificate(pcCertContext);
// Add reference to System.Security
X509Certificate2UI.DisplayCertificate(new X509Certificate2(cert));
CertFreeCertificateContext(pcCertContext);
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
Marshal.FreeHGlobal(subjectblob.pbData);
CryptDestroyKey(hKey);
CryptReleaseContext(hProv, 0);
RpcStringFree(ref pContainerName);
}
with declarations =>
[DllImport("Crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr CertCreateSelfSignCertificate(IntPtr hCryptProvOrNCryptKey, ref CERT_NAME_BLOB pSubjectIssuerBlob, int dwFlags, ref CRYPT_KEY_PROV_INFO pKeyProvInfo,
IntPtr pSignatureAlgorithm, IntPtr pStartTime, IntPtr pEndTime, IntPtr pExtensions);
[DllImport("Crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CertFreeCertificateContext(IntPtr pCertContext);
[StructLayout(LayoutKind.Sequential)]
public struct CERT_NAME_BLOB
{
public int cbData;
public IntPtr pbData;
}
[DllImport("Rpcrt4.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int UuidCreate(out Guid Uuid);
[DllImport("rpcrt4.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int UuidToString(ref Guid uuid, ref IntPtr str);
[DllImport("rpcrt4.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int RpcStringFree(ref IntPtr str);
[DllImport("Advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CryptAcquireContext(ref IntPtr phProv, string szContainer, string szProvider, int dwProvType, uint dwFlags);
[DllImport("Advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CryptReleaseContext(IntPtr hProv, uint dwFlags);
[DllImport("Advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CryptGenKey(IntPtr hProv, uint Algid, int dwFlags, ref IntPtr phKey);
[DllImport("Advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CryptDestroyKey(IntPtr hKey);
public const int RSA1024BIT_KEY = 0x04000000;
public const int CRYPT_EXPORTABLE = 0x00000001;
[DllImport("Crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CertStrToName(uint dwCertEncodingType, string pszX500, uint dwStrType, IntPtr pvReserved,
[In, Out] byte[] pbEncoded, ref uint pcbEncoded, IntPtr ppszError);
public const int CRYPT_ASN_ENCODING = 0x00000001;
public const int CRYPT_NDR_ENCODING = 0x00000002;
public const int X509_ASN_ENCODING = 0x00000001;
public const int X509_NDR_ENCODING = 0x00000002;
public const int PKCS_7_ASN_ENCODING = 0x00010000;
public const int PKCS_7_NDR_ENCODING = 0x00020000;
public const string MS_DEF_PROV = "Microsoft Base Cryptographic Provider v1.0";
public const int PROV_RSA_FULL = 1;
public const uint CRYPT_VERIFYCONTEXT = 0xF0000000;
public const uint CRYPT_NEWKEYSET = 0x00000008;
public const uint CRYPT_DELETEKEYSET = 0x00000010;
public const uint CRYPT_MACHINE_KEYSET = 0x00000020;
public const uint CRYPT_SILENT = 0x00000040;
public const int CERT_SIMPLE_NAME_STR = 1;
public const int CERT_OID_NAME_STR = 2;
public const int CERT_X500_NAME_STR = 3;
public const int CERT_XML_NAME_STR = 4;
public const int CERT_NAME_STR_SEMICOLON_FLAG = 0x40000000;
public const int CERT_NAME_STR_NO_PLUS_FLAG = 0x20000000;
public const int CERT_NAME_STR_NO_QUOTING_FLAG = 0x10000000;
public const int CERT_NAME_STR_CRLF_FLAG = 0x08000000;
public const int CERT_NAME_STR_COMMA_FLAG = 0x04000000;
public const int CERT_NAME_STR_REVERSE_FLAG = 0x02000000;
public const int CERT_NAME_STR_FORWARD_FLAG = 0x01000000;
public const int CERT_NAME_STR_DISABLE_IE4_UTF8_FLAG = 0x00010000;
public const int CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG = 0x00020000;
public const int CERT_NAME_STR_ENABLE_UTF8_UNICODE_FLAG = 0x00040000;
public const int CERT_NAME_STR_FORCE_UTF8_DIR_STR_FLAG = 0x00080000;
public const int CERT_NAME_STR_DISABLE_UTF8_DIR_STR_FLAG = 0x00100000;
public const int CERT_NAME_STR_ENABLE_PUNYCODE_FLAG = 0x00200000;
[StructLayout(LayoutKind.Sequential)]
public struct CRYPT_KEY_PROV_INFO
{
[MarshalAs(UnmanagedType.LPWStr)] public string pwszContainerName;
[MarshalAs(UnmanagedType.LPWStr)] public string pwszProvName;
public int dwProvType;
public int dwFlags;
public int cProvParam;
public IntPtr rgProvParam;
public int dwKeySpec;
}
public const int AT_KEYEXCHANGE = 1;
public const int AT_SIGNATURE = 2;
I made a console application project for my project but its console application so how I change it to Form application... can anyone help me to change this console application to Windows form application.
Who know change console application codes to form application codes.
Please help me.. i want put "static void Main(string[] args) "codes in a button..
public class Win32API
{
[DllImport("ntdll.dll")]
public static extern int NtQueryObject(IntPtr ObjectHandle, int
ObjectInformationClass, IntPtr ObjectInformation, int ObjectInformationLength,
ref int returnLength);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax);
[DllImport("ntdll.dll")]
public static extern uint NtQuerySystemInformation(int
SystemInformationClass, IntPtr SystemInformation, int SystemInformationLength,
ref int returnLength);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr OpenMutex(UInt32 desiredAccess, bool inheritHandle, string name);
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll")]
public static extern int CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DuplicateHandle(IntPtr hSourceProcessHandle,
ushort hSourceHandle, IntPtr hTargetProcessHandle, out IntPtr lpTargetHandle,
uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwOptions);
[DllImport("kernel32.dll")]
public static extern IntPtr GetCurrentProcess();
public enum ObjectInformationClass : int
{
ObjectBasicInformation = 0,
ObjectNameInformation = 1,
ObjectTypeInformation = 2,
ObjectAllTypesInformation = 3,
ObjectHandleInformation = 4
}
[Flags]
public enum ProcessAccessFlags : uint
{
All = 0x001F0FFF,
Terminate = 0x00000001,
CreateThread = 0x00000002,
VMOperation = 0x00000008,
VMRead = 0x00000010,
VMWrite = 0x00000020,
DupHandle = 0x00000040,
SetInformation = 0x00000200,
QueryInformation = 0x00000400,
Synchronize = 0x00100000
}
[StructLayout(LayoutKind.Sequential)]
public struct OBJECT_BASIC_INFORMATION
{ // Information Class 0
public int Attributes;
public int GrantedAccess;
public int HandleCount;
public int PointerCount;
public int PagedPoolUsage;
public int NonPagedPoolUsage;
public int Reserved1;
public int Reserved2;
public int Reserved3;
public int NameInformationLength;
public int TypeInformationLength;
public int SecurityDescriptorLength;
public System.Runtime.InteropServices.ComTypes.FILETIME CreateTime;
}
[StructLayout(LayoutKind.Sequential)]
public struct OBJECT_TYPE_INFORMATION
{ // Information Class 2
public UNICODE_STRING Name;
public int ObjectCount;
public int HandleCount;
public int Reserved1;
public int Reserved2;
public int Reserved3;
public int Reserved4;
public int PeakObjectCount;
public int PeakHandleCount;
public int Reserved5;
public int Reserved6;
public int Reserved7;
public int Reserved8;
public int InvalidAttributes;
public GENERIC_MAPPING GenericMapping;
public int ValidAccess;
public byte Unknown;
public byte MaintainHandleDatabase;
public int PoolType;
public int PagedPoolUsage;
public int NonPagedPoolUsage;
}
[StructLayout(LayoutKind.Sequential)]
public struct OBJECT_NAME_INFORMATION
{ // Information Class 1
public UNICODE_STRING Name;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct UNICODE_STRING
{
public ushort Length;
public ushort MaximumLength;
public IntPtr Buffer;
}
[StructLayout(LayoutKind.Sequential)]
public struct GENERIC_MAPPING
{
public int GenericRead;
public int GenericWrite;
public int GenericExecute;
public int GenericAll;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SYSTEM_HANDLE_INFORMATION
{ // Information Class 16
public int ProcessID;
public byte ObjectTypeNumber;
public byte Flags; // 0x01 = PROTECT_FROM_CLOSE, 0x02 = INHERIT
public ushort Handle;
public int Object_Pointer;
public UInt32 GrantedAccess;
}
public const int MAX_PATH = 260;
public const uint STATUS_INFO_LENGTH_MISMATCH = 0xC0000004;
public const int DUPLICATE_SAME_ACCESS = 0x2;
public const int DUPLICATE_CLOSE_SOURCE = 0x1;
}
public class Win32Processes
{
const int CNST_SYSTEM_HANDLE_INFORMATION = 16;
const uint STATUS_INFO_LENGTH_MISMATCH = 0xc0000004;
public static string getObjectTypeName(Win32API.SYSTEM_HANDLE_INFORMATION shHandle, Process process)
{
IntPtr m_ipProcessHwnd = Win32API.OpenProcess(Win32API.ProcessAccessFlags.All, false, process.Id);
IntPtr ipHandle = IntPtr.Zero;
var objBasic = new Win32API.OBJECT_BASIC_INFORMATION();
IntPtr ipBasic = IntPtr.Zero;
var objObjectType = new Win32API.OBJECT_TYPE_INFORMATION();
IntPtr ipObjectType = IntPtr.Zero;
IntPtr ipObjectName = IntPtr.Zero;
string strObjectTypeName = "";
int nLength = 0;
int nReturn = 0;
IntPtr ipTemp = IntPtr.Zero;
if (!Win32API.DuplicateHandle(m_ipProcessHwnd, shHandle.Handle,
Win32API.GetCurrentProcess(), out ipHandle,
0, false, Win32API.DUPLICATE_SAME_ACCESS))
return null;
ipBasic = Marshal.AllocHGlobal(Marshal.SizeOf(objBasic));
Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectBasicInformation,
ipBasic, Marshal.SizeOf(objBasic), ref nLength);
objBasic = (Win32API.OBJECT_BASIC_INFORMATION)Marshal.PtrToStructure(ipBasic, objBasic.GetType());
Marshal.FreeHGlobal(ipBasic);
ipObjectType = Marshal.AllocHGlobal(objBasic.TypeInformationLength);
nLength = objBasic.TypeInformationLength;
while ((uint)(nReturn = Win32API.NtQueryObject(
ipHandle, (int)Win32API.ObjectInformationClass.ObjectTypeInformation, ipObjectType,
nLength, ref nLength)) ==
Win32API.STATUS_INFO_LENGTH_MISMATCH)
{
Marshal.FreeHGlobal(ipObjectType);
ipObjectType = Marshal.AllocHGlobal(nLength);
}
objObjectType = (Win32API.OBJECT_TYPE_INFORMATION)Marshal.PtrToStructure(ipObjectType, objObjectType.GetType());
if (Is64Bits())
{
ipTemp = new IntPtr(Convert.ToInt64(objObjectType.Name.Buffer.ToString(), 10) >> 32);
}
else
{
ipTemp = objObjectType.Name.Buffer;
}
strObjectTypeName = Marshal.PtrToStringUni(ipTemp, objObjectType.Name.Length >> 1);
Marshal.FreeHGlobal(ipObjectType);
return strObjectTypeName;
}
public static string getObjectName(Win32API.SYSTEM_HANDLE_INFORMATION shHandle, Process process)
{
IntPtr m_ipProcessHwnd = Win32API.OpenProcess(Win32API.ProcessAccessFlags.All, false, process.Id);
IntPtr ipHandle = IntPtr.Zero;
var objBasic = new Win32API.OBJECT_BASIC_INFORMATION();
IntPtr ipBasic = IntPtr.Zero;
IntPtr ipObjectType = IntPtr.Zero;
var objObjectName = new Win32API.OBJECT_NAME_INFORMATION();
IntPtr ipObjectName = IntPtr.Zero;
string strObjectName = "";
int nLength = 0;
int nReturn = 0;
IntPtr ipTemp = IntPtr.Zero;
if (!Win32API.DuplicateHandle(m_ipProcessHwnd, shHandle.Handle, Win32API.GetCurrentProcess(),
out ipHandle, 0, false, Win32API.DUPLICATE_SAME_ACCESS))
return null;
ipBasic = Marshal.AllocHGlobal(Marshal.SizeOf(objBasic));
Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectBasicInformation,
ipBasic, Marshal.SizeOf(objBasic), ref nLength);
objBasic = (Win32API.OBJECT_BASIC_INFORMATION)Marshal.PtrToStructure(ipBasic, objBasic.GetType());
Marshal.FreeHGlobal(ipBasic);
nLength = objBasic.NameInformationLength;
ipObjectName = Marshal.AllocHGlobal(nLength);
while ((uint)(nReturn = Win32API.NtQueryObject(
ipHandle, (int)Win32API.ObjectInformationClass.ObjectNameInformation,
ipObjectName, nLength, ref nLength))
== Win32API.STATUS_INFO_LENGTH_MISMATCH)
{
Marshal.FreeHGlobal(ipObjectName);
ipObjectName = Marshal.AllocHGlobal(nLength);
}
objObjectName = (Win32API.OBJECT_NAME_INFORMATION)Marshal.PtrToStructure(ipObjectName, objObjectName.GetType());
if (Is64Bits())
{
ipTemp = new IntPtr(Convert.ToInt64(objObjectName.Name.Buffer.ToString(), 10) >> 32);
}
else
{
ipTemp = objObjectName.Name.Buffer;
}
if (ipTemp != IntPtr.Zero)
{
byte[] baTemp2 = new byte[nLength];
try
{
Marshal.Copy(ipTemp, baTemp2, 0, nLength);
strObjectName = Marshal.PtrToStringUni(Is64Bits() ?
new IntPtr(ipTemp.ToInt64()) :
new IntPtr(ipTemp.ToInt32()));
return strObjectName;
}
catch (AccessViolationException)
{
return null;
}
finally
{
Marshal.FreeHGlobal(ipObjectName);
Win32API.CloseHandle(ipHandle);
}
}
return null;
}
public static List<Win32API.SYSTEM_HANDLE_INFORMATION>
GetHandles(Process process = null, string IN_strObjectTypeName = null, string IN_strObjectName = null)
{
uint nStatus;
int nHandleInfoSize = 0x10000;
IntPtr ipHandlePointer = Marshal.AllocHGlobal(nHandleInfoSize);
int nLength = 0;
IntPtr ipHandle = IntPtr.Zero;
while ((nStatus = Win32API.NtQuerySystemInformation(CNST_SYSTEM_HANDLE_INFORMATION, ipHandlePointer,
nHandleInfoSize, ref nLength)) ==
STATUS_INFO_LENGTH_MISMATCH)
{
nHandleInfoSize = nLength;
Marshal.FreeHGlobal(ipHandlePointer);
ipHandlePointer = Marshal.AllocHGlobal(nLength);
}
byte[] baTemp = new byte[nLength];
Marshal.Copy(ipHandlePointer, baTemp, 0, nLength);
long lHandleCount = 0;
if (Is64Bits())
{
lHandleCount = Marshal.ReadInt64(ipHandlePointer);
ipHandle = new IntPtr(ipHandlePointer.ToInt64() + 8);
}
else
{
lHandleCount = Marshal.ReadInt32(ipHandlePointer);
ipHandle = new IntPtr(ipHandlePointer.ToInt32() + 4);
}
Win32API.SYSTEM_HANDLE_INFORMATION shHandle;
List<Win32API.SYSTEM_HANDLE_INFORMATION> lstHandles = new List<Win32API.SYSTEM_HANDLE_INFORMATION>();
for (long lIndex = 0; lIndex < lHandleCount; lIndex++)
{
shHandle = new Win32API.SYSTEM_HANDLE_INFORMATION();
if (Is64Bits())
{
shHandle = (Win32API.SYSTEM_HANDLE_INFORMATION)Marshal.PtrToStructure(ipHandle, shHandle.GetType());
ipHandle = new IntPtr(ipHandle.ToInt64() + Marshal.SizeOf(shHandle) + 8);
}
else
{
ipHandle = new IntPtr(ipHandle.ToInt64() + Marshal.SizeOf(shHandle));
shHandle = (Win32API.SYSTEM_HANDLE_INFORMATION)Marshal.PtrToStructure(ipHandle, shHandle.GetType());
}
if (process != null)
{
if (shHandle.ProcessID != process.Id) continue;
}
string strObjectTypeName = "";
if (IN_strObjectTypeName != null){
strObjectTypeName = getObjectTypeName(shHandle, Process.GetProcessById(shHandle.ProcessID));
if (strObjectTypeName != IN_strObjectTypeName) continue;
}
string strObjectName = "";
if (IN_strObjectName != null){
strObjectName = getObjectName(shHandle, Process.GetProcessById(shHandle.ProcessID));
if (strObjectName != IN_strObjectName) continue;
}
string strObjectTypeName2 = getObjectTypeName(shHandle, Process.GetProcessById(shHandle.ProcessID));
string strObjectName2 = getObjectName(shHandle, Process.GetProcessById(shHandle.ProcessID));
Console.WriteLine("{0} {1} {2}", shHandle.ProcessID, strObjectTypeName2, strObjectName2);
lstHandles.Add(shHandle);
}
return lstHandles;
}
public static bool Is64Bits()
{
return Marshal.SizeOf(typeof(IntPtr)) == 8 ? true : false;
}
}
class Program
{
static void Main(string[] args)
{
String MutexName = "MSCTF.Asm.MutexDefault1";
String ProcessName = "notepad";
try
{
Process process = Process.GetProcessesByName(ProcessName)[0];
var handles = Win32Processes.GetHandles(process, "Mutant", "\\Sessions\\1\\BaseNamedObjects\\" + MutexName);
if (handles.Count == 0) throw new System.ArgumentException("NoMutex", "original");
foreach (var handle in handles)
{
IntPtr ipHandle = IntPtr.Zero;
if (!Win32API.DuplicateHandle(Process.GetProcessById(handle.ProcessID).Handle, handle.Handle, Win32API.GetCurrentProcess(), out ipHandle, 0, false, Win32API.DUPLICATE_CLOSE_SOURCE))
Console.WriteLine("DuplicateHandle() failed, error = {0}", Marshal.GetLastWin32Error());
Console.WriteLine("Mutex was killed");
}
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("The process name '{0}' is not currently running", ProcessName);
}
catch (ArgumentException)
{
Console.WriteLine("The Mutex '{0}' was not found in the process '{1}'", MutexName, ProcessName);
}
Console.ReadLine();
}
}
}
`
Create a new project using the Visual Studio "New Project' dialogue and select:
C# Windows Form App (.NET Framework)
Go to the designer and add a button, which can be found in the toolbox.
Then add a button click event handler, put your code inside the button click event handler.
Here's a 3 minute video showing you how to do it
Frustrated with the FolderBrowserDialog class UI, I decided to use this implementation which provides more user friendly features such as showing the folder path.
https://support.microsoft.com/en-ca/kb/306285
Unfortunately, the startLocation property only includes a few enumerated locations such as Desktop, MyDocuments, ect.
Is there a way I can modify this example to accept any file path?
Thanks!
Try this
public delegate int BrowseCallbackProc(IntPtr hwnd, int uMsg, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
[ComVisible(true)]
public class BROWSEINFO
{
public IntPtr hwndOwner;
public IntPtr pidlRoot;
public IntPtr pszDisplayName;
public string lpszTitle;
public int ulFlags;
public BrowseCallbackProc lpfn;
public IntPtr lParam;
public int iImage;
}
public class Win32SDK
{
[DllImport("shell32.dll", PreserveSig = true, CharSet = CharSet.Auto)]
public static extern IntPtr SHBrowseForFolder(BROWSEINFO bi);
[DllImport("shell32.dll", PreserveSig = true, CharSet = CharSet.Auto)]
public static extern bool SHGetPathFromIDList(IntPtr pidl, IntPtr pszPath);
[DllImport("shell32.dll", PreserveSig = true, CharSet = CharSet.Auto)]
public static extern int SHGetSpecialFolderLocation(IntPtr hwnd, int csidl, ref IntPtr ppidl);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, string lParam);
}
public enum BrowseForFolderMessages
{
BFFM_ENABLEOK = 0x465,
BFFM_INITIALIZED = 1,
BFFM_IUNKNOWN = 5,
BFFM_SELCHANGED = 2,
BFFM_SETEXPANDED = 0x46a,
BFFM_SETOKTEXT = 0x469,
BFFM_SETSELECTIONA = 0x466,
BFFM_SETSELECTIONW = 0x467,
BFFM_SETSTATUSTEXTA = 0x464,
BFFM_SETSTATUSTEXTW = 0x468,
BFFM_VALIDATEFAILEDA = 3,
BFFM_VALIDATEFAILEDW = 4
}
[Flags, Serializable]
public enum BrowseFlags
{
BIF_DEFAULT = 0x0000,
BIF_BROWSEFORCOMPUTER = 0x1000,
BIF_BROWSEFORPRINTER = 0x2000,
BIF_BROWSEINCLUDEFILES = 0x4000,
BIF_BROWSEINCLUDEURLS = 0x0080,
BIF_DONTGOBELOWDOMAIN = 0x0002,
BIF_EDITBOX = 0x0010,
BIF_NEWDIALOGSTYLE = 0x0040,
BIF_NONEWFOLDERBUTTON = 0x0200,
BIF_RETURNFSANCESTORS = 0x0008,
BIF_RETURNONLYFSDIRS = 0x0001,
BIF_SHAREABLE = 0x8000,
BIF_STATUSTEXT = 0x0004,
BIF_UAHINT = 0x0100,
BIF_VALIDATE = 0x0020,
BIF_NOTRANSLATETARGETS = 0x0400,
}
public class FolderBrowser
{
private string m_strTitle;
private BrowseFlags m_Flags;
private string m_initDir;
public FolderBrowser()
{
m_Flags = BrowseFlags.BIF_DEFAULT;
m_strTitle = "";
}
public string DirectoryPath { get; private set; }
public string DisplayName { get; private set; }
public string Title
{
set { m_strTitle = value; }
}
public BrowseFlags Flags
{
set { m_Flags = value; }
}
public DialogResult ShowDialog()
{
BROWSEINFO bi = new BROWSEINFO();
bi.pszDisplayName = IntPtr.Zero;
bi.lpfn = IntPtr.Zero;
bi.lpfn = BrowserCallBack;
bi.lParam = IntPtr.Zero;
bi.lpszTitle = "Select Folder";
IntPtr idListPtr = IntPtr.Zero;
IntPtr pszPath = IntPtr.Zero;
try
{
if (m_strTitle.Length != 0)
{
bi.lpszTitle = m_strTitle;
}
bi.ulFlags = (int)m_Flags;
bi.pszDisplayName = Marshal.AllocHGlobal(256);
// Call SHBrowseForFolder
idListPtr = Win32SDK.SHBrowseForFolder(bi);
// Check if the user cancelled out of the dialog or not.
if (idListPtr == IntPtr.Zero)
{
return DialogResult.Cancel;
}
// Allocate ncessary memory buffer to receive the folder path.
pszPath = Marshal.AllocHGlobal(256);
// Call SHGetPathFromIDList to get folder path.
bool bRet = Win32SDK.SHGetPathFromIDList(idListPtr, pszPath);
// Convert the returned native poiner to string.
DirectoryPath = Marshal.PtrToStringAuto(pszPath);
DisplayName = Marshal.PtrToStringAuto(bi.pszDisplayName);
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
return DialogResult.Abort;
}
finally
{
// Free the memory allocated by shell.
if (idListPtr != IntPtr.Zero)
{
Marshal.FreeHGlobal(idListPtr);
}
if (pszPath != IntPtr.Zero)
{
Marshal.FreeHGlobal(pszPath);
}
Marshal.FreeHGlobal(bi.pszDisplayName);
}
return DialogResult.OK;
}
private IntPtr GetStartLocationPath()
{
return IntPtr.Zero;
}
public string InitDir
{
set { m_initDir = value; }
}
private int BrowserCallBack(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam)
{
if (uMsg == (int)BrowseForFolderMessages.BFFM_INITIALIZED)
{
Win32SDK.SendMessage(hWnd, (int)BrowseForFolderMessages.BFFM_SETSELECTIONW, 1, m_initDir);
}
return 0;
}
}
Usage:
FolderBrowser browser = new FolderBrowser();
browser.Title = "Browse folder";
browser.Flags = BrowseFlags.BIF_NEWDIALOGSTYLE | BrowseFlags.BIF_NONEWFOLDERBUTTON;
browser.InitDir = Application.StartupPath; // <-- Start path
if (browser.ShowDialog() == DialogResult.OK)
{
string selectedDirectory = browser.DirectoryPath;
}
I want to get in my c# app the name of the file type, that is shown in file properties in windows... for example .log file has the type as LOG file (.log) or .bat has Batch file for Windows (.bat)(translated from my lang, so maybe not accurate).
Please, where can i find this info? or how to get to this?
i found Get-Registered-File-Types-and-Their-Associated-Ico article, where the autor shows how to get the icon, but not the type name of file that is showed in OS.
You have to call the respective Shell function SHGetFileInfo, which is a native Win32 API.
class NativeMethods
{
private const int FILE_ATTRIBUTE_NORMAL = 0x80;
private const int SHGFI_TYPENAME = 0x400;
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr SHGetFileInfo(
string pszPath,
int dwFileAttributes,
ref SHFILEINFO shinfo,
uint cbfileInfo,
int uFlags);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero;
iIcon = 0;
dwAttributes = 0;
szDisplayName = "";
szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
public static string GetShellFileType(string fileName)
{
var shinfo = new SHFILEINFO(true);
const int flags = SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES;
if (SHGetFileInfo(fileName, FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), flags) == IntPtr.Zero)
{
return "File";
}
return shinfo.szTypeName;
}
}
Then, simply call NativeMethods.GetShellFileType("...").
You can read that info from the registry
use like GetDescription("cpp") or GetDescription(".xml")
public static string ReadDefaultValue(string regKey)
{
using (var key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(regKey, false))
{
if (key != null)
{
return key.GetValue("") as string;
}
}
return null;
}
public static string GetDescription(string ext)
{
if (ext.StartsWith(".") && ext.Length > 1) ext = ext.Substring(1);
var retVal = ReadDefaultValue(ext + "file");
if (!String.IsNullOrEmpty(retVal)) return retVal;
using (var key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("." + ext, false))
{
if (key == null) return "";
using (var subkey = key.OpenSubKey("OpenWithProgids"))
{
if (subkey == null) return "";
var names = subkey.GetValueNames();
if (names == null || names.Length == 0) return "";
foreach (var name in names)
{
retVal = ReadDefaultValue(name);
if (!String.IsNullOrEmpty(retVal)) return retVal;
}
}
}
return "";
}
You can get this information using the SHGetFileInfo.
using System;
using System.Runtime.InteropServices;
namespace GetFileTypeAndDescription
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
SHFILEINFO shinfo = new SHFILEINFO();
IntPtr i = Win32.SHGetFileInfo(#"d:\temp\test.xls", 0, ref
shinfo,(uint)Marshal.SizeOf(shinfo),Win32.SHGFI_TY PENAME);
string s = Convert.ToString(shinfo.szTypeName.Trim());
Console.WriteLine(s);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
class Win32
{
public const uint SHGFI_DISPLAYNAME = 0x00000200;
public const uint SHGFI_TYPENAME = 0x400;
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint
dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
}
}
you have to use the shgetfileinfo, see below link for some code:
http://www.pinvoke.net/default.aspx/shell32.shgetfileinfo
This question already has answers here:
Set System Time Zone from .NET
(2 answers)
Closed 8 years ago.
I have tried to change the timezone of my system with c# code for a few days now and nothing I'm trying is making much sense or working at all. I started out trying to use the PInvoke syntax for SetTimeZoneInformation that I've seen on other questions here.
[DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern bool
SetTimeZoneInformation([In] ref TimeZoneInformation lpTimeZoneInformation);
I was looking into how I should create the TimeZoneInformation struct and got confused since my system obviously has a dynamic DST system. Turns out that ever since Vista Microsoft added a new way of handling Timezones. You now have to use SetDynamicTimeZoneInformation
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetDynamicTimeZoneInformation([In] ref DynamicTimeZoneInformation lpTimeZoneInformation);
I searched for help with this API on msdn and found something saying, "An application must have the SE_TIME_ZONE_NAME privilege for this function to succeed." Eventually I found this page with what seems like pretty good code for getting this done. The problem is that it's just not working.
Here is my non-working code:
[StructLayout(LayoutKind.Sequential)]
public struct LUID
{
public int LowPart;
public int HighPart;
}
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{
public LUID Luid;
public UInt32 Attributes;
public UInt32 PrivilegeCount;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct SystemTime
{
public ushort Year;
public ushort Month;
public ushort DayOfWeek;
public ushort Day;
public ushort Hour;
public ushort Minute;
public ushort Second;
public ushort Millisecond;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DynamicTimeZoneInformation
{
public int bias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string standardName;
public SystemTime standardDate;
public int standardBias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string daylightName;
public SystemTime daylightDate;
public int daylightBias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string timeZoneKeyName;
public bool dynamicDaylightTimeDisabled;
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetDynamicTimeZoneInformation(out DynamicTimeZoneInformation lpTimeZoneInformation);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetDynamicTimeZoneInformation([In] ref DynamicTimeZoneInformation lpTimeZoneInformation);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess, ref int tokenhandle);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetCurrentProcess();
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int LookupPrivilegeValue(string lpsystemname, string lpname, [MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs, [MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGES Newstate, int bufferlength, int PreivousState, int Returnlength);
public const int TOKEN_ASSIGN_PRIMARY = 0x00000001;
public const int TOKEN_DUPLICATE = 0x00000002;
public const int TOKEN_IMPERSONATE = 0x00000004;
public const int TOKEN_QUERY = 0x00000008;
public const int TOKEN_QUERY_SOURCE = 0x00000010;
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public const int TOKEN_ADJUST_GROUPS = 0x00000040;
public const int TOKEN_ADJUST_DEFAULT = 0x00000080;
public const UInt32 SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001;
public const UInt32 SE_PRIVILEGE_ENABLED = 0x00000002;
public const UInt32 SE_PRIVILEGE_REMOVED = 0x00000004;
public const UInt32 SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000;
public static bool EnablePrivilege(string privilege)
{
try
{
int token = 0;
int retVal = 0;
TOKEN_PRIVILEGES TP = new TOKEN_PRIVILEGES();
LUID LD = new LUID();
retVal = OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
retVal = LookupPrivilegeValue(null, privilege, ref LD);
TP.PrivilegeCount = 1;
TP.Attributes = SE_PRIVILEGE_ENABLED;
TP.Luid = LD;
retVal = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
return true;
}
catch
{
return false;
}
}
public static bool DisablePrivilege(string privilege)
{
try
{
int token = 0;
int retVal = 0;
TOKEN_PRIVILEGES TP = new TOKEN_PRIVILEGES();
LUID LD = new LUID();
retVal = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
retVal = LookupPrivilegeValue(null, privilege, ref LD);
TP.PrivilegeCount = 1;
// TP.Attributes should be none (not set) to disable privilege
TP.Luid = LD;
retVal = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
return true;
}
catch
{
return false;
}
}
public bool SetSystemTimeZone(string timeZoneId)
{
//I'm using the TimeZoneInfo class to populate a list in the UI. This code to retrieve the correct timezone is working
TimeZoneInfo newTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
DynamicTimeZoneInformation dtzi = new DynamicTimeZoneInformation();
dtzi.bias = -(Convert.ToInt32(newTimeZone.BaseUtcOffset.TotalMinutes));
dtzi.standardBias = 0;
//Here's where I start to lose it. I don't know which adjustment rule to use from the GetAdjustmentRules call. I'm just using the last one as that seems to usually be the most recent, but it doesn't always have to be.
dtzi.daylightBias = Convert.ToInt32(newTimeZone.GetAdjustmentRules().Last().DaylightDelta.TotalMinutes);
dtzi.standardName = newTimeZone.StandardName;
dtzi.daylightName = newTimeZone.DaylightName;
//It would be nice if this key name would just look up the timezone from the registry and use its settings
dtzi.timeZoneKeyName = newTimeZone.Id;
//No idea if this is the right way to create the SystemTime object I need here.
SystemTime standardDate = new SystemTime();
standardDate.Year = 0;
standardDate.Hour = (ushort)newTimeZone.GetAdjustmentRules().Last().DaylightTransitionEnd.TimeOfDay.Hour;
standardDate.Minute = (ushort)newTimeZone.GetAdjustmentRules().Last().DaylightTransitionEnd.TimeOfDay.Minute;
standardDate.DayOfWeek = (ushort)newTimeZone.GetAdjustmentRules().Last().DaylightTransitionEnd.DayOfWeek;
standardDate.Month = (ushort)newTimeZone.GetAdjustmentRules().Last().DaylightTransitionEnd.Month;
standardDate.Day = (ushort)newTimeZone.GetAdjustmentRules().Last().DaylightTransitionEnd.Week;
dtzi.standardDate = standardDate;
SystemTime daylightDate = new SystemTime();
standardDate.Year = 0;
standardDate.Hour = (ushort)newTimeZone.GetAdjustmentRules().Last().DaylightTransitionStart.TimeOfDay.Hour;
standardDate.Minute = (ushort)newTimeZone.GetAdjustmentRules().Last().DaylightTransitionStart.TimeOfDay.Minute;
standardDate.DayOfWeek = (ushort)newTimeZone.GetAdjustmentRules().Last().DaylightTransitionStart.DayOfWeek;
standardDate.Month = (ushort)newTimeZone.GetAdjustmentRules().Last().DaylightTransitionStart.Month;
standardDate.Day = (ushort)newTimeZone.GetAdjustmentRules().Last().DaylightTransitionStart.Week;
dtzi.daylightDate = daylightDate;
dtzi.dynamicDaylightTimeDisabled = false;
EnablePrivilege("SeTimeZonePrivilege");
if (!SetDynamicTimeZoneInformation(ref dtzi))
returnVal = false;
DisablePrivilege("SeTimeZonePrivilege");
return returnVal;
}
Has anyone had any luck with this...setting the timezone in a post Vista system? Perhaps there is a better way altogether.
Thanks
TheCloudlessSky's answer works for windows XP: starting with Vista the function to call is SetDynamicTimeZoneInformation, otherwise "Your current time zone is not recognized. Please select a valid time zone using the link below." will be shown until the next reboot.
Bonus: calling SetDynamicTimeZoneInformation makes it unnecessary to manually edit the registry, and so it won't be necessary to start the program as administrator.
Here is TheCloudlessSky's snippet modified to work on Vista and higher
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace TimeZoneTest
{
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct SystemTime
{
[MarshalAs(UnmanagedType.U2)]
public short Year;
[MarshalAs(UnmanagedType.U2)]
public short Month;
[MarshalAs(UnmanagedType.U2)]
public short DayOfWeek;
[MarshalAs(UnmanagedType.U2)]
public short Day;
[MarshalAs(UnmanagedType.U2)]
public short Hour;
[MarshalAs(UnmanagedType.U2)]
public short Minute;
[MarshalAs(UnmanagedType.U2)]
public short Second;
[MarshalAs(UnmanagedType.U2)]
public short Milliseconds;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TimeZoneInformation
{
[MarshalAs(UnmanagedType.I4)]
public int Bias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string StandardName;
public SystemTime StandardDate;
[MarshalAs(UnmanagedType.I4)]
public int StandardBias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string DaylightName;
public SystemTime DaylightDate;
[MarshalAs(UnmanagedType.I4)]
public int DaylightBias;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DynamicTimeZoneInformation
{
public int Bias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string StandardName;
public SystemTime StandardDate;
public int StandardBias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DaylightName;
public SystemTime DaylightDate;
public int DaylightBias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string TimeZoneKeyName;
[MarshalAs(UnmanagedType.U1)]
public bool DynamicDaylightTimeDisabled;
}
[StructLayout(LayoutKind.Sequential)]
public struct RegistryTimeZoneInformation
{
[MarshalAs(UnmanagedType.I4)]
public int Bias;
[MarshalAs(UnmanagedType.I4)]
public int StandardBias;
[MarshalAs(UnmanagedType.I4)]
public int DaylightBias;
public SystemTime StandardDate;
public SystemTime DaylightDate;
public RegistryTimeZoneInformation(TimeZoneInformation tzi)
{
this.Bias = tzi.Bias;
this.StandardDate = tzi.StandardDate;
this.StandardBias = tzi.StandardBias;
this.DaylightDate = tzi.DaylightDate;
this.DaylightBias = tzi.DaylightBias;
}
public RegistryTimeZoneInformation(byte[] bytes)
{
if ((bytes == null) || (bytes.Length != 0x2c))
{
throw new ArgumentException("Argument_InvalidREG_TZI_FORMAT");
}
this.Bias = BitConverter.ToInt32(bytes, 0);
this.StandardBias = BitConverter.ToInt32(bytes, 4);
this.DaylightBias = BitConverter.ToInt32(bytes, 8);
this.StandardDate.Year = BitConverter.ToInt16(bytes, 12);
this.StandardDate.Month = BitConverter.ToInt16(bytes, 14);
this.StandardDate.DayOfWeek = BitConverter.ToInt16(bytes, 0x10);
this.StandardDate.Day = BitConverter.ToInt16(bytes, 0x12);
this.StandardDate.Hour = BitConverter.ToInt16(bytes, 20);
this.StandardDate.Minute = BitConverter.ToInt16(bytes, 0x16);
this.StandardDate.Second = BitConverter.ToInt16(bytes, 0x18);
this.StandardDate.Milliseconds = BitConverter.ToInt16(bytes, 0x1a);
this.DaylightDate.Year = BitConverter.ToInt16(bytes, 0x1c);
this.DaylightDate.Month = BitConverter.ToInt16(bytes, 30);
this.DaylightDate.DayOfWeek = BitConverter.ToInt16(bytes, 0x20);
this.DaylightDate.Day = BitConverter.ToInt16(bytes, 0x22);
this.DaylightDate.Hour = BitConverter.ToInt16(bytes, 0x24);
this.DaylightDate.Minute = BitConverter.ToInt16(bytes, 0x26);
this.DaylightDate.Second = BitConverter.ToInt16(bytes, 40);
this.DaylightDate.Milliseconds = BitConverter.ToInt16(bytes, 0x2a);
}
}
public class TokenPrivilegesAccess
{
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess,
ref int tokenhandle);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetCurrentProcess();
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int LookupPrivilegeValue(string lpsystemname, string lpname,
[MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs,
[MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGE Newstate, int bufferlength,
int PreivousState, int Returnlength);
public const int TOKEN_ASSIGN_PRIMARY = 0x00000001;
public const int TOKEN_DUPLICATE = 0x00000002;
public const int TOKEN_IMPERSONATE = 0x00000004;
public const int TOKEN_QUERY = 0x00000008;
public const int TOKEN_QUERY_SOURCE = 0x00000010;
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public const int TOKEN_ADJUST_GROUPS = 0x00000040;
public const int TOKEN_ADJUST_DEFAULT = 0x00000080;
public const UInt32 SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001;
public const UInt32 SE_PRIVILEGE_ENABLED = 0x00000002;
public const UInt32 SE_PRIVILEGE_REMOVED = 0x00000004;
public const UInt32 SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000;
public static bool EnablePrivilege(string privilege)
{
try
{
int token = 0;
int retVal = 0;
TOKEN_PRIVILEGE TP = new TOKEN_PRIVILEGE();
LUID LD = new LUID();
retVal = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
retVal = LookupPrivilegeValue(null, privilege, ref LD);
TP.PrivilegeCount = 1;
var luidAndAtt = new LUID_AND_ATTRIBUTES();
luidAndAtt.Attributes = SE_PRIVILEGE_ENABLED;
luidAndAtt.Luid = LD;
TP.Privilege = luidAndAtt;
retVal = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
return true;
}
catch
{
return false;
}
}
public static bool DisablePrivilege(string privilege)
{
try
{
int token = 0;
int retVal = 0;
TOKEN_PRIVILEGE TP = new TOKEN_PRIVILEGE();
LUID LD = new LUID();
retVal = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
retVal = LookupPrivilegeValue(null, privilege, ref LD);
TP.PrivilegeCount = 1;
// TP.Attributes should be none (not set) to disable privilege
var luidAndAtt = new LUID_AND_ATTRIBUTES();
luidAndAtt.Luid = LD;
TP.Privilege = luidAndAtt;
retVal = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
return true;
}
catch
{
return false;
}
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct LUID
{
internal uint LowPart;
internal uint HighPart;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct LUID_AND_ATTRIBUTES
{
internal LUID Luid;
internal uint Attributes;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TOKEN_PRIVILEGE
{
internal uint PrivilegeCount;
internal LUID_AND_ATTRIBUTES Privilege;
}
public class Program
{
public const int ERROR_ACCESS_DENIED = 0x005;
public const int CORSEC_E_MISSING_STRONGNAME = -2146233317;
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetTimeZoneInformation([In] ref TimeZoneInformation lpTimeZoneInformation);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetDynamicTimeZoneInformation([In] ref DynamicTimeZoneInformation lpTimeZoneInformation);
public static void Main(string[] args)
{
var regTimeZones = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones");
// Print out all the possible time-zones.
//foreach(var subKey in regTimeZones.GetSubKeyNames())
//{
// Console.WriteLine(subKey);
//}
var subKey = regTimeZones.GetSubKeyNames().Where(s => s == "Atlantic Standard Time").First();
string daylightName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Dlt");
string standardName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Std");
byte[] tzi = (byte[])regTimeZones.OpenSubKey(subKey).GetValue("TZI");
var regTzi = new RegistryTimeZoneInformation(tzi);
TokenPrivilegesAccess.EnablePrivilege("SeTimeZonePrivilege");
bool didSet;
if (Environment.OSVersion.Version.Major < 6)
{
var tz = new TimeZoneInformation();
tz.Bias = regTzi.Bias;
tz.DaylightBias = regTzi.DaylightBias;
tz.StandardBias = regTzi.StandardBias;
tz.DaylightDate = regTzi.DaylightDate;
tz.StandardDate = regTzi.StandardDate;
tz.DaylightName = daylightName;
tz.StandardName = standardName;
didSet = Program.SetTimeZoneInformation(ref tz);
}
else
{
var tz = new DynamicTimeZoneInformation();
tz.Bias = regTzi.Bias;
tz.DaylightBias = regTzi.DaylightBias;
tz.StandardBias = regTzi.StandardBias;
tz.DaylightDate = regTzi.DaylightDate;
tz.StandardDate = regTzi.StandardDate;
tz.DaylightName = daylightName;
tz.StandardName = standardName;
tz.TimeZoneKeyName = subKey;
tz.DynamicDaylightTimeDisabled = false;
didSet = Program.SetDynamicTimeZoneInformation(ref tz);
}
int lastError = Marshal.GetLastWin32Error();
TokenPrivilegesAccess.DisablePrivilege("SeTimeZonePrivilege");
if (didSet)
{
Console.WriteLine("Success, TimeZone Set!");
}
else
{
if (lastError == Program.ERROR_ACCESS_DENIED)
{
Console.WriteLine("Error: Access denied... Try running application as administrator.");
}
else if (lastError == Program.CORSEC_E_MISSING_STRONGNAME)
{
Console.WriteLine("Error: Application is not signed ... Right click the project > Signing > Check 'Sign the assembly'.");
}
else
{
Console.WriteLine("Win32Error: " + lastError + "\nHRESULT: " + Marshal.GetHRForLastWin32Error());
}
}
Console.ReadLine();
}
}
}
I've done this before and here's how I did it.
The only problem is that when you check the DateTime in the task bar, it says Your current time zone is not recognized. Please select a valid time zone using the link below.. I've never been able to "fix" that... so yeah.
EDIT: I found a way to make the timezone "valid" (you must restart the machine to make it work). Be sure to read the NOTE that explains that app.manifest needs requireAdministrator. ^_^
Good luck!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace TimeZoneTest
{
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct SystemTime
{
[MarshalAs(UnmanagedType.U2)]
public short Year;
[MarshalAs(UnmanagedType.U2)]
public short Month;
[MarshalAs(UnmanagedType.U2)]
public short DayOfWeek;
[MarshalAs(UnmanagedType.U2)]
public short Day;
[MarshalAs(UnmanagedType.U2)]
public short Hour;
[MarshalAs(UnmanagedType.U2)]
public short Minute;
[MarshalAs(UnmanagedType.U2)]
public short Second;
[MarshalAs(UnmanagedType.U2)]
public short Milliseconds;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TimeZoneInformation
{
[MarshalAs(UnmanagedType.I4)]
public int Bias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string StandardName;
public SystemTime StandardDate;
[MarshalAs(UnmanagedType.I4)]
public int StandardBias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string DaylightName;
public SystemTime DaylightDate;
[MarshalAs(UnmanagedType.I4)]
public int DaylightBias;
}
[StructLayout(LayoutKind.Sequential)]
public struct RegistryTimeZoneInformation
{
[MarshalAs(UnmanagedType.I4)]
public int Bias;
[MarshalAs(UnmanagedType.I4)]
public int StandardBias;
[MarshalAs(UnmanagedType.I4)]
public int DaylightBias;
public SystemTime StandardDate;
public SystemTime DaylightDate;
public RegistryTimeZoneInformation(TimeZoneInformation tzi)
{
this.Bias = tzi.Bias;
this.StandardDate = tzi.StandardDate;
this.StandardBias = tzi.StandardBias;
this.DaylightDate = tzi.DaylightDate;
this.DaylightBias = tzi.DaylightBias;
}
public RegistryTimeZoneInformation(byte[] bytes)
{
if ((bytes == null) || (bytes.Length != 0x2c))
{
throw new ArgumentException("Argument_InvalidREG_TZI_FORMAT");
}
this.Bias = BitConverter.ToInt32(bytes, 0);
this.StandardBias = BitConverter.ToInt32(bytes, 4);
this.DaylightBias = BitConverter.ToInt32(bytes, 8);
this.StandardDate.Year = BitConverter.ToInt16(bytes, 12);
this.StandardDate.Month = BitConverter.ToInt16(bytes, 14);
this.StandardDate.DayOfWeek = BitConverter.ToInt16(bytes, 0x10);
this.StandardDate.Day = BitConverter.ToInt16(bytes, 0x12);
this.StandardDate.Hour = BitConverter.ToInt16(bytes, 20);
this.StandardDate.Minute = BitConverter.ToInt16(bytes, 0x16);
this.StandardDate.Second = BitConverter.ToInt16(bytes, 0x18);
this.StandardDate.Milliseconds = BitConverter.ToInt16(bytes, 0x1a);
this.DaylightDate.Year = BitConverter.ToInt16(bytes, 0x1c);
this.DaylightDate.Month = BitConverter.ToInt16(bytes, 30);
this.DaylightDate.DayOfWeek = BitConverter.ToInt16(bytes, 0x20);
this.DaylightDate.Day = BitConverter.ToInt16(bytes, 0x22);
this.DaylightDate.Hour = BitConverter.ToInt16(bytes, 0x24);
this.DaylightDate.Minute = BitConverter.ToInt16(bytes, 0x26);
this.DaylightDate.Second = BitConverter.ToInt16(bytes, 40);
this.DaylightDate.Milliseconds = BitConverter.ToInt16(bytes, 0x2a);
}
}
public class TokenPrivilegesAccess
{
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess,
ref int tokenhandle);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetCurrentProcess();
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int LookupPrivilegeValue(string lpsystemname, string lpname,
[MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs,
[MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGE Newstate, int bufferlength,
int PreivousState, int Returnlength);
public const int TOKEN_ASSIGN_PRIMARY = 0x00000001;
public const int TOKEN_DUPLICATE = 0x00000002;
public const int TOKEN_IMPERSONATE = 0x00000004;
public const int TOKEN_QUERY = 0x00000008;
public const int TOKEN_QUERY_SOURCE = 0x00000010;
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public const int TOKEN_ADJUST_GROUPS = 0x00000040;
public const int TOKEN_ADJUST_DEFAULT = 0x00000080;
public const UInt32 SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001;
public const UInt32 SE_PRIVILEGE_ENABLED = 0x00000002;
public const UInt32 SE_PRIVILEGE_REMOVED = 0x00000004;
public const UInt32 SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000;
public static bool EnablePrivilege(string privilege)
{
try
{
int token = 0;
int retVal = 0;
TOKEN_PRIVILEGE TP = new TOKEN_PRIVILEGE();
LUID LD = new LUID();
retVal = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
retVal = LookupPrivilegeValue(null, privilege, ref LD);
TP.PrivilegeCount = 1;
var luidAndAtt = new LUID_AND_ATTRIBUTES();
luidAndAtt.Attributes = SE_PRIVILEGE_ENABLED;
luidAndAtt.Luid = LD;
TP.Privilege = luidAndAtt;
retVal = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
return true;
}
catch
{
return false;
}
}
public static bool DisablePrivilege(string privilege)
{
try
{
int token = 0;
int retVal = 0;
TOKEN_PRIVILEGE TP = new TOKEN_PRIVILEGE();
LUID LD = new LUID();
retVal = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
retVal = LookupPrivilegeValue(null, privilege, ref LD);
TP.PrivilegeCount = 1;
// TP.Attributes should be none (not set) to disable privilege
var luidAndAtt = new LUID_AND_ATTRIBUTES();
luidAndAtt.Luid = LD;
TP.Privilege = luidAndAtt;
retVal = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
return true;
}
catch
{
return false;
}
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct LUID
{
internal uint LowPart;
internal uint HighPart;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct LUID_AND_ATTRIBUTES
{
internal LUID Luid;
internal uint Attributes;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TOKEN_PRIVILEGE
{
internal uint PrivilegeCount;
internal LUID_AND_ATTRIBUTES Privilege;
}
public class Program
{
public const int ERROR_ACCESS_DENIED = 0x005;
public const int CORSEC_E_MISSING_STRONGNAME = -2146233317;
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool SetTimeZoneInformation([In] ref TimeZoneInformation lpTimeZoneInformation);
public static void Main(string[] args)
{
var regTimeZones = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones");
// Print out all the possible time-zones.
//foreach(var subKey in regTimeZones.GetSubKeyNames())
//{
// Console.WriteLine(subKey);
//}
var subKey = regTimeZones.GetSubKeyNames().Where(s => s == "Atlantic Standard Time").First();
string daylightName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Dlt");
string standardName = (string)regTimeZones.OpenSubKey(subKey).GetValue("Std");
byte[] tzi = (byte[])regTimeZones.OpenSubKey(subKey).GetValue("TZI");
var regTzi = new RegistryTimeZoneInformation(tzi);
var tz = new TimeZoneInformation();
tz.Bias = regTzi.Bias;
tz.DaylightBias = regTzi.DaylightBias;
tz.StandardBias = regTzi.StandardBias;
tz.DaylightDate = regTzi.DaylightDate;
tz.StandardDate = regTzi.StandardDate;
tz.DaylightName = daylightName;
tz.StandardName = standardName;
TokenPrivilegesAccess.EnablePrivilege("SeTimeZonePrivilege");
bool didSet = Program.SetTimeZoneInformation(ref tz);
int lastError = Marshal.GetLastWin32Error();
TokenPrivilegesAccess.DisablePrivilege("SeTimeZonePrivilege");
// NOTE: This fixes the "Your current time zone is not recognized. Please select a valid time zone using the link below" error
// only when the machine is *restarted*.
//
// You must have the following set in your app.manifest to request admin rights to write to this key.
// <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
// <security>
// <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
// <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
// </requestedPrivileges>
// ...
var key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation", true);
key.SetValue("TimeZoneKeyName", key.GetValue("StandardName"));
if (didSet)
{
Console.WriteLine("Success, TimeZone Set!");
}
else
{
if (lastError == Program.ERROR_ACCESS_DENIED)
{
Console.WriteLine("Error: Access denied... Try running application as administrator.");
}
else if (lastError == Program.CORSEC_E_MISSING_STRONGNAME)
{
Console.WriteLine("Error: Application is not signed ... Right click the project > Signing > Check 'Sign the assembly'.");
}
else
{
Console.WriteLine("Win32Error: " + lastError + "\nHRESULT: " + Marshal.GetHRForLastWin32Error());
}
}
Console.ReadLine();
}
}
}