如何在Lua上使用Win API获取硬盘的序列号

问题描述 投票:0回答:1
local ffi = require("ffi")
ffi.cdef[[
  int __stdcall GetVolumeInformationA(
  const char* lpRootPathName,
  char* lpVolumeNameBuffer,
  uint32_t nVolumeNameSize,
  uint32_t* lpVolumeSerialNumber,
  uint32_t* lpMaximumComponentLength,
  uint32_t* lpFileSystemFlags,
  char* lpFileSystemNameBuffer,
  uint32_t nFileSystemNameSize
);
]]
local serial = ffi.new("unsigned long[1]", 0)
ffi.C.GetVolumeInformationA(nil, nil, 0, serial, nil, nil, nil, 0)
serial1 = serial[0]

这将给出逻辑驱动器的序列号,我需要获取硬盘的序列号。我怎么才能得到它?

api winapi lua hard-drive
1个回答
0
投票

wmic不是WinAPI的一部分,但它提供了更简单的解决方案:

local function get_HDD0_serial()
   local pipe = io.popen"wmic diskdrive where(index=0) get serialnumber /value"
   local serial = (pipe:read"*a":match"SerialNumber=([^\r\n]*)" or ""):match"^(.-)%s*$"
   pipe:close()
   return serial
end

print(get_HDD0_serial())

如果您只想要WinAPI解决方案,可以使用getWmiQueryResult重写它

© www.soinside.com 2019 - 2024. All rights reserved.