2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
12 * Windows support functions for Lua
22 #if (LUA_VERSION_NUM == 502)
23 #define luaL_register(L, N, R) luaL_setfuncs(L, R, 0)
26 static int Lget_nameservers(lua_State *L) {
27 char stack_buffer[1024]; // stack allocated buffer
28 IP4_ARRAY* ips = (IP4_ARRAY*) stack_buffer;
29 DWORD len = sizeof(stack_buffer);
32 status = DnsQueryConfig(DnsConfigDnsServerList, FALSE, NULL, NULL, ips, &len);
35 lua_createtable(L, ips->AddrCount, 0);
36 for (i = 0; i < ips->AddrCount; i++) {
37 DWORD ip = ips->AddrArray[i];
39 sprintf_s(ip_str, sizeof(ip_str), "%d.%d.%d.%d", (ip >> 0) & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255);
40 lua_pushstring(L, ip_str);
41 lua_rawseti(L, -2, i+1);
46 lua_pushfstring(L, "DnsQueryConfig returned %d", status);
51 static int lerror(lua_State *L, char* string) {
53 lua_pushfstring(L, "%s: %d", string, GetLastError());
57 static int Lget_consolecolor(lua_State *L) {
58 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
59 WORD color; DWORD read_len;
61 CONSOLE_SCREEN_BUFFER_INFO info;
63 if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle");
64 if (!GetConsoleScreenBufferInfo(console, &info)) return lerror(L, "GetConsoleScreenBufferInfo");
65 if (!ReadConsoleOutputAttribute(console, &color, 1, info.dwCursorPosition, &read_len)) return lerror(L, "ReadConsoleOutputAttribute");
67 lua_pushnumber(L, color);
70 static int Lset_consolecolor(lua_State *L) {
71 int color = luaL_checkint(L, 1);
72 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
73 if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle");
74 if (!SetConsoleTextAttribute(console, color)) return lerror(L, "SetConsoleTextAttribute");
75 lua_pushboolean(L, 1);
79 static const luaL_Reg Reg[] =
81 { "get_nameservers", Lget_nameservers },
82 { "get_consolecolor", Lget_consolecolor },
83 { "set_consolecolor", Lset_consolecolor },
87 LUALIB_API int luaopen_util_windows(lua_State *L) {
89 luaL_register(L, NULL, Reg);
90 lua_pushliteral(L, "-3.14");
91 lua_setfield(L, -2, "version");