lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


在 2017/3/2 6:15, JM Venet Magic.fr 写道:
> Hello,
>
> Yesterday,  I sent this message to have a little help of lua friends ..
>
> ------------------------
>
> Hello,
>
> I need to retrieve HDD's infos in lua, and store them in variables.
> Does someone has an idea, especially the physical serial number, not the
> one after formatting ?
> Thanks,
> JM Venet
>
> -------------------
> today, after hard searching on the net, I found this : For windows, the disk informations are retrieveable via the library kernell32.dll with the function GetVolumeInformation this function sends these datas (contained in a data structure:
>
>  _In_opt_  LPCTSTR lpRootPathName,
>   _Out_opt_ LPTSTR  lpVolumeNameBuffer,
>   _In_      DWORD   nVolumeNameSize,
>   _Out_opt_ LPDWORD lpVolumeSerialNumber,
>   _Out_opt_ LPDWORD lpMaximumComponentLength,
>   _Out_opt_ LPDWORD lpFileSystemFlags,
>   _Out_opt_ LPTSTR  lpFileSystemNameBuffer,
>   _In_      DWORD   nFileSystemNameSize
>
> I coded with Wscite this :
>
> local path = 'C:\\Windows\\System32\\kernell32.dll'
>
> local func = package.loadlib(path, 'GetVolumeInformation')
>
> a = {}
> a[1] = func.lpRootPathName
> a[2] = func.lpVolumeNameBuffer
> a[3] = func.nVolumeNameSize
> a[4] = func.lpVolumeSerialNumber
> a[5] = func.lpMaximumComponentLength
> a[6] = func.lpFileSystemFlags
> a[7] = func.lpFileSystemNameBuffer
> a[8] = func.nFileSystemNameSize
>
> after process by F5, I received the following messages
>
>>lua -e "io.stdout:setvbuf 'no'" "test.lua"
> lua: test.lua:6: attempt to index local 'func' (a nil value)
> stack traceback:
> 	test.lua:6: in main chunk
> 	[C]: ?
>>Exit code: 1
>
> If I put the lines from the statement a = {}, to the end in comments the messages are : (func is nil)
> lua -e "io.stdout:setvbuf 'no'" "test.lua"
>>Exit code: 0
>
> is there someone who can give me a little help to retrieve the datas contained in the stucture returned by the function GetVolumeInformation thanks for help JM Venet
>

simple way using external commands:

if you need the harddisk serial number, try this:

--------------------------------------------------
local pipe = io.popen("wmic diskdrive get serialnumber")
local result = pipe:read("*a")
print(result)
--------------------------------------------------

if you have more than one disks installed, use this command to tell which is which:

    wmic diskdrive get name,model,serialnumber

if you need the C: volume serial number, try "vol C:" command.

since the results are texts, you still need to parse the answer to extract the information you need.
but it should not be difficult.



complicated way with some Windows API programming:

GetVolumeInformation is to retrieve the information of a volume (a.k.a partition or logical drive).

kernel32.dll is a system library, not a Lua module, and GetVolumeInformation is a native function
with Windows specific call convention, thus not understandable by Lua VM.

to call native code from Lua, you need a Lua binding to it, either through the Lua C API or via ffi.

if you don't have a C compiler and/or don't know C programming language, I'd suggest using the LuaJIT ffi,
which doesn't need a C compiler to work with. but you still need to declare and load the function first.
normally you can just copy & paste the C header file declaration into a `ffi.cdef' block, but to care
those Windows API specific typedefs and annotations.

and if you use ffi, don't forget to use the real function name GetVolumeInformationW/GetVolumeInformationA.


to get hardware serial number takes a little more programming effort. one among many ways is to call
DeviceIoControl with the control code IOCTL_STORAGE_QUERY_PROPERTY. check msdn documents for details.
I believe there exist APIs equivalent to the wmic commands, but I didn't look into it.


-- 
the nerdy Peng / 书呆彭 /