Lazarus 写一个 dll 以 json 的方式输出 smbios 信息
library smbios;
{$mode objfpc}{$H+}
uses
Classes, SysUtils, fpjson, uSMBIOS;
function GetSMBIOS(dest: PAnsiChar):integer; stdcall;
var
jObject: TJSONObject;
jArray: TJSONArray;
SMBios: TSMBios;
LBIOS : TBiosInformation;
OEMStr : TOEMStringsInformation;
i : Integer;
begin
jObject := TJSONObject.Create;
SMBios:=TSMBios.Create;
try
LBIOS:=SMBios.BiosInfo;
jObject.Add('Vendor', LBIOS.VendorStr);
jObject.Add('Version', LBIOS.VersionStr);
jObject.Add('Start Segment', IntToHex(LBIOS.RAWBiosInformation^.StartingSegment,4));
jObject.Add('ReleaseDate', LBIOS.ReleaseDateStr);
jObject.Add('Bios Rom Size', 64*(LBIOS.RAWBiosInformation^.BiosRomSize+1));
if LBIOS.RAWBiosInformation^.SystemBIOSMajorRelease<>$ff then
jObject.Add('System BIOS Major Release', LBIOS.RAWBiosInformation^.SystemBIOSMajorRelease);
if LBIOS.RAWBiosInformation^.SystemBIOSMinorRelease<>$ff then
jObject.Add('System BIOS Minor Release', LBIOS.RAWBiosInformation^.SystemBIOSMinorRelease);
//If the system does not have field upgradeable embedded controller firmware, the value is 0FFh.
if LBIOS.RAWBiosInformation^.EmbeddedControllerFirmwareMajorRelease<>$ff then
jObject.Add('Embedded Controller Firmware Major Release', LBIOS.RAWBiosInformation^.EmbeddedControllerFirmwareMajorRelease);
if LBIOS.RAWBiosInformation^.EmbeddedControllerFirmwareMinorRelease<>$ff then
jObject.Add('Embedded Controller Firmware Minor Releasee', LBIOS.RAWBiosInformation^.EmbeddedControllerFirmwareMinorRelease);
if SMBios.HasOEMStringsInfo then
begin
jArray := TJSONArray.Create;
for OEMStr in SMBios.OEMStringsInfo do
for i:=1 to OEMStr.RAWOEMStringsInformation^.Count do
jArray.Add(OEMStr.GetOEMString(i));
jObject.Add('OEM Strings', jArray);
end;
StrPLCopy(dest, jObject.AsJSON, 2048);
// Writeln(jObject.AsJSON);
finally
jObject.Free;
SMBios.Free;
end;
Result := 0;
end;
exports
GetSMBIOS;
end.
#include <catch.hpp>
#include <Windows.h>
#include <iostream>
TEST_CASE("smbios test", "[smbios test]")
{
typedef int (__stdcall *fn_GetSMBIOS)(char* dest);
// function GetSMBIOS(dest: PAnsiChar):integer; stdcall;
HMODULE hLib = LoadLibrary(L"smbios.dll");
fn_GetSMBIOS GetSMBIOS = (fn_GetSMBIOS)GetProcAddress(hLib, "GetSMBIOS");
std::vector<char> buffer(2048);
GetSMBIOS(&buffer[0]);
printf(&buffer[0]);
FreeLibrary(hLib);
}