Skip to content

Instantly share code, notes, and snippets.

@hhrhhr
Created March 18, 2015 20:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhrhhr/6ecfb237de47a489efa7 to your computer and use it in GitHub Desktop.
Save hhrhhr/6ecfb237de47a489efa7 to your computer and use it in GitHub Desktop.
test access to shared memory of AIDA64 with luajit
local lpName = "AIDA64_SensorValues"
local FILE_MAP_ACCESS = 0x0004 --> FILE_MAP_READ
local ffi = require("ffi")
ffi.cdef[[
void* __stdcall OpenFileMappingA(
unsigned long dwDesiredAccess,
int bInheritHandle,
const char* lpName
);
void* __stdcall MapViewOfFile(
void* hFileMappingObject,
unsigned long dwDesiredAccess,
unsigned long dwFileOffsetHigh,
unsigned long dwFileOffsetLow,
unsigned long dwNumberOfBytesToMap
);
int __stdcall UnmapViewOfFile(const void* lpBaseAddress);
int __stdcall CloseHandle(void* hObject);
void __stdcall Sleep(unsigned long dwMilliseconds);
unsigned long __stdcall GetLastError(void);
char* strerror(int errnum);
]]
local C = ffi.C
local function error(errorString, exitCode)
local errno = C.GetLastError()
local strerror = ffi.string(C.strerror(errno))
print(string.format("%s (%d: %s)", errorString, errno, strerror))
os.exit(exitCode)
end
local hMapFile = C.OpenFileMappingA(FILE_MAP_ACCESS, 0, lpName)
if hMapFile == nil then
error("Could not open file mapping object", 1)
end
local pBuf = C.MapViewOfFile(hMapFile, FILE_MAP_ACCESS, 0, 0, 0)
if pBuf == nil then
print("CloseHandle...")
C.CloseHandle(hMapFile)
error("Could not map view of file", 2)
end
-------------------------------------------------------------------------------
local pattern = "<id>([^<]+)</id><label>([^<]+)</label><value>([^<]+)</value>"
for i = 1, 5 do
local str = ffi.string(pBuf)
for id, label, value in string.gmatch(str, pattern) do
print(string.format("%16s %32s %16s", id, label, value))
end
print()
C.Sleep(1000)
end
-------------------------------------------------------------------------------
C.UnmapViewOfFile(pBuf)
C.CloseHandle(hMapFile)
print("end")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment