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 |
* |
* |
35 |
#include "wptW32API.h" |
#include "wptW32API.h" |
36 |
#include "wptErrors.h" |
#include "wptErrors.h" |
37 |
#include "wptVersion.h" |
#include "wptVersion.h" |
38 |
|
#include "wptUTF8.h" |
39 |
#include "wptTypes.h" |
#include "wptTypes.h" |
40 |
|
|
41 |
|
|
42 |
extern "C" void _SHFree (void *p); |
extern "C" void _SHFree (void *p); |
43 |
|
|
44 |
|
|
45 |
|
/* Insert a new item into the menu @hm at position @pos. */ |
46 |
|
void |
47 |
|
insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text) |
48 |
|
{ |
49 |
|
MENUITEMINFO mi; |
50 |
|
|
51 |
|
memset (&mi, 0, sizeof mi); |
52 |
|
mi.cbSize = sizeof mi; |
53 |
|
mi.fType = MF_STRING; |
54 |
|
mi.dwTypeData = (char *)text; |
55 |
|
mi.cch = strlen (text); |
56 |
|
mi.wID = m_id; |
57 |
|
mi.fMask = MIIM_ID|MIIM_DATA| MIIM_TYPE; |
58 |
|
InsertMenuItem (hm, pos, FALSE, &mi); |
59 |
|
} |
60 |
|
|
61 |
|
|
62 |
static void |
static void |
63 |
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) |
64 |
{ |
{ |
126 |
id can be either FILE_OPEN or FILE_SAVE. |
id can be either FILE_OPEN or FILE_SAVE. |
127 |
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. */ |
128 |
const char * |
const char * |
129 |
get_filename_dlg (HWND hwnd, int id, const char * title, |
get_filename_dlg (HWND hwnd, int id, const char *title, |
130 |
const char * filter, const char * name) |
const char *filter, const char *name) |
131 |
{ |
{ |
132 |
static char file[512] = ""; |
static char file[2*MAX_PATH+1] = ""; |
133 |
|
char workdir[MAX_PATH+1]; |
134 |
|
char *sel_file = NULL; |
135 |
OPENFILENAME open; |
OPENFILENAME open; |
136 |
|
|
137 |
if (name && strlen (name) < (sizeof (file)-1)) |
if (name && strlen (name) < (DIM (file)-1)) |
138 |
strcpy (file, name); |
strcpy (file, name); |
139 |
else |
else |
140 |
memset (file, 0, sizeof (file)); |
memset (file, 0, sizeof (file)); |
141 |
if (!filter) |
if (!filter) |
142 |
filter = "All Files (*.*)\0*.*\0\0"; |
filter = "All Files (*.*)\0*.*\0\0"; |
143 |
/* XXX: problem with gettext because of the 'artificial' |
|
144 |
double string termination!. */ |
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* |
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); |
271 |
|
CloseClipboard (); |
272 |
GlobalFree (clipmem); |
GlobalFree (clipmem); |
273 |
|
|
|
leave: |
|
|
CloseClipboard (); |
|
274 |
return rc; |
return rc; |
275 |
} |
} |
276 |
|
|
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 |
} |
} |
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 |
|
|
497 |
Format: MAJOR.MINOR.PATCH1.PATCH2 |
Format: MAJOR.MINOR.PATCH1.PATCH2 |
498 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
499 |
int |
int |
500 |
get_file_version (const char *fname, WORD *major, WORD *minor, |
get_file_version (const char *fname, |
501 |
WORD *patch1, WORD *patch2) |
WORD *major, WORD *minor, WORD *patch1, WORD *patch2) |
502 |
{ |
{ |
503 |
VS_FIXEDFILEINFO *inf = {0}; |
VS_FIXEDFILEINFO *inf; |
504 |
char file[MAX_PATH+1] = {0}; |
char file[MAX_PATH+1] = {0}; |
505 |
LPVOID buf, data; |
LPVOID buf, data; |
506 |
DWORD arg; |
DWORD arg; |
511 |
size = GetFileVersionInfoSize (file, &arg); |
size = GetFileVersionInfoSize (file, &arg); |
512 |
if (!size) |
if (!size) |
513 |
return -1; |
return -1; |
514 |
buf = (LPVOID)new CHAR[size]; |
buf = (LPVOID)new char[size]; |
515 |
if (!buf) |
if (!buf) |
516 |
BUG (NULL); |
BUG (NULL); |
517 |
GetFileVersionInfo (file, 0, size, buf); |
GetFileVersionInfo (file, 0, size, buf); |
519 |
qlen=0; |
qlen=0; |
520 |
VerQueryValue (buf, "\\", &data, &qlen); |
VerQueryValue (buf, "\\", &data, &qlen); |
521 |
if (!qlen) { |
if (!qlen) { |
522 |
delete [] (char*)buf; |
delete [](char*)buf; |
523 |
return -1; |
return -1; |
524 |
} |
} |
525 |
inf = (VS_FIXEDFILEINFO*)data; |
inf = (VS_FIXEDFILEINFO*)data; |
533 |
if (patch2) |
if (patch2) |
534 |
*patch2 = LOWORD (inf->dwProductVersionLS); |
*patch2 = LOWORD (inf->dwProductVersionLS); |
535 |
|
|
536 |
delete [] (char*)buf; |
delete [](char*)buf; |
537 |
return 0; |
return 0; |
538 |
} |
} |
539 |
|
|
567 |
{ |
{ |
568 |
char tmp[32]; |
char tmp[32]; |
569 |
|
|
570 |
|
|
571 |
if (!name) { |
if (!name) { |
572 |
sprintf (tmp, "%08lX", GetTickCount ()); |
_snprintf (tmp, DIM (tmp)-1, "%08lX", GetTickCount ()); |
573 |
name = tmp; |
name = tmp; |
574 |
} |
} |
575 |
|
|
576 |
/* in the mobile mode we use a local temp folder |
/* In the mobile mode we need to use the current directory |
577 |
with the fixed name 'temp'. */ |
as the temp path because no files should be stored outside |
578 |
if (mobile_mode_active) { |
the USB disk drive. */ |
579 |
_snprintf (buf, buflen-1, "temp\\%s", name); |
#ifdef WINPT_MOBILE |
580 |
return 0; |
memset (buf, 0, buflen); |
581 |
} |
strncpy (buf, name, buflen-1); |
582 |
|
return 0; |
583 |
|
#endif |
584 |
|
|
585 |
if (!GetTempPath (buflen - strlen (name) -2, buf)) { |
if (!GetTempPath (buflen - strlen (name) -2, buf)) { |
586 |
log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ()); |
log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ()); |
587 |
return -1; |
return -1; |
588 |
} |
} |
589 |
|
|
590 |
strcat (buf, name); |
strcat (buf, name); |
591 |
return 0; |
return 0; |
592 |
} |
} |
593 |
|
|
594 |
|
|
595 |
struct reminder_hd_s { |
void |
596 |
int msecs; |
ListBox_AddString_utf8 (HWND lb, const char *txt) |
597 |
HWND dlg; |
{ |
598 |
HANDLE hd; |
char *utf8_txt; |
|
}; |
|
599 |
|
|
600 |
|
utf8_txt = utf8_to_native (txt); |
601 |
|
SendMessage (lb, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt)); |
602 |
|
safe_free (utf8_txt); |
603 |
|
} |
604 |
|
|
605 |
static DWORD CALLBACK |
|
606 |
foreground_reminder_thread (void *c) |
void |
607 |
|
ComboBox_AddString_utf8 (HWND cb, const char *txt) |
608 |
{ |
{ |
609 |
struct reminder_hd_s *ctx = (struct reminder_hd_s *)c; |
|
610 |
Sleep (ctx->msecs); |
char *utf8_txt; |
611 |
SetForegroundWindow (ctx->dlg); |
|
612 |
CloseHandle (ctx->hd); |
utf8_txt = utf8_to_native (txt); |
613 |
delete ctx; |
SendMessage ((cb), CB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt)); |
614 |
ExitThread (0); |
safe_free (utf8_txt); |
|
return 0; |
|
615 |
} |
} |
616 |
|
|
617 |
/* Try to force the window @dlg to the foreground. |
|
618 |
On NT5 or later this will not work if the user |
/* GetDlgItemText replacement with UTF8 support. */ |
619 |
is working in another window (console for example). */ |
int |
620 |
void |
GetDlgItemText_utf8 (HWND dlg, int id, char **r_txt) |
621 |
force_foreground_window (HWND dlg, int msecs) |
{ |
622 |
|
int len = GetWindowTextLength (GetDlgItem (dlg, id)); |
623 |
|
char *txt; |
624 |
|
|
625 |
|
*r_txt = NULL; |
626 |
|
if (len < 1) |
627 |
|
return 0; |
628 |
|
txt = new char[len+2]; |
629 |
|
if (!txt) |
630 |
|
BUG (NULL); |
631 |
|
GetDlgItemText (dlg, id, txt, len+1); |
632 |
|
*r_txt = native_to_utf8 (txt); |
633 |
|
free_if_alloc (txt); |
634 |
|
return len; |
635 |
|
} |
636 |
|
|
637 |
|
|
638 |
|
/* Return TRUE if the current user has admin privileges. */ |
639 |
|
BOOL |
640 |
|
user_is_admin (void) |
641 |
{ |
{ |
642 |
struct reminder_hd_s *hd; |
SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY; |
643 |
DWORD tid; |
HANDLE hd; |
644 |
|
TOKEN_GROUPS *ptg = NULL; |
645 |
|
DWORD ngtoken; |
646 |
|
DWORD i; |
647 |
|
BOOL admin = FALSE; |
648 |
|
PSID psid = 0; |
649 |
|
|
650 |
|
if (GetVersion () & 0x80000000) /* Win9X */ |
651 |
|
return TRUE; |
652 |
|
|
653 |
|
if (!OpenThreadToken (GetCurrentThread (), TOKEN_QUERY, FALSE, &hd) && |
654 |
|
!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &hd)) |
655 |
|
return FALSE; |
656 |
|
|
657 |
|
if (!GetTokenInformation (hd, TokenGroups, NULL, 0, &ngtoken) && |
658 |
|
GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
659 |
|
ptg = (TOKEN_GROUPS*)GlobalAlloc (GPTR, ngtoken); |
660 |
|
if (!ptg) |
661 |
|
return FALSE; |
662 |
|
|
663 |
|
if (!GetTokenInformation (hd, TokenGroups, |
664 |
|
ptg, ngtoken, &ngtoken)) { |
665 |
|
GlobalFree (ptg); |
666 |
|
return FALSE; |
667 |
|
} |
668 |
|
AllocateAndInitializeSid (&SystemSidAuthority, |
669 |
|
2, SECURITY_BUILTIN_DOMAIN_RID, |
670 |
|
DOMAIN_ALIAS_RID_ADMINS, |
671 |
|
0, 0, 0, 0, 0, 0, |
672 |
|
&psid); |
673 |
|
for (i = 0; i < ptg->GroupCount; i++) { |
674 |
|
if (EqualSid (ptg->Groups[i].Sid, psid)) { |
675 |
|
admin = TRUE; |
676 |
|
break; |
677 |
|
} |
678 |
|
} |
679 |
|
FreeSid (psid); |
680 |
|
GlobalFree (ptg); |
681 |
|
} |
682 |
|
|
683 |
hd = new reminder_hd_s; |
CloseHandle (hd); |
684 |
hd->dlg = dlg; |
return admin; |
|
hd->msecs = msecs; |
|
|
hd->hd = CreateThread (NULL, 0, foreground_reminder_thread, |
|
|
hd, NULL, &tid); |
|
685 |
} |
} |