1 |
/* wptW32API.cpp - Common W32 API functions |
/* wptW32API.cpp - Common W32 API functions |
2 |
* Copyright (C) 2001, 2002, 2003, 2005, 2006 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 |
32 |
#include "wptVersion.h" |
#include "wptVersion.h" |
33 |
#include "wptUTF8.h" |
#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); |
126 |
const char *filter, const char *name) |
const char *filter, const char *name) |
127 |
{ |
{ |
128 |
static char file[2*MAX_PATH+1] = ""; |
static char file[2*MAX_PATH+1] = ""; |
129 |
|
char _filter[512]; |
130 |
char workdir[MAX_PATH+1]; |
char workdir[MAX_PATH+1]; |
131 |
char *sel_file = NULL; |
char *sel_file = NULL; |
132 |
OPENFILENAME open; |
OPENFILENAME open; |
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 |
|
memcpy (_filter+strlen (_filter)+1, "*.*\0\0", 5); |
141 |
|
filter = _filter; |
142 |
|
} |
143 |
|
|
144 |
GetCurrentDirectory (MAX_PATH, workdir); |
GetCurrentDirectory (MAX_PATH, workdir); |
145 |
memset (&open, 0, sizeof (open)); |
memset (&open, 0, sizeof (open)); |
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 |
|
|
338 |
} |
} |
339 |
|
|
340 |
|
|
341 |
/* Generate a file name from a special dirctory. */ |
/* Generates a file name from a special dirctory. */ |
342 |
char* |
char* |
343 |
make_special_filename (int folder, const char *file, const char *ext) |
make_special_filename (int folder, const char *file, const char *ext) |
344 |
{ |
{ |
345 |
BOOL ec; |
BOOL ec; |
346 |
char path[MAX_PATH], *p; |
char path[MAX_PATH+1], *p; |
347 |
size_t n=0; |
size_t n=0; |
348 |
|
|
349 |
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
493 |
} |
} |
494 |
|
|
495 |
|
|
|
/* Retrieve the product verion of the given file @fname. |
|
|
Format: MAJOR.MINOR.PATCH1.PATCH2 |
|
|
Return value: 0 on success. */ |
|
|
int |
|
|
get_file_version (const char *fname, |
|
|
WORD *major, WORD *minor, WORD *patch1, WORD *patch2) |
|
|
{ |
|
|
VS_FIXEDFILEINFO *inf; |
|
|
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); |
|
|
|
|
|
qlen=0; |
|
|
VerQueryValue (buf, "\\", &data, &qlen); |
|
|
if (!qlen) { |
|
|
delete [](char*)buf; |
|
|
return -1; |
|
|
} |
|
|
inf = (VS_FIXEDFILEINFO*)data; |
|
|
|
|
|
if (major) |
|
|
*major = HIWORD (inf->dwProductVersionMS); |
|
|
if (minor) |
|
|
*minor = LOWORD (inf->dwProductVersionMS); |
|
|
if (patch1) |
|
|
*patch1 = HIWORD (inf->dwProductVersionLS); |
|
|
if (patch2) |
|
|
*patch2 = LOWORD (inf->dwProductVersionLS); |
|
|
|
|
|
delete [](char*)buf; |
|
|
return 0; |
|
|
} |
|
496 |
|
|
497 |
|
|
498 |
/* Return date in a format which complies with the |
/* Return date in a format which complies with the |
499 |
system locale settings. */ |
locale user settings. */ |
500 |
const char* |
const char* |
501 |
get_locale_date (long tm_t, char *buf, DWORD buflen) |
get_locale_date (long tm_t, char *buf, DWORD buflen) |
502 |
{ |
{ |
503 |
SYSTEMTIME st; |
SYSTEMTIME st; |
504 |
struct tm *ptm; |
struct tm *ptm; |
505 |
|
|
506 |
ptm = localtime (&tm_t); |
ptm = localtime (&tm_t); |
507 |
st.wYear = (WORD)ptm->tm_year; |
memset (&st, 0, sizeof (st)); |
508 |
st.wMonth = (WORD)ptm->tm_mon; |
st.wYear = (WORD)ptm->tm_year+1900; |
509 |
|
st.wMonth = (WORD)ptm->tm_mon+1; |
510 |
st.wDay = (WORD)ptm->tm_mday; |
st.wDay = (WORD)ptm->tm_mday; |
|
st.wYear += 1900; |
|
|
st.wMonth += 1; |
|
511 |
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
512 |
NULL, buf, buflen)) |
NULL, buf, buflen)) |
513 |
return NULL; |
return NULL; |
514 |
return buf; |
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 |
/* Generate a temporary file name by using the users |
/* Generate a temporary file name by using the users |
559 |
temp path and optionally a name @name provided by the caller. |
temp path and optionally a name @name provided by the caller. |
569 |
name = tmp; |
name = tmp; |
570 |
} |
} |
571 |
|
|
|
/* In the mobile mode we need to use the current directory |
|
|
as the temp path because no files should be stored outside |
|
|
the USB disk drive. */ |
|
|
#ifdef WINPT_MOBILE |
|
|
memset (buf, 0, buflen); |
|
|
strncpy (buf, name, buflen-1); |
|
|
return 0; |
|
|
#endif |
|
|
|
|
572 |
if (!GetTempPath (buflen - strlen (name) -2, buf)) { |
if (!GetTempPath (buflen - strlen (name) -2, buf)) { |
573 |
log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ()); |
log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ()); |
574 |
return -1; |
return -1; |
593 |
void |
void |
594 |
ComboBox_AddString_utf8 (HWND cb, const char *txt) |
ComboBox_AddString_utf8 (HWND cb, const char *txt) |
595 |
{ |
{ |
|
|
|
596 |
char *utf8_txt; |
char *utf8_txt; |
597 |
|
|
598 |
utf8_txt = utf8_to_native (txt); |
utf8_txt = utf8_to_native (txt); |
620 |
return len; |
return len; |
621 |
} |
} |
622 |
|
|
623 |
|
int |
624 |
|
html_help_show (int map_id) |
625 |
|
{ |
626 |
|
STARTUPINFO si; |
627 |
|
PROCESS_INFORMATION pi; |
628 |
|
SECURITY_ATTRIBUTES sec_attr; |
629 |
|
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. */ |
/* Return TRUE if the current user has admin privileges. */ |
648 |
BOOL |
BOOL |
649 |
user_is_admin (void) |
user_is_admin (void) |
656 |
BOOL admin = FALSE; |
BOOL admin = FALSE; |
657 |
PSID psid = 0; |
PSID psid = 0; |
658 |
|
|
|
if (GetVersion () & 0x80000000) /* Win9X */ |
|
|
return TRUE; |
|
|
|
|
659 |
if (!OpenThreadToken (GetCurrentThread (), TOKEN_QUERY, FALSE, &hd) && |
if (!OpenThreadToken (GetCurrentThread (), TOKEN_QUERY, FALSE, &hd) && |
660 |
!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &hd)) |
!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &hd)) |
661 |
return FALSE; |
return FALSE; |
666 |
if (!ptg) |
if (!ptg) |
667 |
return FALSE; |
return FALSE; |
668 |
|
|
669 |
if (!GetTokenInformation (hd, TokenGroups, |
if (!GetTokenInformation (hd, TokenGroups, ptg, ngtoken, &ngtoken)) { |
|
ptg, ngtoken, &ngtoken)) { |
|
670 |
GlobalFree (ptg); |
GlobalFree (ptg); |
671 |
return FALSE; |
return FALSE; |
672 |
} |
} |