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, 2002, 2003, 2005, 2006 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 |
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 |
|
|
36 |
|
|
37 |
extern "C" void _SHFree (void *p); |
extern "C" void _SHFree (void *p); |
38 |
|
|
39 |
|
|
|
|
|
40 |
/* Insert a new item into the menu @hm at position @pos. */ |
/* Insert a new item into the menu @hm at position @pos. */ |
41 |
void |
void |
42 |
insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text) |
insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text) |
124 |
get_filename_dlg (HWND hwnd, int id, const char *title, |
get_filename_dlg (HWND hwnd, int id, const char *title, |
125 |
const char *filter, const char *name) |
const char *filter, const char *name) |
126 |
{ |
{ |
127 |
static char file[512] = ""; |
static char file[2*MAX_PATH+1] = ""; |
128 |
|
char workdir[MAX_PATH+1]; |
129 |
|
char *sel_file = NULL; |
130 |
OPENFILENAME open; |
OPENFILENAME open; |
131 |
|
|
132 |
if (name && strlen (name) < (sizeof (file)-1)) |
if (name && strlen (name) < (DIM (file)-1)) |
133 |
strcpy (file, name); |
strcpy (file, name); |
134 |
else |
else |
135 |
memset (file, 0, sizeof (file)); |
memset (file, 0, sizeof (file)); |
136 |
if (!filter) |
if (!filter) |
137 |
filter = "All Files (*.*)\0*.*\0\0"; |
filter = "All Files (*.*)\0*.*\0\0"; |
138 |
/* XXX: problem with gettext because of the 'artificial' |
|
139 |
double string termination!. */ |
GetCurrentDirectory (MAX_PATH, workdir); |
140 |
memset (&open, 0, sizeof (open)); |
memset (&open, 0, sizeof (open)); |
141 |
open.lStructSize = sizeof (OPENFILENAME); |
open.lStructSize = sizeof (OPENFILENAME); |
142 |
open.hInstance = glob_hinst; |
open.hInstance = glob_hinst; |
144 |
open.lpstrFilter = filter; |
open.lpstrFilter = filter; |
145 |
open.hwndOwner = hwnd; |
open.hwndOwner = hwnd; |
146 |
open.lpstrFile = file; |
open.lpstrFile = file; |
147 |
open.nMaxFile = sizeof (file) - 1; |
open.nMaxFile = DIM (file) - 1; |
148 |
if (id == CDLG_FILE_OPEN) |
if (id == CDLG_FILE_OPEN) |
149 |
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
150 |
else |
else |
151 |
open.Flags = OFN_OVERWRITEPROMPT; |
open.Flags = OFN_OVERWRITEPROMPT; |
152 |
|
|
153 |
if (id == CDLG_FILE_OPEN && GetOpenFileName (&open)) |
if (id == CDLG_FILE_OPEN && GetOpenFileName (&open)) |
154 |
return open.lpstrFile; |
sel_file = open.lpstrFile; |
155 |
else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open)) |
else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open)) |
156 |
return open.lpstrFile; |
sel_file = open.lpstrFile; |
157 |
|
SetCurrentDirectory (workdir); |
158 |
|
|
159 |
return NULL; |
return sel_file; |
160 |
} |
} |
161 |
|
|
162 |
const char* |
const char* |
192 |
else |
else |
193 |
memset (folder, 0, sizeof (folder)); |
memset (folder, 0, sizeof (folder)); |
194 |
il = SHBrowseForFolder (&bi); |
il = SHBrowseForFolder (&bi); |
195 |
if (il) { |
if (il != NULL) { |
196 |
SHGetPathFromIDList (il, folder); |
SHGetPathFromIDList (il, folder); |
197 |
_SHFree (il); |
_SHFree (il); |
198 |
return folder; |
return folder; |
254 |
BUG (NULL); |
BUG (NULL); |
255 |
p = (char *) GlobalLock (clipmem); |
p = (char *) GlobalLock (clipmem); |
256 |
if (p == NULL) { |
if (p == NULL) { |
257 |
rc = WPTERR_GENERAL;; |
CloseClipboard (); |
258 |
goto leave; |
GlobalFree (clipmem); |
259 |
|
return WPTERR_GENERAL; |
260 |
} |
} |
261 |
memcpy (p, text, nbytes); |
memcpy (p, text, nbytes); |
262 |
p[nbytes] = '\0'; |
p[nbytes] = '\0'; |
263 |
|
|
|
GlobalUnlock (clipmem); |
|
264 |
SetClipboardData (CF_TEXT, clipmem); |
SetClipboardData (CF_TEXT, clipmem); |
265 |
|
GlobalUnlock (clipmem); |
266 |
|
CloseClipboard (); |
267 |
GlobalFree (clipmem); |
GlobalFree (clipmem); |
268 |
|
|
|
leave: |
|
|
CloseClipboard (); |
|
269 |
return rc; |
return rc; |
270 |
} |
} |
271 |
|
|
275 |
int |
int |
276 |
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) |
277 |
{ |
{ |
278 |
|
const char *fmt; |
279 |
char *p, *new_text; |
char *p, *new_text; |
280 |
|
|
281 |
p = get_clip_text (hwnd); |
p = get_clip_text (hwnd); |
282 |
if (!p) |
if (!p) |
283 |
return WPTERR_CLIP_GET; |
return WPTERR_CLIP_GET; |
284 |
new_text = new char [strlen (p)+strlen (text)+8]; |
if (as_footer == 0) |
285 |
|
fmt = "%s\r\n%s\r\n\r\n"; |
286 |
|
else |
287 |
|
fmt = "%s\n%s\n\n"; |
288 |
|
new_text = new char [strlen (p)+strlen (text)+strlen (fmt)+2]; |
289 |
if (!new_text) |
if (!new_text) |
290 |
BUG (0); |
BUG (0); |
291 |
if (as_footer == 0) |
if (as_footer == 0) |
292 |
sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p); |
sprintf (new_text, fmt, text, p); |
293 |
else |
else |
294 |
sprintf (new_text, "%s\n%s\n\n", p, text); |
sprintf (new_text, fmt, p, text); |
295 |
set_clip_text (hwnd, new_text, strlen (new_text)+1); |
set_clip_text (hwnd, new_text, strlen (new_text)+1); |
296 |
free_if_alloc (p); |
free_if_alloc (p); |
297 |
free_if_alloc (new_text); |
free_if_alloc (new_text); |
308 |
char *p; |
char *p; |
309 |
size_t size = 0; |
size_t size = 0; |
310 |
|
|
311 |
if( path && *path ) |
if (path && *path) |
312 |
size += strlen( path ); |
size += strlen (path); |
313 |
if( file && *file ) |
if (file && *file) |
314 |
size += strlen( file ); |
size += strlen (file); |
315 |
if( ext && *ext ) |
if (ext && *ext) |
316 |
size += strlen( ext ); |
size += strlen (ext); |
317 |
p = new char[size + 4]; |
p = new char[size + 4]; |
318 |
memset( p, 0, size ); |
if (!p) |
319 |
if( path ) { |
BUG (0); |
320 |
strcat( p, path ); |
memset (p, 0, size); |
321 |
if( path[strlen( path ) -1] != '\\' ) |
if (path) { |
322 |
strcat( p, "\\" ); |
strcat (p, path); |
323 |
} |
if (path[strlen (path) -1] != '\\') |
324 |
if( file ) |
strcat (p, "\\"); |
325 |
strcat( p, file ); |
} |
326 |
if( ext ) { |
if (file != NULL) |
327 |
strcat( p, "." ); |
strcat (p, file); |
328 |
strcat( p, ext ); |
if (ext != NULL) { |
329 |
|
strcat (p, "."); |
330 |
|
strcat (p, ext); |
331 |
} |
} |
332 |
return p; |
return p; |
333 |
} |
} |
437 |
} |
} |
438 |
|
|
439 |
|
|
|
/* 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; |
|
|
} |
|
|
|
|
|
|
|
440 |
/* Center the hwndChild relative to parent. |
/* Center the hwndChild relative to parent. |
441 |
The style param allows to specificy additional styles (like topmost). */ |
The style param allows to specificy additional styles (like topmost). */ |
442 |
void |
void |
482 |
|
|
483 |
/* Center the given hwndChild window with no special style. */ |
/* Center the given hwndChild window with no special style. */ |
484 |
void |
void |
485 |
center_window (HWND hwndChild, HWND hwndParent) |
center_window (HWND child, HWND parent) |
486 |
{ |
{ |
487 |
center_window2 (hwndChild, hwndParent, NULL); |
center_window2 (child, parent, NULL); |
488 |
} |
} |
489 |
|
|
490 |
|
|
492 |
Format: MAJOR.MINOR.PATCH1.PATCH2 |
Format: MAJOR.MINOR.PATCH1.PATCH2 |
493 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
494 |
int |
int |
495 |
get_file_version (const char *fname, WORD *major, WORD *minor, |
get_file_version (const char *fname, |
496 |
WORD *patch1, WORD *patch2) |
WORD *major, WORD *minor, WORD *patch1, WORD *patch2) |
497 |
{ |
{ |
498 |
VS_FIXEDFILEINFO *inf = {0}; |
VS_FIXEDFILEINFO *inf; |
499 |
char file[MAX_PATH+1] = {0}; |
char file[MAX_PATH+1] = {0}; |
500 |
LPVOID buf, data; |
LPVOID buf, data; |
501 |
DWORD arg; |
DWORD arg; |
506 |
size = GetFileVersionInfoSize (file, &arg); |
size = GetFileVersionInfoSize (file, &arg); |
507 |
if (!size) |
if (!size) |
508 |
return -1; |
return -1; |
509 |
buf = (LPVOID)new CHAR[size]; |
buf = (LPVOID)new char[size]; |
510 |
if (!buf) |
if (!buf) |
511 |
BUG (NULL); |
BUG (NULL); |
512 |
GetFileVersionInfo (file, 0, size, buf); |
GetFileVersionInfo (file, 0, size, buf); |
514 |
qlen=0; |
qlen=0; |
515 |
VerQueryValue (buf, "\\", &data, &qlen); |
VerQueryValue (buf, "\\", &data, &qlen); |
516 |
if (!qlen) { |
if (!qlen) { |
517 |
delete [] (char*)buf; |
delete [](char*)buf; |
518 |
return -1; |
return -1; |
519 |
} |
} |
520 |
inf = (VS_FIXEDFILEINFO*)data; |
inf = (VS_FIXEDFILEINFO*)data; |
528 |
if (patch2) |
if (patch2) |
529 |
*patch2 = LOWORD (inf->dwProductVersionLS); |
*patch2 = LOWORD (inf->dwProductVersionLS); |
530 |
|
|
531 |
delete [] (char*)buf; |
delete [](char*)buf; |
532 |
return 0; |
return 0; |
533 |
} |
} |
534 |
|
|
535 |
|
|
536 |
/* Return date in a format which complies with the |
/* Return date in a format which complies with the |
537 |
system locale settings. */ |
locale user settings. */ |
538 |
const char* |
const char* |
539 |
get_locale_date (long tm_t, char *buf, DWORD buflen) |
get_locale_date (long tm_t, char *buf, DWORD buflen) |
540 |
{ |
{ |
541 |
SYSTEMTIME st; |
SYSTEMTIME st; |
542 |
struct tm *ptm; |
struct tm *ptm; |
543 |
|
|
544 |
ptm = localtime (&tm_t); |
ptm = localtime (&tm_t); |
545 |
st.wYear = (WORD)ptm->tm_year; |
memset (&st, 0, sizeof (st)); |
546 |
st.wMonth = (WORD)ptm->tm_mon; |
st.wYear = (WORD)ptm->tm_year+1900; |
547 |
|
st.wMonth = (WORD)ptm->tm_mon+1; |
548 |
st.wDay = (WORD)ptm->tm_mday; |
st.wDay = (WORD)ptm->tm_mday; |
|
st.wYear += 1900; |
|
|
st.wMonth += 1; |
|
549 |
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
550 |
NULL, buf, buflen)) |
NULL, buf, buflen)) |
551 |
return NULL; |
return NULL; |
552 |
return buf; |
return buf; |
553 |
} |
} |
554 |
|
|
555 |
|
/* Return the time in a format which complies with the locale user |
556 |
|
settings. */ |
557 |
|
const char* |
558 |
|
get_locale_time (long tm_t, char *buf, DWORD buflen) |
559 |
|
{ |
560 |
|
SYSTEMTIME st; |
561 |
|
struct tm *ptm; |
562 |
|
|
563 |
|
ptm = localtime (&tm_t); |
564 |
|
memset (&st, 0, sizeof (st)); |
565 |
|
st.wMinute = (WORD)ptm->tm_min; |
566 |
|
st.wHour = (WORD)ptm->tm_hour; |
567 |
|
st.wSecond = (WORD)ptm->tm_sec; |
568 |
|
|
569 |
|
if (!GetTimeFormat (LOCALE_USER_DEFAULT, TIME_FORCE24HOURFORMAT, &st, |
570 |
|
NULL, buf, buflen)) |
571 |
|
return NULL; |
572 |
|
return buf; |
573 |
|
} |
574 |
|
|
575 |
|
|
576 |
|
/* Return a combination of date+time which complies to the |
577 |
|
local user settings. */ |
578 |
|
const char* |
579 |
|
get_locale_timedate (long tm_t, char *buf, DWORD buflen) |
580 |
|
{ |
581 |
|
DWORD nleft = buflen; |
582 |
|
|
583 |
|
if (!get_locale_date (tm_t, buf, buflen)) |
584 |
|
return NULL; |
585 |
|
nleft -= strlen (buf)+1; |
586 |
|
if (nleft < 1) |
587 |
|
return NULL; |
588 |
|
strcat (buf, " "); |
589 |
|
nleft--; |
590 |
|
if (!get_locale_time (tm_t, buf+strlen (buf), nleft)) |
591 |
|
return NULL; |
592 |
|
return buf; |
593 |
|
} |
594 |
|
|
595 |
|
|
596 |
/* Generate a temporary file name by using the users |
/* Generate a temporary file name by using the users |
597 |
temp path and optionally a name @name provided by the caller. |
temp path and optionally a name @name provided by the caller. |
601 |
{ |
{ |
602 |
char tmp[32]; |
char tmp[32]; |
603 |
|
|
604 |
|
|
605 |
if (!name) { |
if (!name) { |
606 |
sprintf (tmp, "%08lX", GetTickCount ()); |
_snprintf (tmp, DIM (tmp)-1, "%08lX", GetTickCount ()); |
607 |
name = tmp; |
name = tmp; |
608 |
} |
} |
609 |
|
|
610 |
/* in the mobile mode we use a local temp folder |
/* In the mobile mode we need to use the current directory |
611 |
with the fixed name 'temp'. */ |
as the temp path because no files should be stored outside |
612 |
if (mobile_mode_active) { |
the USB disk drive. */ |
613 |
_snprintf (buf, buflen-1, "temp\\%s", name); |
#ifdef WINPT_MOBILE |
614 |
return 0; |
memset (buf, 0, buflen); |
615 |
} |
strncpy (buf, name, buflen-1); |
616 |
|
return 0; |
617 |
|
#endif |
618 |
|
|
619 |
if (!GetTempPath (buflen - strlen (name) -2, buf)) { |
if (!GetTempPath (buflen - strlen (name) -2, buf)) { |
620 |
log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ()); |
log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ()); |
621 |
return -1; |
return -1; |
622 |
} |
} |
623 |
|
|
624 |
strcat (buf, name); |
strcat (buf, name); |
625 |
return 0; |
return 0; |
626 |
} |
} |
627 |
|
|
628 |
|
|
629 |
struct reminder_hd_s { |
void |
630 |
int msecs; |
ListBox_AddString_utf8 (HWND lb, const char *txt) |
631 |
HWND dlg; |
{ |
632 |
HANDLE hd; |
char *utf8_txt; |
633 |
}; |
|
634 |
|
utf8_txt = utf8_to_native (txt); |
635 |
|
SendMessage (lb, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt)); |
636 |
|
safe_free (utf8_txt); |
637 |
|
} |
638 |
|
|
639 |
|
|
640 |
static DWORD CALLBACK |
void |
641 |
foreground_reminder_thread (void *c) |
ComboBox_AddString_utf8 (HWND cb, const char *txt) |
642 |
{ |
{ |
643 |
struct reminder_hd_s *ctx = (struct reminder_hd_s *)c; |
char *utf8_txt; |
644 |
Sleep (ctx->msecs); |
|
645 |
SetForegroundWindow (ctx->dlg); |
utf8_txt = utf8_to_native (txt); |
646 |
CloseHandle (ctx->hd); |
SendMessage ((cb), CB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt)); |
647 |
delete ctx; |
safe_free (utf8_txt); |
|
ExitThread (0); |
|
|
return 0; |
|
648 |
} |
} |
649 |
|
|
650 |
/* Try to force the window @dlg to the foreground. |
|
651 |
On NT5 or later this will not work if the user |
/* GetDlgItemText replacement with UTF8 support. */ |
652 |
is working in another window (console for example). */ |
int |
653 |
void |
GetDlgItemText_utf8 (HWND dlg, int id, char **r_txt) |
|
force_foreground_window (HWND dlg, int msecs) |
|
654 |
{ |
{ |
655 |
struct reminder_hd_s *hd; |
int len = GetWindowTextLength (GetDlgItem (dlg, id)); |
656 |
DWORD tid; |
char *txt; |
657 |
|
|
658 |
|
*r_txt = NULL; |
659 |
|
if (len < 1) |
660 |
|
return 0; |
661 |
|
txt = new char[len+2]; |
662 |
|
if (!txt) |
663 |
|
BUG (NULL); |
664 |
|
GetDlgItemText (dlg, id, txt, len+1); |
665 |
|
*r_txt = native_to_utf8 (txt); |
666 |
|
free_if_alloc (txt); |
667 |
|
return len; |
668 |
|
} |
669 |
|
|
670 |
|
|
671 |
|
/* Return TRUE if the current user has admin privileges. */ |
672 |
|
BOOL |
673 |
|
user_is_admin (void) |
674 |
|
{ |
675 |
|
SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY; |
676 |
|
HANDLE hd; |
677 |
|
TOKEN_GROUPS *ptg = NULL; |
678 |
|
DWORD ngtoken; |
679 |
|
DWORD i; |
680 |
|
BOOL admin = FALSE; |
681 |
|
PSID psid = 0; |
682 |
|
|
683 |
|
if (GetVersion () & 0x80000000) /* Win9X */ |
684 |
|
return TRUE; |
685 |
|
|
686 |
|
if (!OpenThreadToken (GetCurrentThread (), TOKEN_QUERY, FALSE, &hd) && |
687 |
|
!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &hd)) |
688 |
|
return FALSE; |
689 |
|
|
690 |
|
if (!GetTokenInformation (hd, TokenGroups, NULL, 0, &ngtoken) && |
691 |
|
GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
692 |
|
ptg = (TOKEN_GROUPS*)GlobalAlloc (GPTR, ngtoken); |
693 |
|
if (!ptg) |
694 |
|
return FALSE; |
695 |
|
|
696 |
|
if (!GetTokenInformation (hd, TokenGroups, |
697 |
|
ptg, ngtoken, &ngtoken)) { |
698 |
|
GlobalFree (ptg); |
699 |
|
return FALSE; |
700 |
|
} |
701 |
|
AllocateAndInitializeSid (&SystemSidAuthority, |
702 |
|
2, SECURITY_BUILTIN_DOMAIN_RID, |
703 |
|
DOMAIN_ALIAS_RID_ADMINS, |
704 |
|
0, 0, 0, 0, 0, 0, |
705 |
|
&psid); |
706 |
|
for (i = 0; i < ptg->GroupCount; i++) { |
707 |
|
if (EqualSid (ptg->Groups[i].Sid, psid)) { |
708 |
|
admin = TRUE; |
709 |
|
break; |
710 |
|
} |
711 |
|
} |
712 |
|
FreeSid (psid); |
713 |
|
GlobalFree (ptg); |
714 |
|
} |
715 |
|
|
716 |
hd = new reminder_hd_s; |
CloseHandle (hd); |
717 |
hd->dlg = dlg; |
return admin; |
|
hd->msecs = msecs; |
|
|
hd->hd = CreateThread (NULL, 0, foreground_reminder_thread, |
|
|
hd, NULL, &tid); |
|
718 |
} |
} |