2
* libetm-0.4 / win32_specific.c - Copyright (C) Emmanuel Thomas-Maurin 2008-2011
3
* <manutm007@gmail.com>
5
* - win32 specific functions -
7
* This program is free software: you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation, either version 3 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
32
#define APP_WIN32REG_KEYPATH "app_win32_registry_keypath" /* or whatever */
34
/* return NULL if error */
35
const char *get_appdata_dir()
37
static TCHAR appdata_dir[MAX_PATH];
42
if (SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, appdata_dir) != S_OK)
46
return (const char *)appdata_dir;
51
/* return NULL if error */
52
const char *get_progfiles_dir()
54
static TCHAR progfiles_dir[MAX_PATH];
59
if (SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, 0, progfiles_dir) != S_OK)
63
return (const char *)progfiles_dir;
68
/* key_value must be able to store 255 chars */
69
int get_key_value_from_win32registry(const char *key_name, char *key_value)
71
char news_win32regkey_full[128];
73
DWORD type = REG_SZ, buf_size = 256;
76
str_n_cpy(news_win32regkey_full, APP_WIN32REG_KEYPATH, 64);
77
str_n_cat(news_win32regkey_full, key_name, 63);
79
if ((result = RegOpenKeyEx(HKEY_CURRENT_USER, news_win32regkey_full,
80
0L, KEY_QUERY_VALUE, &hkey)) == ERROR_SUCCESS) {
81
if (RegQueryValueEx(hkey, NULL, NULL, &type, (unsigned char*)key_value,
82
&buf_size) == ERROR_SUCCESS) {
87
return WIN32REGKEY_NOT_FOUND;
90
if (result == ERROR_FILE_NOT_FOUND)
91
return WIN32REGKEY_NOT_FOUND;
93
return WIN32REGKEY_OTHER_ERROR;
97
int save_key_value_into_win32registry(const char *key_name, const char *key_value)
99
char news_win32regkey_full[128];
103
str_n_cpy(news_win32regkey_full, APP_WIN32REG_KEYPATH, 64);
104
str_n_cat(news_win32regkey_full, key_name, 63);
106
if (RegCreateKeyEx(HKEY_CURRENT_USER, news_win32regkey_full, 0L,
107
NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hkey, &disp)
109
if (RegSetValueEx(hkey, NULL, 0L, REG_SZ, (unsigned char *)TEXT(key_value),
110
256) == ERROR_SUCCESS) {
115
return WIN32REGKEY_SAVE_ERROR;
118
return WIN32REGKEY_CREATE_ERROR;
121
/* return NULL if error */
122
const char *get_default_browser_from_win32registry()
124
static char browser_cmd[512];
126
DWORD type = REG_SZ, buf_size = 512;
129
if ((result = RegOpenKeyEx(HKEY_CLASSES_ROOT, "http\\shell\\open\\command",
130
0L, KEY_QUERY_VALUE, &hkey)) == ERROR_SUCCESS) {
131
if (RegQueryValueEx(hkey, NULL, NULL, &type, (unsigned char*)browser_cmd,
132
&buf_size) == ERROR_SUCCESS) {
134
return (const char *)browser_cmd;
143
/* return -1 if error */
144
int get_win32_taskbar_height()
150
if ((hwnd = FindWindow("Shell_traywnd", "")) != NULL) {
151
if (GetWindowRect(hwnd, lpRect))
152
return (int) (lpRect->bottom - lpRect->top);
157
/* find up to 15 mac addresses for this computer
158
* return NULL if error */
159
/*const char **find_mac_addresses()
161
IP_ADAPTER_INFO *adapter_info;
162
ULONG buf_len = sizeof(adapter_info);
163
PIP_ADAPTER_INFO p_adapter_info;
164
static char macaddr[16][256], tmp[3];
165
static char *p_macaddr[16];
166
unsigned int i, j = 0;
168
adapter_info = (IP_ADAPTER_INFO *)malloc2(sizeof(IP_ADAPTER_INFO));
169
buf_len = sizeof(IP_ADAPTER_INFO);
170
// initial call is supposed to fail
171
if (GetAdaptersInfo(adapter_info, &buf_len) != ERROR_SUCCESS) {
173
adapter_info = (IP_ADAPTER_INFO *)malloc2(buf_len);
176
if (GetAdaptersInfo(adapter_info, &buf_len) != ERROR_SUCCESS) {
180
p_adapter_info = (PIP_ADAPTER_INFO)adapter_info;
181
while (p_adapter_info && j < 15) {
182
macaddr[j][0] = '\0';
183
for (i = 0; i < 127 && i < p_adapter_info->AddressLength; i++) {
184
snprintf(tmp, 3, "%02X", p_adapter_info->Address[i]);
185
str_n_cat(macaddr[j], tmp, 2);
187
p_macaddr[j] = macaddr[j];
189
p_adapter_info = p_adapter_info->Next;
194
return (const char **)p_macaddr;