summaryrefslogtreecommitdiff
path: root/core/src/xmake/winos/registry_query.c
blob: 55cc8e9259df826bdcdeb90b1924b9ed65b472f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*!A cross-platform build utility based on Lua
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright (C) 2015-2020, TBOOX Open Source Group.
 *
 * @author      TitanSnow, ruki
 * @file        registry_query.c
 *
 */

/* //////////////////////////////////////////////////////////////////////////////////////
 * trace
 */
#define TB_TRACE_MODULE_NAME                "registry_query"
#define TB_TRACE_MODULE_DEBUG               (0)

/* //////////////////////////////////////////////////////////////////////////////////////
 * includes
 */
#include "prefix.h"

/* //////////////////////////////////////////////////////////////////////////////////////
 * types
 */
// the RegGetValueA func type
typedef BOOL (WINAPI* xm_RegGetValueA_t)(HKEY hkey, LPCSTR lpSubKey, LPCSTR lpValue, DWORD dwFlags, LPDWORD pdwType, PVOID pvData, LPDWORD pcbData);

/* //////////////////////////////////////////////////////////////////////////////////////
 * implementation
 */

/* query registry
 *
 * local value, errors = winos.registry_query("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug;Debugger")
 */
tb_int_t xm_winos_registry_query(lua_State* lua)
{
    // check
    tb_assert_and_check_return_val(lua, 0);

    // get the path
    tb_char_t const* path = luaL_checkstring(lua, 1);
    tb_check_return_val(path, 0);

    // query key-value    
    tb_bool_t   ok = tb_false;
    HKEY        key = NULL;
    HKEY        keynew = NULL;
    tb_char_t   pathbuf[TB_PATH_MAXN];
    tb_char_t*  value = tb_null;
    do
    {
        // copy path
        tb_size_t size = tb_strlcpy(pathbuf, path, sizeof(pathbuf));
        if (size >= sizeof(pathbuf)) 
        {
            lua_pushnil(lua);
            lua_pushfstring(lua, "too long path: %s", path);
            break;
        }

        // parse path key and subkey
        tb_char_t* pathkey      = tb_null;
        tb_char_t* pathsubkey   = tb_null;
        tb_char_t* p            = pathbuf;
        for (; *p && *p != '\\'; p++) ;
        if (*p == '\\')
        {
            *p = '\0';
            pathkey = pathbuf;
            pathsubkey = p + 1;
        }
        if (!pathkey || !pathsubkey)
        {
            lua_pushnil(lua);
            lua_pushfstring(lua, "parse pathkey and pathsubkey failed: %s", path);
            break;
        }

        // parse value name
        tb_char_t* valuename = tb_null;
        for (p = pathbuf + size - 1; p >= pathbuf && *p && *p != ';'; p--) ;
        if (p >= pathbuf && *p == ';')
        {
            valuename = p + 1;
            *p = '\0';
        }

        // trace
        tb_trace_d("key: %s, subkey: %s, name: %s", pathkey, pathsubkey, valuename? valuename : "(default)");

        // get registry key
        if (!tb_strcmp(pathkey, "HKEY_CLASSES_ROOT"))         key = HKEY_CLASSES_ROOT;
        else if (!tb_strcmp(pathkey, "HKEY_CURRENT_CONFIG"))  key = HKEY_CURRENT_CONFIG;
        else if (!tb_strcmp(pathkey, "HKEY_CURRENT_USER"))    key = HKEY_CURRENT_USER;
        else if (!tb_strcmp(pathkey, "HKEY_LOCAL_MACHINE"))   key = HKEY_LOCAL_MACHINE;
        else if (!tb_strcmp(pathkey, "HKEY_USERS"))           key = HKEY_USERS;
        else 
        {
            lua_pushnil(lua);
            lua_pushfstring(lua, "invalid registry path: %s", path);
            break;
        }

        // attempt to load RegGetValueA
        static xm_RegGetValueA_t s_RegGetValueA = tb_null;
        if (!s_RegGetValueA)
        {
            // load the advapi32 module
            tb_dynamic_ref_t module = (tb_dynamic_ref_t)GetModuleHandleA("advapi32.dll");
            if (!module) module = tb_dynamic_init("advapi32.dll");
            if (module) s_RegGetValueA = (xm_RegGetValueA_t)tb_dynamic_func(module, "RegGetValueA");
        }

        // get registry value
        DWORD type = 0;
        if (s_RegGetValueA)
        {
            // get registry value size
            DWORD valuesize = 0;
            if (s_RegGetValueA(key, pathsubkey, valuename, RRF_RT_ANY, 0, tb_null, &valuesize) != ERROR_SUCCESS)
            {
                lua_pushnil(lua);
                lua_pushfstring(lua, "get registry value size failed: %s", path);
                break;
            }

            // make value buffer
            value = (tb_char_t*)tb_malloc0(valuesize + 1);
            tb_assert_and_check_break(value);

            // get value result
            type = 0;
            if (s_RegGetValueA(key, pathsubkey, valuename, RRF_RT_ANY, &type, (PVOID)value, &valuesize) != ERROR_SUCCESS)
            {
                lua_pushnil(lua);
                lua_pushfstring(lua, "get registry value failed: %s", path);
                break;
            }
        }
        else
        {
            // open registry key
            if (RegOpenKeyExA(key, pathsubkey, 0, KEY_QUERY_VALUE, &keynew) != ERROR_SUCCESS && keynew)
            {
                lua_pushnil(lua);
                lua_pushfstring(lua, "open registry key failed: %s", path);
                break;
            }

            // get registry value size
            DWORD valuesize = 0;
            if (RegQueryValueExA(keynew, valuename, tb_null, tb_null, tb_null, &valuesize) != ERROR_SUCCESS)
            {
                lua_pushnil(lua);
                lua_pushfstring(lua, "get registry value size failed: %s", path);
                break;
            }

            // make value buffer
            value = (tb_char_t*)tb_malloc0(valuesize + 1);
            tb_assert_and_check_break(value);

            // get value result
            type = 0;
            if (RegQueryValueExA(keynew, valuename, tb_null, &type, (LPBYTE)value, &valuesize) != ERROR_SUCCESS)
            {
                lua_pushnil(lua);
                lua_pushfstring(lua, "get registry value failed: %s", path);
                break;
            }
        }

        // save result
        switch (type)
        {
        case REG_SZ:
        case REG_EXPAND_SZ:
            lua_pushstring(lua, value);
            ok = tb_true;
            break;
        case REG_DWORD:
            lua_pushfstring(lua, "%d", *((tb_int_t*)value));
            ok = tb_true;
            break;
        case REG_QWORD:
            lua_pushfstring(lua, "%lld", *((tb_int64_t*)value));
            ok = tb_true;
            break;
        default:
            lua_pushnil(lua);
            lua_pushfstring(lua, "unsupported registry value type: %d", type);
            break;
        }

    } while (0);

    // exit registry key
    if (keynew != NULL)
        RegCloseKey(keynew);
    keynew = NULL;

    // exit value
    if (value) tb_free(value);
    value = tb_null;

    // ok?
    return ok? 1 : 2;
}