diff --git a/nl_luainit.lua b/nl_luainit.lua index a327bd7..8ed7a80 100644 --- a/nl_luainit.lua +++ b/nl_luainit.lua @@ -57,6 +57,7 @@ ffi.cdef[[ DWORD64 nl_BaseOfDll; LONG nl_attach(PVOID *ppPointer, PVOID pDetour); LONG nl_detach(PVOID *ppPointer, PVOID pDetour); + DWORD tempPrintLastError(); typedef void CxxClass; ]] @@ -399,19 +400,23 @@ end -- Find a symbol index. local function nl_symbol(name) + print("calling nl_symbol(\""..name.."\")") local si = new("SYMBOL_INFO[1]") si[0].SizeOfStruct = sizeof("SYMBOL_INFO") si[0].MaxNameLen = 0 - local ok = Dbghelp.SymFromName(NLAPI.nl_hProcess, name, si) - if ok then return si[0] end + local ret = Dbghelp.SymFromName(NLAPI.nl_hProcess, name, si) + if ret == 1 then return si[0] end + NLAPI.tempPrintLastError() end local function nl_type(name) + print("calling nl_type(\""..name.."\")") local si = new("SYMBOL_INFO[1]") si[0].SizeOfStruct = sizeof("SYMBOL_INFO") si[0].MaxNameLen = 0 - local ok = Dbghelp.SymGetTypeFromName(NLAPI.nl_hProcess, NLAPI.nl_BaseOfDll, name, si) - if ok then return si[0] end + local ret = Dbghelp.SymGetTypeFromName(NLAPI.nl_hProcess, NLAPI.nl_BaseOfDll, name, si) + if ret == 1 then return si[0] end + NLAPI.tempPrintLastError() end -- Get the value of a symbol. diff --git a/nl_payload.c b/nl_payload.c index d569182..b041b4f 100644 --- a/nl_payload.c +++ b/nl_payload.c @@ -47,6 +47,32 @@ __declspec(dllexport) LONG nl_detach(PVOID *ppPointer, PVOID pDetour) return ret; } +// idk how to thread this through to Lua; this is probably not the best +// way but it works +__declspec(dllexport) DWORD tempPrintLastError() +{ + // adapted from https://docs.microsoft.com/en-us/windows/win32/debug/retrieving-the-last-error-code + + LPVOID lpMsgBuf; + DWORD code = GetLastError(); + + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + code, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &lpMsgBuf, + 0, NULL ); + + printf("Error %d: %s", code, (char *)lpMsgBuf); + + LocalFree(lpMsgBuf); + + return 0; // returning b/c declaring the function as VOID instead of DWORD caused mysterious issues +} + static BOOL nlP_filexists(LPCTSTR szPath) { DWORD dwAttrib = GetFileAttributes(szPath);