How to get information about computer devices using " Kernel32.dll "

I was able to find out the serial number of the hard drive, the model and the volume of the hard one using this code.

[System.Runtime.InteropServices.DllImport("kernel32.dll")] private static extern int DeviceIoControl( int hDevice, int dwIoControlCode, [In(), Out()] SENDCMDINPARAMS lpInBuffer, int lpInBufferSize, [In(), Out()] SENDCMDOUTPARAMS lpOutBuffer, int lpOutBufferSize, ref int lpBytesReturned, int lpOverlapped ); 

Where is the information about the processor, motherboard, OS? Searched in PInvoke.net

  • one
    In the current formulation, the question is too general, since it contains three questions at once. Mentioning a specific kernel32.dll significantly reduces the number of possible responses. - Vladimir Martyanov

1 answer 1

In fact, some information can be obtained without any P / Invoke.

Environment class:

 Console.WriteLine("OS version: " + Environment.OSVersion.ToString()); Console.WriteLine("Processor count: " + Environment.ProcessorCount.ToString()); 

HKLM \ Hardware registry branch:

 using Microsoft.Win32; //... object result = Registry.GetValue( "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "ProcessorNameString", ""); if (result != null) Console.WriteLine("Processor name: " + (result).ToString()); result = Registry.GetValue( "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "~MHz", 0); if (result != null) { Console.WriteLine("Processor frequency: " + ((int)result).ToString()+" MHz"); } result = Registry.GetValue( "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\BIOS" , "BaseBoardManufacturer", ""); if (result != null) Console.WriteLine("Motherboard vendor: " + (result).ToString()); result = Registry.GetValue( "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\BIOS" , "BaseBoardProduct", 0); if (result != null) Console.WriteLine("Motherboard name: " + (result).ToString()); 

But most of the information about devices is in the SMBIOS table, which can be obtained using the GetSystemFirmwareTable function (indeed, from kernel32.dll):

 [DllImport("kernel32.dll")] public static extern uint GetSystemFirmwareTable( uint FirmwareTableProviderSignature, uint FirmwareTableID, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pFirmwareTableBuffer, uint BufferSize); // ... byte[] arr = new byte[5000]; uint sig = 0x52534D42;//RSMB uint res = GetSystemFirmwareTable(sig, 0, arr, 5000); if (res == 0 || res > 5000) { throw new ApplicationException("GetSystemFirmwareTable failed"); } 

The structure of the SMBIOS table is described in the specification: https://www.dmtf.org/standards/smbios

An example of retrieving information about hardware in the C language can be found here . Also, there is a C # SMBIOS parser code under the MPL license.