From 964d728a42aad12aed5e5bf3462718faed2b50ef Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Wed, 6 Jul 2016 16:01:36 -0400 Subject: [PATCH] Use file associations to open config and log - Use ShellExecute to open config and log files so that file associations can be used. If that fails fall-back to the default editor (notepad.exe). - Remove the keys editor and log_viewer from registry. The user can change the editor/viewer through fie association. Signed-off-by: Selva Nair --- Makefile.am | 1 + registry.c | 14 ++++++++------ viewlog.c | 29 +++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/Makefile.am b/Makefile.am index d526be7..4394643 100644 --- a/Makefile.am +++ b/Makefile.am @@ -104,6 +104,7 @@ openvpn_gui_LDADD = \ -lwtsapi32 \ -lcrypt32 \ -lnetapi32 \ + -lole32 \ -lsecur32 openvpn-gui-res.o: $(openvpn_gui_RESOURCES) $(srcdir)/openvpn-gui-res.h diff --git a/registry.c b/registry.c index 27ebb33..569a475 100644 --- a/registry.c +++ b/registry.c @@ -128,13 +128,15 @@ GetRegistryKeys() if (!GetRegKey(_T("log_append"), o.append_string, _T("0"), _countof(o.append_string))) return(false); - _sntprintf_0(temp_path, _T("%s\\system32\\notepad.exe"), windows_dir); - if (!GetRegKey(_T("log_viewer"), o.log_viewer, - temp_path, _countof(o.log_viewer))) return(false); + if (o.editor[0] != L'\0') /* set by cmd-line */ + ExpandString (o.editor, _countof(o.editor)); + else + _sntprintf_0(o.editor, _T("%s\\system32\\notepad.exe"), windows_dir); - _sntprintf_0(temp_path, _T("%s\\system32\\notepad.exe"), windows_dir); - if (!GetRegKey(_T("editor"), o.editor, - temp_path, _countof(o.editor))) return(false); + if (o.log_viewer[0] != L'\0') /* set by cmd-line */ + ExpandString (o.log_viewer, _countof(o.log_viewer)); + else + _sntprintf_0(o.log_viewer, _T("%s\\system32\\notepad.exe"), windows_dir); if (!GetRegKey(_T("service_only"), o.service_only, _T("0"), _countof(o.service_only))) return(false); diff --git a/viewlog.c b/viewlog.c index f036486..cefeff7 100644 --- a/viewlog.c +++ b/viewlog.c @@ -25,6 +25,8 @@ #include #include +#include +#include #include "tray.h" #include "openvpn.h" @@ -37,18 +39,29 @@ extern options_t o; void ViewLog(int config) { - TCHAR filename[200]; + TCHAR filename[2*MAX_PATH]; STARTUPINFO start_info; PROCESS_INFORMATION proc_info; SECURITY_ATTRIBUTES sa; SECURITY_DESCRIPTOR sd; + HINSTANCE status; CLEAR (start_info); CLEAR (proc_info); CLEAR (sa); CLEAR (sd); + /* Try first using file association */ + CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); /* Safe to init COM multiple times */ + status = ShellExecuteW (o.hWnd, L"open", o.conn[config].log_path, NULL, o.log_dir, SW_SHOWNORMAL); + + if (status > (HINSTANCE) 32) /* Success */ + return; + else + PrintDebug (L"Opening log file using ShellExecute with verb = open failed" + " for config '%s' (status = %lu)", o.conn[config].config_name, status); + _sntprintf_0(filename, _T("%s \"%s\""), o.log_viewer, o.conn[config].log_path); /* fill in STARTUPINFO struct */ @@ -81,18 +94,30 @@ void ViewLog(int config) void EditConfig(int config) { - TCHAR filename[200]; + TCHAR filename[2*MAX_PATH]; STARTUPINFO start_info; PROCESS_INFORMATION proc_info; SECURITY_ATTRIBUTES sa; SECURITY_DESCRIPTOR sd; + HINSTANCE status; CLEAR (start_info); CLEAR (proc_info); CLEAR (sa); CLEAR (sd); + /* Try first using file association */ + _sntprintf_0(filename, L"%s\\%s", o.conn[config].config_dir, o.conn[config].config_file); + + CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); /* Safe to init COM multiple times */ + status = ShellExecuteW (o.hWnd, L"open", filename, NULL, o.conn[config].config_dir, SW_SHOWNORMAL); + if (status > (HINSTANCE) 32) + return; + else + PrintDebug (L"Opening config file using ShellExecute with verb = open failed" + " for config '%s' (status = %lu)", o.conn[config].config_name, status); + _sntprintf_0(filename, _T("%s \"%s\\%s\""), o.editor, o.conn[config].config_dir, o.conn[config].config_file); /* fill in STARTUPINFO struct */