1 |
/* wptW32API.cpp - Common W32 API functions |
/* wptW32API.cpp - Common W32 API functions |
2 |
* Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz |
* Copyright (C) 2001-2003, 2005-2006, 2009 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
* GNU General Public License for more details. |
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
15 |
*/ |
*/ |
|
|
|
16 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
#include <config.h> |
18 |
#endif |
#endif |
24 |
#include <shellapi.h> |
#include <shellapi.h> |
25 |
#include <shlobj.h> |
#include <shlobj.h> |
26 |
#include <commctrl.h> |
#include <commctrl.h> |
27 |
|
#include <time.h> |
28 |
|
|
29 |
#include "wptNLS.h" |
#include "wptNLS.h" |
30 |
#include "wptW32API.h" |
#include "wptW32API.h" |
31 |
#include "wptErrors.h" |
#include "wptErrors.h" |
32 |
#include "wptVersion.h" |
#include "wptVersion.h" |
33 |
|
#include "wptUTF8.h" |
34 |
#include "wptTypes.h" |
#include "wptTypes.h" |
35 |
|
#include "StringBuffer.h" |
36 |
|
|
37 |
|
|
38 |
extern "C" void _SHFree (void *p); |
extern "C" void _SHFree (void *p); |
39 |
|
|
40 |
|
|
41 |
|
/* Insert a new item into the menu @hm at position @pos. */ |
42 |
|
void |
43 |
|
insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text) |
44 |
|
{ |
45 |
|
MENUITEMINFO mi; |
46 |
|
|
47 |
|
memset (&mi, 0, sizeof mi); |
48 |
|
mi.cbSize = sizeof mi; |
49 |
|
mi.fType = MF_STRING; |
50 |
|
mi.dwTypeData = (char *)text; |
51 |
|
mi.cch = strlen (text); |
52 |
|
mi.wID = m_id; |
53 |
|
mi.fMask = MIIM_ID|MIIM_DATA| MIIM_TYPE; |
54 |
|
InsertMenuItem (hm, pos, FALSE, &mi); |
55 |
|
} |
56 |
|
|
57 |
|
|
58 |
static void |
static void |
59 |
set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text) |
set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text) |
60 |
{ |
{ |
71 |
|
|
72 |
/* Set the text of a menu item @m_uid to @text. */ |
/* Set the text of a menu item @m_uid to @text. */ |
73 |
void |
void |
74 |
set_menu_text (HMENU menu, int m_uid, const char *text) |
set_menu_text (HMENU menu, UINT m_uid, const char *text) |
75 |
{ |
{ |
76 |
set_menu_text_ext (menu, 0, m_uid, text); |
set_menu_text_ext (menu, 0, m_uid, text); |
77 |
} |
} |
79 |
|
|
80 |
/* Set the text of a menu item with the position @pos to @text. */ |
/* Set the text of a menu item with the position @pos to @text. */ |
81 |
void |
void |
82 |
set_menu_text_bypos (HMENU menu, int pos, const char *text) |
set_menu_text_bypos (HMENU menu, UINT pos, const char *text) |
83 |
{ |
{ |
84 |
set_menu_text_ext (menu, 1, pos, text); |
set_menu_text_ext (menu, 1, pos, text); |
85 |
} |
} |
87 |
|
|
88 |
/* Set the state of a menu item @m_uid to @state. */ |
/* Set the state of a menu item @m_uid to @state. */ |
89 |
void |
void |
90 |
set_menu_state (HMENU menu, int m_uid, int state) |
set_menu_state (HMENU menu, UINT m_uid, UINT state) |
91 |
{ |
{ |
92 |
MENUITEMINFO mii; |
MENUITEMINFO mii; |
93 |
|
|
99 |
} |
} |
100 |
|
|
101 |
|
|
102 |
|
/* Retrieve the state of the menu item @m_uid and return it. */ |
103 |
|
UINT |
104 |
|
get_menu_state (HMENU menu, UINT m_uid) |
105 |
|
{ |
106 |
|
MENUITEMINFO mii; |
107 |
|
|
108 |
|
memset (&mii, 0, sizeof (mii)); |
109 |
|
mii.cbSize = sizeof (mii); |
110 |
|
mii.fMask = MIIM_STATE; |
111 |
|
GetMenuItemInfo (menu, m_uid, FALSE, &mii); |
112 |
|
return mii.fState; |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
enum { |
enum { |
117 |
CDLG_FILE_OPEN = 0, |
CDLG_FILE_OPEN = 0, |
118 |
CDLG_FILE_SAVE = 1 |
CDLG_FILE_SAVE = 1 |
122 |
id can be either FILE_OPEN or FILE_SAVE. |
id can be either FILE_OPEN or FILE_SAVE. |
123 |
The return value is the file name or NULL if cancel was chosen. */ |
The return value is the file name or NULL if cancel was chosen. */ |
124 |
const char * |
const char * |
125 |
get_filename_dlg (HWND hwnd, int id, const char * title, |
get_filename_dlg (HWND hwnd, int id, const char *title, |
126 |
const char * filter, const char * name) |
const char *filter, const char *name) |
127 |
{ |
{ |
128 |
static char file[512] = ""; |
static char file[2*MAX_PATH+1] = ""; |
129 |
|
char _filter[512]; |
130 |
|
char workdir[MAX_PATH+1]; |
131 |
|
char *sel_file = NULL; |
132 |
OPENFILENAME open; |
OPENFILENAME open; |
133 |
|
|
134 |
if (name && strlen (name) < (sizeof (file)-1)) |
if (name && strlen (name) < (DIM (file)-1)) |
135 |
strcpy (file, name); |
strcpy (file, name); |
136 |
else |
else |
137 |
memset (file, 0, sizeof (file)); |
memset (file, 0, sizeof (file)); |
138 |
if (!filter) |
if (!filter) { |
139 |
filter = _("All Files (*.*)\0*.*\0\0"); |
strcpy (_filter, _("All Files (*.*)")); |
140 |
/* XXX: problem with gettext because of the 'artificial' |
memcpy (_filter+strlen (_filter)+1, "*.*\0\0", 5); |
141 |
double string termination!. */ |
filter = _filter; |
142 |
|
} |
143 |
|
|
144 |
|
GetCurrentDirectory (MAX_PATH, workdir); |
145 |
memset (&open, 0, sizeof (open)); |
memset (&open, 0, sizeof (open)); |
146 |
open.lStructSize = sizeof (OPENFILENAME); |
open.lStructSize = sizeof (OPENFILENAME); |
147 |
open.hInstance = glob_hinst; |
open.hInstance = glob_hinst; |
149 |
open.lpstrFilter = filter; |
open.lpstrFilter = filter; |
150 |
open.hwndOwner = hwnd; |
open.hwndOwner = hwnd; |
151 |
open.lpstrFile = file; |
open.lpstrFile = file; |
152 |
open.nMaxFile = sizeof (file) - 1; |
open.nMaxFile = DIM (file) - 1; |
153 |
if (id == CDLG_FILE_OPEN) |
if (id == CDLG_FILE_OPEN) |
154 |
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
155 |
else |
else |
156 |
open.Flags = OFN_OVERWRITEPROMPT; |
open.Flags = OFN_OVERWRITEPROMPT; |
157 |
|
|
158 |
if (id == CDLG_FILE_OPEN && GetOpenFileName (&open)) |
if (id == CDLG_FILE_OPEN && GetOpenFileName (&open)) |
159 |
return open.lpstrFile; |
sel_file = open.lpstrFile; |
160 |
else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open)) |
else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open)) |
161 |
return open.lpstrFile; |
sel_file = open.lpstrFile; |
162 |
|
SetCurrentDirectory (workdir); |
163 |
|
|
164 |
return NULL; |
return sel_file; |
165 |
} |
} |
166 |
|
|
167 |
const char* |
const char* |
184 |
const char* |
const char* |
185 |
get_folder_dlg (HWND hwnd, const char *title, const char *name) |
get_folder_dlg (HWND hwnd, const char *title, const char *name) |
186 |
{ |
{ |
187 |
static char folder[MAX_PATH+1] = ""; |
static char folder[2*MAX_PATH+1] = ""; |
188 |
BROWSEINFO bi; |
BROWSEINFO bi; |
189 |
ITEMIDLIST *il; |
ITEMIDLIST *il; |
190 |
|
|
197 |
else |
else |
198 |
memset (folder, 0, sizeof (folder)); |
memset (folder, 0, sizeof (folder)); |
199 |
il = SHBrowseForFolder (&bi); |
il = SHBrowseForFolder (&bi); |
200 |
if (il) { |
if (il != NULL) { |
201 |
SHGetPathFromIDList (il, folder); |
SHGetPathFromIDList (il, folder); |
202 |
_SHFree (il); |
_SHFree (il); |
203 |
return folder; |
return folder; |
259 |
BUG (NULL); |
BUG (NULL); |
260 |
p = (char *) GlobalLock (clipmem); |
p = (char *) GlobalLock (clipmem); |
261 |
if (p == NULL) { |
if (p == NULL) { |
262 |
rc = WPTERR_GENERAL;; |
CloseClipboard (); |
263 |
goto leave; |
GlobalFree (clipmem); |
264 |
|
return WPTERR_GENERAL; |
265 |
} |
} |
266 |
memcpy (p, text, nbytes); |
memcpy (p, text, nbytes); |
267 |
p[nbytes] = '\0'; |
p[nbytes] = '\0'; |
268 |
|
|
|
GlobalUnlock (clipmem); |
|
269 |
SetClipboardData (CF_TEXT, clipmem); |
SetClipboardData (CF_TEXT, clipmem); |
270 |
|
GlobalUnlock (clipmem); |
|
leave: |
|
271 |
CloseClipboard (); |
CloseClipboard (); |
272 |
|
GlobalFree (clipmem); |
273 |
|
|
274 |
return rc; |
return rc; |
275 |
} /* set_clip_text */ |
} |
276 |
|
|
277 |
|
|
278 |
/* Append or prepend some text to the clipboard contents. |
/* Append or prepend some text to the clipboard contents. |
280 |
int |
int |
281 |
set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer) |
set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer) |
282 |
{ |
{ |
283 |
|
const char *fmt; |
284 |
char *p, *new_text; |
char *p, *new_text; |
285 |
|
|
286 |
p = get_clip_text (hwnd); |
p = get_clip_text (hwnd); |
287 |
if (!p) |
if (!p) |
288 |
return WPTERR_CLIP_GET; |
return WPTERR_CLIP_GET; |
289 |
new_text = new char [strlen (p)+strlen (text)+8]; |
if (as_footer == 0) |
290 |
|
fmt = "%s\r\n%s\r\n\r\n"; |
291 |
|
else |
292 |
|
fmt = "%s\n%s\n\n"; |
293 |
|
new_text = new char [strlen (p)+strlen (text)+strlen (fmt)+2]; |
294 |
if (!new_text) |
if (!new_text) |
295 |
BUG (0); |
BUG (0); |
296 |
if (as_footer == 0) |
if (as_footer == 0) |
297 |
sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p); |
sprintf (new_text, fmt, text, p); |
298 |
else |
else |
299 |
sprintf (new_text, "%s\n%s\n\n", p, text); |
sprintf (new_text, fmt, p, text); |
300 |
set_clip_text (hwnd, new_text, strlen (new_text)+1); |
set_clip_text (hwnd, new_text, strlen (new_text)+1); |
301 |
free_if_alloc (p); |
free_if_alloc (p); |
302 |
free_if_alloc (new_text); |
free_if_alloc (new_text); |
313 |
char *p; |
char *p; |
314 |
size_t size = 0; |
size_t size = 0; |
315 |
|
|
316 |
if( path && *path ) |
if (path && *path) |
317 |
size += strlen( path ); |
size += strlen (path); |
318 |
if( file && *file ) |
if (file && *file) |
319 |
size += strlen( file ); |
size += strlen (file); |
320 |
if( ext && *ext ) |
if (ext && *ext) |
321 |
size += strlen( ext ); |
size += strlen (ext); |
322 |
p = new char[size + 4]; |
p = new char[size + 4]; |
323 |
memset( p, 0, size ); |
if (!p) |
324 |
if( path ) { |
BUG (0); |
325 |
strcat( p, path ); |
memset (p, 0, size); |
326 |
if( path[strlen( path ) -1] != '\\' ) |
if (path) { |
327 |
strcat( p, "\\" ); |
strcat (p, path); |
328 |
} |
if (path[strlen (path) -1] != '\\') |
329 |
if( file ) |
strcat (p, "\\"); |
330 |
strcat( p, file ); |
} |
331 |
if( ext ) { |
if (file != NULL) |
332 |
strcat( p, "." ); |
strcat (p, file); |
333 |
strcat( p, ext ); |
if (ext != NULL) { |
334 |
|
strcat (p, "."); |
335 |
|
strcat (p, ext); |
336 |
} |
} |
337 |
return p; |
return p; |
338 |
} |
} |
339 |
|
|
340 |
|
|
341 |
|
/* Generates a file name from a special dirctory. */ |
342 |
|
char* |
343 |
|
make_special_filename (int folder, const char *file, const char *ext) |
344 |
|
{ |
345 |
|
BOOL ec; |
346 |
|
char path[MAX_PATH+1], *p; |
347 |
|
size_t n=0; |
348 |
|
|
349 |
|
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
350 |
|
ec = SHGetSpecialFolderPath (HWND_DESKTOP, path, folder, TRUE); |
351 |
|
if (ec != 1) { |
352 |
|
log_debug ("SHGetSpecialFolderPath() failed\r\n", (int)GetLastError ()); |
353 |
|
return NULL; |
354 |
|
} |
355 |
|
|
356 |
|
n = strlen (path)+1; |
357 |
|
if (file) |
358 |
|
n += strlen (file)+1; |
359 |
|
if (ext) |
360 |
|
n += strlen (ext)+1; |
361 |
|
p = new char[n+2]; |
362 |
|
if (!p) |
363 |
|
BUG (0); |
364 |
|
memset (p, 0, n+2); |
365 |
|
strcpy (p, path); |
366 |
|
if (file) { |
367 |
|
strcat (p, "\\"); |
368 |
|
strcat (p, file); |
369 |
|
} |
370 |
|
if (ext) |
371 |
|
strcat (p, ext); |
372 |
|
return p; |
373 |
|
} |
374 |
|
|
375 |
|
|
376 |
/* return 0 if the file @fname exists, otherwise >0. */ |
/* return 0 if the file @fname exists, otherwise >0. */ |
377 |
int |
int |
378 |
file_exist_check (const char *fname) |
file_exist_check (const char *fname) |
418 |
} |
} |
419 |
|
|
420 |
|
|
|
int |
|
|
init_file_lock( LOCK *ctx, const char *file ) |
|
|
{ |
|
|
|
|
|
ctx->size = get_file_size( file ); |
|
|
ctx->file = m_strdup( file ); |
|
|
ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL, |
|
|
OPEN_ALWAYS, 0, NULL ); |
|
|
if( ctx->fh == INVALID_HANDLE_VALUE ) |
|
|
return WPTERR_GENERAL; |
|
|
if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) { |
|
|
CloseHandle( ctx->fh ); |
|
|
ctx->fh = INVALID_HANDLE_VALUE; |
|
|
ctx->size = 0; |
|
|
free( ctx->file ); |
|
|
return WPTERR_GENERAL; |
|
|
} |
|
|
return 0; |
|
|
} /* init_file_lock */ |
|
|
|
|
|
|
|
|
void |
|
|
release_file_lock( LOCK *ctx ) |
|
|
{ |
|
|
free_if_alloc( ctx->file ); |
|
|
ctx->file = NULL; |
|
|
ctx->size = 0; |
|
|
CloseHandle( ctx->fh ); |
|
|
} /* release_file_lock */ |
|
|
|
|
|
|
|
421 |
/* Start a dialog with the exception that before it is checked that the |
/* Start a dialog with the exception that before it is checked that the |
422 |
dialog is not already openened. */ |
dialog is not already openened. */ |
423 |
int |
int |
442 |
} |
} |
443 |
|
|
444 |
|
|
|
/* Safe strdup version (C++ version). */ |
|
|
char* |
|
|
m_strdup (const char *str) |
|
|
{ |
|
|
char *p = new char[strlen (str) + 1]; |
|
|
if (!p) |
|
|
BUG (NULL); |
|
|
strcpy (p, str); |
|
|
return p; |
|
|
} |
|
|
|
|
|
|
|
445 |
/* Center the hwndChild relative to parent. |
/* Center the hwndChild relative to parent. |
446 |
The style param allows to specificy additional styles (like topmost). */ |
The style param allows to specificy additional styles (like topmost). */ |
447 |
void |
void |
487 |
|
|
488 |
/* Center the given hwndChild window with no special style. */ |
/* Center the given hwndChild window with no special style. */ |
489 |
void |
void |
490 |
center_window (HWND hwndChild, HWND hwndParent) |
center_window (HWND child, HWND parent) |
491 |
{ |
{ |
492 |
center_window2 (hwndChild, hwndParent, NULL); |
center_window2 (child, parent, NULL); |
493 |
|
} |
494 |
|
|
495 |
|
|
496 |
|
|
497 |
|
|
498 |
|
/* Return date in a format which complies with the |
499 |
|
locale user settings. */ |
500 |
|
const char* |
501 |
|
get_locale_date (long tm_t, char *buf, DWORD buflen) |
502 |
|
{ |
503 |
|
SYSTEMTIME st; |
504 |
|
struct tm *ptm; |
505 |
|
|
506 |
|
ptm = localtime (&tm_t); |
507 |
|
memset (&st, 0, sizeof (st)); |
508 |
|
st.wYear = (WORD)ptm->tm_year+1900; |
509 |
|
st.wMonth = (WORD)ptm->tm_mon+1; |
510 |
|
st.wDay = (WORD)ptm->tm_mday; |
511 |
|
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
512 |
|
NULL, buf, buflen)) |
513 |
|
return NULL; |
514 |
|
return buf; |
515 |
|
} |
516 |
|
|
517 |
|
/* Return the time in a format which complies with the locale user |
518 |
|
settings. */ |
519 |
|
const char* |
520 |
|
get_locale_time (long tm_t, char *buf, DWORD buflen) |
521 |
|
{ |
522 |
|
SYSTEMTIME st; |
523 |
|
struct tm *ptm; |
524 |
|
|
525 |
|
ptm = localtime (&tm_t); |
526 |
|
memset (&st, 0, sizeof (st)); |
527 |
|
st.wMinute = (WORD)ptm->tm_min; |
528 |
|
st.wHour = (WORD)ptm->tm_hour; |
529 |
|
st.wSecond = (WORD)ptm->tm_sec; |
530 |
|
|
531 |
|
if (!GetTimeFormat (LOCALE_USER_DEFAULT, TIME_FORCE24HOURFORMAT, &st, |
532 |
|
NULL, buf, buflen)) |
533 |
|
return NULL; |
534 |
|
return buf; |
535 |
|
} |
536 |
|
|
537 |
|
|
538 |
|
/* Return a combination of date+time which complies to the |
539 |
|
local user settings. */ |
540 |
|
const char* |
541 |
|
get_locale_timedate (long tm_t, char *buf, DWORD buflen) |
542 |
|
{ |
543 |
|
DWORD nleft = buflen; |
544 |
|
|
545 |
|
if (!get_locale_date (tm_t, buf, buflen)) |
546 |
|
return NULL; |
547 |
|
nleft -= strlen (buf)+1; |
548 |
|
if (nleft < 1) |
549 |
|
return NULL; |
550 |
|
strcat (buf, " "); |
551 |
|
nleft--; |
552 |
|
if (!get_locale_time (tm_t, buf+strlen (buf), nleft)) |
553 |
|
return NULL; |
554 |
|
return buf; |
555 |
} |
} |
556 |
|
|
557 |
|
|
558 |
/* Retrieve the product verion of the given file @fname. |
/* Generate a temporary file name by using the users |
559 |
Format: MAJOR.MINOR.PATCH1.PATCH2 |
temp path and optionally a name @name provided by the caller. |
560 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
561 |
int |
int |
562 |
get_file_version (const char *fname, WORD *major, WORD *minor, |
get_temp_name (char *buf, DWORD buflen, const char *name) |
|
WORD *patch1, WORD *patch2) |
|
563 |
{ |
{ |
564 |
VS_FIXEDFILEINFO *inf = {0}; |
char tmp[32]; |
|
char file[MAX_PATH+1] = {0}; |
|
|
LPVOID buf, data; |
|
|
DWORD arg; |
|
|
DWORD size; |
|
|
UINT qlen; |
|
|
|
|
|
strncpy (file, fname, MAX_PATH); |
|
|
size = GetFileVersionInfoSize (file, &arg); |
|
|
if (!size) |
|
|
return -1; |
|
|
buf = (LPVOID)new CHAR[size]; |
|
|
if (!buf) |
|
|
BUG (NULL); |
|
|
GetFileVersionInfo (file, 0, size, buf); |
|
565 |
|
|
566 |
qlen=0; |
|
567 |
VerQueryValue (buf, "\\", &data, &qlen); |
if (!name) { |
568 |
if (!qlen) { |
_snprintf (tmp, DIM (tmp)-1, "%08lX", GetTickCount ()); |
569 |
delete [] (char*)buf; |
name = tmp; |
|
return -1; |
|
570 |
} |
} |
|
inf = (VS_FIXEDFILEINFO*)data; |
|
571 |
|
|
572 |
if (major) |
if (!GetTempPath (buflen - strlen (name) -2, buf)) { |
573 |
*major = HIWORD (inf->dwProductVersionMS); |
log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ()); |
574 |
if (minor) |
return -1; |
575 |
*minor = LOWORD (inf->dwProductVersionMS); |
} |
|
if (patch1) |
|
|
*patch1 = HIWORD (inf->dwProductVersionLS); |
|
|
if (patch2) |
|
|
*patch2 = LOWORD (inf->dwProductVersionLS); |
|
576 |
|
|
577 |
delete [] (char*)buf; |
strcat (buf, name); |
578 |
return 0; |
return 0; |
579 |
} |
} |
580 |
|
|
581 |
|
|
582 |
void |
void |
583 |
set_active_window( HWND dlg) |
ListBox_AddString_utf8 (HWND lb, const char *txt) |
584 |
{ |
{ |
585 |
activ_hwnd = dlg; |
char *utf8_txt; |
|
} /* set_active_window */ |
|
586 |
|
|
587 |
void |
utf8_txt = utf8_to_native (txt); |
588 |
reset_active_window( void ) |
SendMessage (lb, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt)); |
589 |
{ |
safe_free (utf8_txt); |
590 |
activ_hwnd = NULL; |
} |
|
} /* reset_active_window */ |
|
591 |
|
|
592 |
|
|
593 |
static DWORD CALLBACK |
void |
594 |
reminder_thread (void *ctx) |
ComboBox_AddString_utf8 (HWND cb, const char *txt) |
595 |
{ |
{ |
596 |
reminder_ctx_s *c = (reminder_ctx_s *)ctx; |
char *utf8_txt; |
597 |
|
|
598 |
Sleep( c->msecs ); |
utf8_txt = utf8_to_native (txt); |
599 |
SetForegroundWindow( activ_hwnd ); |
SendMessage ((cb), CB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt)); |
600 |
|
safe_free (utf8_txt); |
601 |
|
} |
602 |
|
|
|
return 0; |
|
|
} /* reminder_thread */ |
|
603 |
|
|
604 |
|
/* GetDlgItemText replacement with UTF8 support. */ |
605 |
|
int |
606 |
|
GetDlgItemText_utf8 (HWND dlg, int id, char **r_txt) |
607 |
|
{ |
608 |
|
int len = GetWindowTextLength (GetDlgItem (dlg, id)); |
609 |
|
char *txt; |
610 |
|
|
611 |
|
*r_txt = NULL; |
612 |
|
if (len < 1) |
613 |
|
return 0; |
614 |
|
txt = new char[len+2]; |
615 |
|
if (!txt) |
616 |
|
BUG (NULL); |
617 |
|
GetDlgItemText (dlg, id, txt, len+1); |
618 |
|
*r_txt = native_to_utf8 (txt); |
619 |
|
free_if_alloc (txt); |
620 |
|
return len; |
621 |
|
} |
622 |
|
|
623 |
HANDLE |
int |
624 |
window_reminder( struct reminder_ctx_s *ctx ) |
html_help_show (int map_id) |
625 |
{ |
{ |
626 |
DWORD tid = 0; |
STARTUPINFO si; |
627 |
|
PROCESS_INFORMATION pi; |
628 |
return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid ); |
SECURITY_ATTRIBUTES sec_attr; |
629 |
} /* window_reminder */ |
char path[2*MAX_PATH+1]; |
630 |
|
|
631 |
|
memset (&sec_attr, 0, sizeof (sec_attr)); |
632 |
|
memset (&si, 0, sizeof (si)); |
633 |
|
si.cb = sizeof (STARTUPINFO); |
634 |
|
memset (&pi, 0, sizeof (pi)); |
635 |
|
//hh.exe -mapid 40 "winpt.$lang.chm" |
636 |
|
|
637 |
|
_snprintf (path, 2*MAX_PATH, "hh.exe -mapid %d winpt.%s.chm", |
638 |
|
map_id, gettext_get_langid ()); |
639 |
|
CreateProcess (NULL, path, &sec_attr, &sec_attr, |
640 |
|
FALSE, 0, NULL, NULL, &si, &pi); |
641 |
|
CloseHandle (pi.hProcess); |
642 |
|
CloseHandle (pi.hThread); |
643 |
|
return 0; |
644 |
|
} |
645 |
|
|
646 |
|
|
647 |
|
/* Return TRUE if the current user has admin privileges. */ |
648 |
|
BOOL |
649 |
|
user_is_admin (void) |
650 |
|
{ |
651 |
|
SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY; |
652 |
|
HANDLE hd; |
653 |
|
TOKEN_GROUPS *ptg = NULL; |
654 |
|
DWORD ngtoken; |
655 |
|
DWORD i; |
656 |
|
BOOL admin = FALSE; |
657 |
|
PSID psid = 0; |
658 |
|
|
659 |
|
if (!OpenThreadToken (GetCurrentThread (), TOKEN_QUERY, FALSE, &hd) && |
660 |
|
!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &hd)) |
661 |
|
return FALSE; |
662 |
|
|
663 |
|
if (!GetTokenInformation (hd, TokenGroups, NULL, 0, &ngtoken) && |
664 |
|
GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
665 |
|
ptg = (TOKEN_GROUPS*)GlobalAlloc (GPTR, ngtoken); |
666 |
|
if (!ptg) |
667 |
|
return FALSE; |
668 |
|
|
669 |
|
if (!GetTokenInformation (hd, TokenGroups, ptg, ngtoken, &ngtoken)) { |
670 |
|
GlobalFree (ptg); |
671 |
|
return FALSE; |
672 |
|
} |
673 |
|
AllocateAndInitializeSid (&SystemSidAuthority, |
674 |
|
2, SECURITY_BUILTIN_DOMAIN_RID, |
675 |
|
DOMAIN_ALIAS_RID_ADMINS, |
676 |
|
0, 0, 0, 0, 0, 0, |
677 |
|
&psid); |
678 |
|
for (i = 0; i < ptg->GroupCount; i++) { |
679 |
|
if (EqualSid (ptg->Groups[i].Sid, psid)) { |
680 |
|
admin = TRUE; |
681 |
|
break; |
682 |
|
} |
683 |
|
} |
684 |
|
FreeSid (psid); |
685 |
|
GlobalFree (ptg); |
686 |
|
} |
687 |
|
|
688 |
|
CloseHandle (hd); |
689 |
|
return admin; |
690 |
|
} |