Two extra Windows functions
- Posted by Jonas Temple <jktemple at yhti.net> Nov 22, 2002
- 555 views
All, I decided to put these two extra functions that I use up for any interested parties. They are: sequence user = GetUserName() gets the user id entered when the user logged on to Windows, if any. sequence {atom error, sequence message} = getErrorInfo() Gets the last error code and message logged by Windows. You could, for example, use this (as I did) to get more specific information about why you shellExecuteEx() function call didn't work. Derek, if you're interested in this it's compatible with win32lib V57.9. Jonas include win32lib.ew without warning constant FORMAT_MESSAGE_ALLOCATE_BUFFER = #00000100, FORMAT_MESSAGE_IGNORE_INSERTS = #00000200, FORMAT_MESSAGE_FROM_STRING = #00000400, FORMAT_MESSAGE_FROM_HMODULE = #00000800, FORMAT_MESSAGE_FROM_SYSTEM = #00001000, FORMAT_MESSAGE_ARGUMENT_ARRAY = #00002000, FORMAT_MESSAGE_MAX_WIDTH_MASK = #000000FF constant FormatMessage = registerw32Function(kernel32, "FormatMessageA", {C_UINT, C_POINTER, C_UINT, C_UINT, C_POINTER, C_UINT, C_POINTER}, C_UINT) global atom advapi32 advapi32 = open_dll("advapi32.dll") global constant xGetUserName = define_c_func(advapi32, "GetUserNameA", {C_POINTER, C_POINTER}, C_INT) global function GetUserName() sequence s atom namelength, buffer, ok, error s = {} namelength = allocate(4) poke4(namelength, 30) buffer = allocate(namelength + 1) if xGetUserName != -1 then ok = c_func(xGetUserName, {buffer, namelength}) if ok then s = peek_string(buffer) end if end if free(buffer) free(namelength) return s end function -- Get the last error code and message global function getErrorInfo() atom msg_buffer, rtn_code, msg_len sequence msg msg_buffer = allocate(1024) rtn_code = w32Func(GetLastError, {}) msg_len = w32Func(FormatMessage, {or_all({FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS}), NULL, rtn_code, 0, msg_buffer, 1024, NULL}) if msg_len then msg = peek({msg_buffer, msg_len}) else msg = "" end if free(msg_buffer) return{rtn_code, msg} end function