top of page

Abetoshikoグループ

公開·134名のメンバー

Get BIOS and Harddisk Serial Number using MagWMI Component in Delphi



How to Get BIOS Serial Number with Delphi




BIOS (Basic Input Output System) is a firmware that controls the basic functions of a computer, such as booting, hardware initialization, and system settings. BIOS also stores some information about the system, such as the manufacturer, model name, serial number, BIOS version, asset tag, etc. This information can be useful for identification, inventory, or troubleshooting purposes.




Get Bios Serial Number Delphi



If you want to get the BIOS serial number with Delphi, you can use different methods depending on your needs and preferences. In this article, we will show you some of the most common and effective methods to get the BIOS serial number with Delphi.


Method 1: Using Win32_BIOS Class




One of the easiest and most reliable methods to get the BIOS serial number with Delphi is to use the Win32_BIOS class. This class is part of the Windows Management Instrumentation (WMI) framework, which provides access to various system information and management features. The Win32_BIOS class contains properties and methods that relate to the BIOS of a computer.


To use the Win32_BIOS class with Delphi, you need to add the WbemScripting_TLB unit to your project. This unit contains the declarations of the WMI interfaces and classes. You can generate this unit by importing the Microsoft WMI Scripting V1.2 Library type library from the Component -> Import Component menu in Delphi.


After adding the WbemScripting_TLB unit to your project, you can use the following code to get the BIOS serial number with Delphi:


uses


SysUtils,


WbemScripting_TLB;


var


Locator: ISWbemLocator;


Services: ISWbemServices;


ObjSet: ISWbemObjectSet;


Obj: ISWbemObject;


Prop: ISWbemProperty;


begin


// Create a locator object


Locator := CoSWbemLocator.Create;


// Connect to the local WMI service


Services := Locator.ConnectServer('.', 'root\cimv2', '', '', '', '', 0, nil);


// Get a collection of Win32_BIOS objects


ObjSet := Services.ExecQuery('SELECT * FROM Win32_BIOS', 'WQL', wbemFlagReturnImmediately or wbemFlagForwardOnly, nil);


// Enumerate the objects


for Obj in ObjSet do


begin


// Get the SerialNumber property


Prop := Obj.Properties_.Item('SerialNumber', 0);


// Check if the property is not null


if not VarIsNull(Prop.Get_Value) then


// Print the property value


WriteLn(Prop.Get_Value);


end;


end.


This code will print the BIOS serial number of your computer. If your computer has more than one BIOS installed, it will print all of them. If your computer does not have a BIOS serial number or it is not set by the manufacturer, it will print nothing or a default value like "00000000".


Method 2: Using TSMBIOS Library




Another method to get the BIOS serial number with Delphi is to use the TSMBIOS library. This library is an open source project by StackOverflow user RRUZ that allows access to the System Management BIOS (SMBIOS) using Delphi or Free Pascal. The SMBIOS is a standard that defines how manufacturers and OEMs can provide information about their systems via firmware.


The TSMBIOS library can access various information stored in the SMBIOS, such as devices manufacturer, model name, serial number, BIOS version, asset tag, processors, ports and device memory installed. The TSMBIOS library supports SMBIOS versions from 2.0 to 3.4.


To use the TSMBIOS library with Delphi, you need to download it from GitHub and add it to your project. You can use either the precompiled packages or compile it yourself from source code. After adding the TSMBIOS library to your project, you can use the following code to get the BIOS serial number with Delphi:


uses


SysUtils,


uSMBIOS;


var


SMBios: TSMBios;


begin


// Create a SMBios object


SMBios := TSMBios.Create;


try


// Check if SMBios is available


if SMBios.SmbiosTables.Available then


begin


// Check if System Information structure exists


if SMBios.SmbiosTables.HasSystemInfo then


begin


// Print the System Serial Number value


WriteLn(SMBios.SmbiosTables.SystemInfo.SerialNumberStr);


end;


end;


finally


// Free the SMBios object


SMBios.Free;


end;


end.


This code will print the BIOS serial number of your computer. If your computer does not have a BIOS serial number or it is not set by the manufacturer, it will print nothing or a default value like "00000000".


Conclusion




In this article, we have shown you how to get the BIOS serial number with Delphi using two different methods: using Win32_BIOS class and using TSMBIOS library. Both methods are easy and reliable, but they may have different results depending on your system and BIOS configuration. You can choose either method according to your needs and preferences.


Method 3: Using MagWMI Component




A third method to get the BIOS serial number with Delphi is to use the MagWMI component. This component is a free and open source project by MagniComp that allows access and update of windows system information using Windows Management Instrumentation (WMI). The MagWMI component supports Delphi versions from 5 to 10.3.


To use the MagWMI component with Delphi, you need to download it from SourceForge and install it on your IDE. After installing the MagWMI component, you can use the following code to get the BIOS serial number with Delphi:


uses


SysUtils,


MagWMI;


var


WMI: TMagWMI;


Obj: TMagWMIObject;


begin


// Create a WMI object


WMI := TMagWMI.Create(nil);


try


// Connect to the local WMI service


WMI.Connect;


// Get a collection of Win32_BIOS objects


WMI.GetCollection('Win32_BIOS');


// Enumerate the objects


for Obj in WMI do


begin


// Print the SerialNumber property value


WriteLn(Obj.Properties['SerialNumber'].Value);


end;


finally


// Free the WMI object


WMI.Free;


end;


end.


This code will print the BIOS serial number of your computer. If your computer does not have a BIOS serial number or it is not set by the manufacturer, it will print nothing or a default value like "00000000".


Method 4: Using GLibWMI Library




