2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2006-01-23 08:47:05 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2006-01-23 08:47:05 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
2006-01-23 08:47:05 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2006-01-23 08:47:05 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2006-01-23 08:47:05 +00:00
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__)
|
2006-12-14 18:15:22 +00:00
|
|
|
#include "boinc_win.h"
|
2010-05-11 19:10:29 +00:00
|
|
|
#elif defined(_WIN32) && defined(__STDWX_H__)
|
|
|
|
#include "stdwx.h"
|
2006-12-14 18:15:22 +00:00
|
|
|
#endif
|
2009-07-25 02:27:42 +00:00
|
|
|
|
2013-01-08 05:36:44 +00:00
|
|
|
#if defined(_MSC_VER) || defined(__MINGW32__)
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
2006-01-23 08:47:05 +00:00
|
|
|
|
2013-01-08 05:36:44 +00:00
|
|
|
#include "diagnostics.h"
|
2013-09-20 18:46:26 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "filesys.h"
|
2012-08-11 05:47:18 +00:00
|
|
|
#include "win_util.h"
|
|
|
|
|
2009-07-25 03:51:18 +00:00
|
|
|
|
2007-02-20 16:27:15 +00:00
|
|
|
/**
|
|
|
|
* This function terminates a process by process id instead of a handle.
|
|
|
|
**/
|
|
|
|
BOOL TerminateProcessById( DWORD dwProcessID ) {
|
|
|
|
HANDLE hProcess;
|
|
|
|
BOOL bRetVal = FALSE;
|
|
|
|
|
|
|
|
hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, dwProcessID );
|
|
|
|
|
|
|
|
if (hProcess) {
|
|
|
|
bRetVal = TerminateProcess(hProcess, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle( hProcess );
|
|
|
|
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
2015-08-20 22:59:18 +00:00
|
|
|
// change the current directory to the BOINC data directory
|
|
|
|
// in the registry, if it exists
|
|
|
|
//
|
2008-05-22 03:09:11 +00:00
|
|
|
void chdir_to_data_dir() {
|
2011-01-07 20:23:22 +00:00
|
|
|
LONG lReturnValue;
|
|
|
|
HKEY hkSetupHive;
|
2013-09-20 18:46:26 +00:00
|
|
|
char szPath[MAX_PATH];
|
2014-05-06 15:28:47 +00:00
|
|
|
LPSTR lpszValue = NULL;
|
|
|
|
LPSTR lpszExpandedValue = NULL;
|
|
|
|
DWORD dwValueType = REG_EXPAND_SZ;
|
2011-01-07 20:23:22 +00:00
|
|
|
DWORD dwSize = 0;
|
2008-05-22 03:09:11 +00:00
|
|
|
|
2013-09-20 18:46:26 +00:00
|
|
|
lReturnValue = RegOpenKeyExA(
|
2008-05-22 03:09:11 +00:00
|
|
|
HKEY_LOCAL_MACHINE,
|
2013-09-20 18:46:26 +00:00
|
|
|
"SOFTWARE\\Space Sciences Laboratory, U.C. Berkeley\\BOINC Setup",
|
2011-01-07 20:23:22 +00:00
|
|
|
0,
|
2008-05-22 03:09:11 +00:00
|
|
|
KEY_READ,
|
|
|
|
&hkSetupHive
|
|
|
|
);
|
|
|
|
if (lReturnValue == ERROR_SUCCESS) {
|
|
|
|
// How large does our buffer need to be?
|
2013-09-20 18:46:26 +00:00
|
|
|
lReturnValue = RegQueryValueExA(
|
2008-05-22 03:09:11 +00:00
|
|
|
hkSetupHive,
|
2013-09-20 18:46:26 +00:00
|
|
|
"DATADIR",
|
2008-05-22 03:09:11 +00:00
|
|
|
NULL,
|
2014-05-06 15:28:47 +00:00
|
|
|
&dwValueType,
|
2008-05-22 03:09:11 +00:00
|
|
|
NULL,
|
|
|
|
&dwSize
|
|
|
|
);
|
|
|
|
if (lReturnValue != ERROR_FILE_NOT_FOUND) {
|
|
|
|
// Allocate the buffer space.
|
2014-05-06 15:28:47 +00:00
|
|
|
lpszValue = (LPSTR) malloc(dwSize);
|
|
|
|
(*lpszValue) = NULL;
|
2008-05-22 03:09:11 +00:00
|
|
|
|
|
|
|
// Now get the data
|
2013-09-20 18:46:26 +00:00
|
|
|
lReturnValue = RegQueryValueExA(
|
2008-05-22 03:09:11 +00:00
|
|
|
hkSetupHive,
|
2013-09-20 18:46:26 +00:00
|
|
|
"DATADIR",
|
2008-05-22 03:09:11 +00:00
|
|
|
NULL,
|
2014-05-06 15:28:47 +00:00
|
|
|
&dwValueType,
|
|
|
|
(LPBYTE)lpszValue,
|
2008-05-22 03:09:11 +00:00
|
|
|
&dwSize
|
|
|
|
);
|
|
|
|
|
2014-05-06 15:28:47 +00:00
|
|
|
// Expand the Strings
|
|
|
|
// We need to get the size of the buffer needed
|
|
|
|
dwSize = 0;
|
|
|
|
lReturnValue = ExpandEnvironmentStringsA(lpszValue, NULL, dwSize);
|
|
|
|
|
|
|
|
if (lReturnValue) {
|
|
|
|
// Make the buffer big enough for the expanded string
|
|
|
|
lpszExpandedValue = (LPSTR) malloc(lReturnValue);
|
|
|
|
(*lpszExpandedValue) = NULL;
|
|
|
|
dwSize = lReturnValue;
|
|
|
|
|
|
|
|
ExpandEnvironmentStringsA(lpszValue, lpszExpandedValue, dwSize);
|
|
|
|
|
|
|
|
SetCurrentDirectoryA(lpszExpandedValue);
|
|
|
|
}
|
2013-09-20 18:46:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_COMMON_APPDATA|CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, szPath))) {
|
|
|
|
strncat(szPath, "\\boinc", (sizeof(szPath) - strlen(szPath)));
|
|
|
|
if (boinc_file_exists(szPath)) {
|
|
|
|
SetCurrentDirectoryA(szPath);
|
|
|
|
}
|
2008-05-22 03:09:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-07 20:23:22 +00:00
|
|
|
if (hkSetupHive) RegCloseKey(hkSetupHive);
|
2014-05-06 15:28:47 +00:00
|
|
|
if (lpszValue) free(lpszValue);
|
|
|
|
if (lpszExpandedValue) free(lpszExpandedValue);
|
2008-05-22 03:09:11 +00:00
|
|
|
}
|
2009-07-08 19:33:27 +00:00
|
|
|
|
2014-11-18 21:35:47 +00:00
|
|
|
std::wstring boinc_ascii_to_wide(const std::string& str) {
|
2013-01-08 05:36:44 +00:00
|
|
|
int length_wide = MultiByteToWideChar(CP_ACP, 0, str.data(), -1, NULL, 0);
|
|
|
|
wchar_t *string_wide = static_cast<wchar_t*>(_alloca((length_wide * sizeof(wchar_t)) + sizeof(wchar_t)));
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, str.data(), -1, string_wide, length_wide);
|
|
|
|
std::wstring result(string_wide, length_wide);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:35:47 +00:00
|
|
|
std::string boinc_wide_to_ascii(const std::wstring& str) {
|
2013-01-08 05:36:44 +00:00
|
|
|
int length_ansi = WideCharToMultiByte(CP_UTF8, 0, str.data(), -1, NULL, 0, NULL, NULL);
|
|
|
|
char* string_ansi = static_cast<char*>(_alloca(length_ansi + sizeof(char)));
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, str.data(), -1, string_ansi, length_ansi, NULL, NULL);
|
|
|
|
std::string result(string_ansi, length_ansi);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get message for given error
|
|
|
|
//
|
|
|
|
char* windows_format_error_string(
|
|
|
|
unsigned long dwError, char* pszBuf, int iSize
|
|
|
|
) {
|
|
|
|
DWORD dwRet;
|
2013-01-08 07:44:20 +00:00
|
|
|
LPWSTR lpszTemp = NULL;
|
2013-01-08 05:36:44 +00:00
|
|
|
|
2013-01-08 07:44:20 +00:00
|
|
|
dwRet = FormatMessageW(
|
2013-01-08 05:36:44 +00:00
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_ARGUMENT_ARRAY,
|
|
|
|
NULL,
|
|
|
|
dwError,
|
|
|
|
LANG_NEUTRAL,
|
2013-01-08 07:44:20 +00:00
|
|
|
(LPWSTR)&lpszTemp,
|
2013-01-08 05:36:44 +00:00
|
|
|
0,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
|
2013-01-29 17:47:26 +00:00
|
|
|
if (dwRet != 0) {
|
|
|
|
// include the hex error code as well
|
2014-11-18 21:35:47 +00:00
|
|
|
snprintf(pszBuf, iSize, "%S (0x%x)", lpszTemp, dwError);
|
2013-01-29 17:47:26 +00:00
|
|
|
if (lpszTemp) {
|
2014-11-24 04:22:57 +00:00
|
|
|
LocalFree((HLOCAL)lpszTemp);
|
2013-01-29 17:47:26 +00:00
|
|
|
}
|
2013-04-11 08:22:43 +00:00
|
|
|
} else {
|
|
|
|
strcpy(pszBuf, "(unknown error)");
|
2013-01-08 05:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pszBuf;
|
|
|
|
}
|
|
|
|
|