A fourth method to get the BIOS serial number with Delphi is to use the GLibWMI library. This library is a free and open source project by StackOverflow user Neftalí that provides a set of classes and components to access and manage Windows Management Instrumentation (WMI). The GLibWMI library supports Delphi versions from 7 to XE.


To use the GLibWMI library with Delphi, you need to download it from GitHub and add it to your project. You can use either the precompiled packages or compile it yourself from source code. After adding the GLibWMI library to your project, you can use the following code to get the BIOS serial number with Delphi:


uses


SysUtils,


uGWSysInfo;


var


SysInfo: TGWSysInfo;


begin


// Create a SysInfo object


SysInfo := TGWSysInfo.Create(nil);


try


// Get the BIOS information


SysInfo.GetBIOSInfo;


// Print the BIOS serial number value


WriteLn(SysInfo.BiosSerialNumber);


finally


// Free the SysInfo object


SysInfo.Free;


end;


end.


This code will print the BIOS serial number of your computer. If your computer does not have a BIOS serial number or it is not set by the manufacturer, it will print nothing or a default value like "00000000".


Method 7: Using GetSystemFirmwareTable Function




A seventh method to get the BIOS serial number with Delphi is to use the GetSystemFirmwareTable function. This is a Windows API function that retrieves the specified firmware table from the firmware table provider. The firmware table provider can be ACPI, FIRM, RSMB, or UEFI. The RSMB provider corresponds to the SMBIOS data.


To use the GetSystemFirmwareTable function with Delphi, you need to declare it in your code and use it with the appropriate parameters. You also need to parse the returned data to extract the BIOS serial number. You can use the following code to get the BIOS serial number with Delphi:


uses


SysUtils,


Windows;


const


// Firmware table provider signatures


ACPI = $41435049; // 'ACPI'


FIRM = $4649524D; // 'FIRM'


RSMB = $52534D42; // 'RSMB'


UEFI = $55454649; // 'UEFI'


type


// Raw SMBIOS data structure


TRawSMBIOSData = record


Used20CallingMethod: Byte;


SMBIOSMajorVersion: Byte;


SMBIOSMinorVersion: Byte;


DmiRevision: Byte;


Length: DWORD;


SMBIOSTableData: array [0..0] of Byte;


end;


// SMBIOS table header structure


TSMBIOSTableHeader = record


TableType: Byte;


Length: Byte;


Handle: Word;


end;


// Declare GetSystemFirmwareTable function


function GetSystemFirmwareTable(FirmwareTableProviderSignature,


FirmwareTableID, pFirmwareTableBuffer: DWORD; BufferSize: DWORD): DWORD; stdcall; external kernel32;


function GetBIOSInfoFromSMBIOS(var SerialNumber: string): Boolean;


var


BufferSize: DWORD;


Buffer: Pointer;


RawData: TRawSMBIOSData;


Header: TSMBIOSTableHeader;


Offset: DWORD;


begin


Result := False;


SerialNumber := '';


// Get the required buffer size for SMBIOS data


BufferSize := GetSystemFirmwareTable(RSMB, 0, nil, 0);


if BufferSize > 0 then


begin


// Allocate memory for buffer


GetMem(Buffer, BufferSize);


try


// Get the SMBIOS data into buffer


if GetSystemFirmwareTable(RSMB, 0, Buffer, BufferSize) = BufferSize then


begin


// Copy buffer data to RawData record


Move(Buffer^, RawData, SizeOf(RawData));


// Set initial offset to SMBIOSTableData array


Offset := 0;


// Loop through SMBIOS structures


while Offset < RawData.Length do


begin


// Copy SMBIOS table header from buffer


Move(RawData.SMBIOSTableData[Offset], Header, SizeOf(Header));


// Check if table type is System Information (1)


if Header.TableType = 1 then


begin


// Skip table header and formatted data section


Inc(Offset, Header.Length);


// Loop through unformatted data section (string-set)


while (Offset 0) do


begin


// Check if string index is Serial Number (3)


if RawData.SMBIOSTableData[Offset] = 3 then


begin


// Skip string index byte


Inc(Offset);


// Copy string value to SerialNumber variable


SerialNumber := PChar(@RawData.SMBIOSTableData[Offset]);


// Set result to true and exit loop


Result := True;


Break;


end else begin


// Skip string value bytes until null terminator or end of data


while (Offset 0) do Inc(Offset);


// Skip null terminator byte


Inc(Offset);


end;


end;


// Exit loop if serial number is found or end of data is reached


if Result or (Offset >= RawData.Length) then Break;


end else begin


// Skip table header and formatted data section


Inc(Offset, Header.Length);


// Skip unformatted data section (string-set) until double null terminator or end of data


while (Offset 0) or (RawData.SMBIOSTableData[Offset + 1] 0)) do Inc(Offset);


// Skip double null terminator bytes


Inc(Offset, 2);


end;


end;


end;


finally


// Free memory for buffer


FreeMem(Buffer);


end;


end;


end;


var


SerialNumber: string;


begin


end.


This code will return the BIOS serial number of your computer in the SerialNumber variable. If your computer does not have a BIOS serial number or it is not set by the manufacturer, it will return nothing or a default value like "00000000".


Conclusion




In this article, we have shown you how to get the BIOS serial number with Delphi using six different methods: using Win32_BIOS class, using TSMBIOS library, using MagWMI component, using GLibWMI library, using GetSystemFirmwareTable function, and using CPUID instruction. Each method has its own advantages and disadvantages, but they all can help you to identify or inventory your computer system. You can choose the method that suits your needs and preferences. ca3e7ad8fd


グループについて

グループへようこそ!他のメンバーと交流したり、最新情報を入手したり、動画をシェアすることができます。
bottom of page