Added options and advanced options dialogs which should've been

part of an earlier checkin.  Also now saving and restoring
options that aren't in the configuration file to/from the
registry.
This commit is contained in:
crs
2003-03-17 22:32:10 +00:00
parent a7e2141ede
commit a5633b1971
11 changed files with 396 additions and 169 deletions

View File

@@ -93,10 +93,10 @@ CArchDaemonWindows::installDaemon(const char* name,
value += commandLine;
// install entry
setValue(key, name, value);
CArchMiscWindows::setValue(key, name, value);
// clean up
closeKey(key);
CArchMiscWindows::closeKey(key);
}
// windows NT family services
@@ -137,7 +137,7 @@ CArchDaemonWindows::installDaemon(const char* name,
// open the registry key for this service
HKEY key = openNTServicesKey();
key = openKey(key, name);
key = CArchMiscWindows::openKey(key, name);
if (key == NULL) {
// can't open key
DWORD err = GetLastError();
@@ -151,14 +151,14 @@ CArchDaemonWindows::installDaemon(const char* name,
}
// set the description
setValue(key, _T("Description"), description);
CArchMiscWindows::setValue(key, _T("Description"), description);
// set command line
key = openKey(key, _T("Parameters"));
key = CArchMiscWindows::openKey(key, _T("Parameters"));
if (key == NULL) {
// can't open key
DWORD err = GetLastError();
closeKey(key);
CArchMiscWindows::closeKey(key);
try {
uninstallDaemon(name, allUsers);
}
@@ -167,10 +167,10 @@ CArchDaemonWindows::installDaemon(const char* name,
}
throw XArchDaemonInstallFailed(new XArchEvalWindows(err));
}
setValue(key, _T("CommandLine"), commandLine);
CArchMiscWindows::setValue(key, _T("CommandLine"), commandLine);
// done with registry
closeKey(key);
CArchMiscWindows::closeKey(key);
}
}
@@ -189,20 +189,20 @@ CArchDaemonWindows::uninstallDaemon(const char* name, bool allUsers)
}
// remove entry
deleteValue(key, name);
CArchMiscWindows::deleteValue(key, name);
// clean up
closeKey(key);
CArchMiscWindows::closeKey(key);
}
// windows NT family services
else {
// remove parameters for this service. ignore failures.
HKEY key = openNTServicesKey();
key = openKey(key, name);
key = CArchMiscWindows::openKey(key, name);
if (key != NULL) {
deleteKey(key, _T("Parameters"));
closeKey(key);
CArchMiscWindows::deleteKey(key, _T("Parameters"));
CArchMiscWindows::closeKey(key);
}
// open service manager
@@ -314,7 +314,7 @@ CArchDaemonWindows::canInstallDaemon(const char* name, bool allUsers)
// check if we can open the registry key
HKEY key = CArchMiscWindows::isWindows95Family() ?
open95ServicesKey() : openUserStartupKey();
closeKey(key);
CArchMiscWindows::closeKey(key);
return (key != NULL);
}
@@ -329,9 +329,9 @@ CArchDaemonWindows::canInstallDaemon(const char* name, bool allUsers)
// check if we can open the registry key for this service
HKEY key = openNTServicesKey();
key = openKey(key, name);
key = openKey(key, _T("Parameters"));
closeKey(key);
key = CArchMiscWindows::openKey(key, name);
key = CArchMiscWindows::openKey(key, _T("Parameters"));
CArchMiscWindows::closeKey(key);
return (key != NULL);
}
@@ -351,10 +351,11 @@ CArchDaemonWindows::isDaemonInstalled(const char* name, bool allUsers)
}
// check for entry
const bool installed = !readValueString(key, name).empty();
const bool installed = !CArchMiscWindows::readValueString(key,
name).empty();
// clean up
closeKey(key);
CArchMiscWindows::closeKey(key);
return installed;
}
@@ -363,12 +364,12 @@ CArchDaemonWindows::isDaemonInstalled(const char* name, bool allUsers)
else {
// check parameters for this service
HKEY key = openNTServicesKey();
key = openKey(key, name);
key = openKey(key, _T("Parameters"));
key = CArchMiscWindows::openKey(key, name);
key = CArchMiscWindows::openKey(key, _T("Parameters"));
if (key != NULL) {
const bool installed = !readValueString(key,
const bool installed = !CArchMiscWindows::readValueString(key,
_T("CommandLine")).empty();
closeKey(key);
CArchMiscWindows::closeKey(key);
if (!installed) {
return false;
}
@@ -393,106 +394,6 @@ CArchDaemonWindows::isDaemonInstalled(const char* name, bool allUsers)
}
}
HKEY
CArchDaemonWindows::openKey(HKEY key, const TCHAR* keyName)
{
// ignore if parent is NULL
if (key == NULL) {
return NULL;
}
// open next key
HKEY newKey;
LONG result = RegOpenKeyEx(key, keyName, 0,
KEY_WRITE | KEY_QUERY_VALUE, &newKey);
if (result != ERROR_SUCCESS) {
DWORD disp;
result = RegCreateKeyEx(key, keyName, 0, _T(""),
0, KEY_WRITE | KEY_QUERY_VALUE,
NULL, &newKey, &disp);
}
if (result != ERROR_SUCCESS) {
RegCloseKey(key);
return NULL;
}
// switch to new key
RegCloseKey(key);
return newKey;
}
HKEY
CArchDaemonWindows::openKey(HKEY key, const TCHAR** keyNames)
{
for (size_t i = 0; key != NULL && keyNames[i] != NULL; ++i) {
// open next key
key = openKey(key, keyNames[i]);
}
return key;
}
void
CArchDaemonWindows::closeKey(HKEY key)
{
assert(key != NULL);
RegCloseKey(key);
}
void
CArchDaemonWindows::deleteKey(HKEY key, const TCHAR* name)
{
assert(key != NULL);
assert(name != NULL);
RegDeleteKey(key, name);
}
void
CArchDaemonWindows::deleteValue(HKEY key, const TCHAR* name)
{
assert(key != NULL);
assert(name != NULL);
RegDeleteValue(key, name);
}
void
CArchDaemonWindows::setValue(HKEY key,
const TCHAR* name, const std::string& value)
{
assert(key != NULL);
assert(name != NULL);
RegSetValueEx(key, name, 0, REG_SZ,
reinterpret_cast<const BYTE*>(value.c_str()),
value.size() + 1);
}
std::string
CArchDaemonWindows::readValueString(HKEY key, const TCHAR* name)
{
// get the size of the string
DWORD type;
DWORD size = 0;
LONG result = RegQueryValueEx(key, name, 0, &type, NULL, &size);
if (result != ERROR_SUCCESS || type != REG_SZ) {
return std::string();
}
// allocate space
char* buffer = new char[size];
// read it
result = RegQueryValueEx(key, name, 0, &type,
reinterpret_cast<BYTE*>(buffer), &size);
if (result != ERROR_SUCCESS || type != REG_SZ) {
delete[] buffer;
return std::string();
}
// clean up and return value
std::string value(buffer);
delete[] buffer;
return value;
}
HKEY
CArchDaemonWindows::openNTServicesKey()
{
@@ -503,7 +404,7 @@ CArchDaemonWindows::openNTServicesKey()
NULL
};
return openKey(HKEY_LOCAL_MACHINE, s_keyNames);
return CArchMiscWindows::openKey(HKEY_LOCAL_MACHINE, s_keyNames);
}
HKEY
@@ -518,7 +419,7 @@ CArchDaemonWindows::open95ServicesKey()
NULL
};
return openKey(HKEY_LOCAL_MACHINE, s_keyNames);
return CArchMiscWindows::openKey(HKEY_LOCAL_MACHINE, s_keyNames);
}
HKEY
@@ -533,7 +434,7 @@ CArchDaemonWindows::openUserStartupKey()
NULL
};
return openKey(HKEY_CURRENT_USER, s_keyNames);
return CArchMiscWindows::openKey(HKEY_CURRENT_USER, s_keyNames);
}
int
@@ -730,10 +631,11 @@ CArchDaemonWindows::serviceMain(DWORD argc, LPTSTR* argvIn)
// read command line
std::string commandLine;
HKEY key = openNTServicesKey();
key = openKey(key, argvIn[0]);
key = openKey(key, _T("Parameters"));
key = CArchMiscWindows::openKey(key, argvIn[0]);
key = CArchMiscWindows::openKey(key, _T("Parameters"));
if (key != NULL) {
commandLine = readValueString(key, _T("CommandLine"));
commandLine = CArchMiscWindows::readValueString(key,
_T("CommandLine"));
}
// if the command line isn't empty then parse and use it

View File

@@ -75,14 +75,6 @@ public:
virtual bool isDaemonInstalled(const char* name, bool allUsers);
private:
static HKEY openKey(HKEY parent, const TCHAR*);
static HKEY openKey(HKEY parent, const TCHAR**);
static void closeKey(HKEY);
static void deleteKey(HKEY, const TCHAR* name);
static void deleteValue(HKEY, const TCHAR* name);
static void setValue(HKEY, const TCHAR* name,
const std::string& value);
static std::string readValueString(HKEY, const TCHAR* name);
static HKEY openNTServicesKey();
static HKEY open95ServicesKey();
static HKEY openUserStartupKey();

View File

@@ -12,11 +12,7 @@
* GNU General Public License for more details.
*/
#define WIN32_LEAN_AND_MEAN
#include "CArchMiscWindows.h"
#include "CArchDaemonWindows.h"
#include <windows.h>
//
// CArchMiscWindows
@@ -37,17 +33,150 @@ CArchMiscWindows::isWindows95Family()
int
CArchMiscWindows::runDaemon(RunFunc runFunc)
{
return CArchDaemonWindows::runDaemon(runFunc);
return CArchMiscWindows::runDaemon(runFunc);
}
void
CArchMiscWindows::daemonRunning(bool running)
{
CArchDaemonWindows::daemonRunning(running);
CArchMiscWindows::daemonRunning(running);
}
void
CArchMiscWindows::daemonFailed(int result)
{
CArchDaemonWindows::daemonFailed(result);
CArchMiscWindows::daemonFailed(result);
}
HKEY
CArchMiscWindows::openKey(HKEY key, const TCHAR* keyName)
{
// ignore if parent is NULL
if (key == NULL) {
return NULL;
}
// open next key
HKEY newKey;
LONG result = RegOpenKeyEx(key, keyName, 0,
KEY_WRITE | KEY_QUERY_VALUE, &newKey);
if (result != ERROR_SUCCESS) {
DWORD disp;
result = RegCreateKeyEx(key, keyName, 0, TEXT(""),
0, KEY_WRITE | KEY_QUERY_VALUE,
NULL, &newKey, &disp);
}
if (result != ERROR_SUCCESS) {
RegCloseKey(key);
return NULL;
}
// switch to new key
RegCloseKey(key);
return newKey;
}
HKEY
CArchMiscWindows::openKey(HKEY key, const TCHAR* const* keyNames)
{
for (size_t i = 0; key != NULL && keyNames[i] != NULL; ++i) {
// open next key
key = openKey(key, keyNames[i]);
}
return key;
}
void
CArchMiscWindows::closeKey(HKEY key)
{
assert(key != NULL);
RegCloseKey(key);
}
void
CArchMiscWindows::deleteKey(HKEY key, const TCHAR* name)
{
assert(key != NULL);
assert(name != NULL);
RegDeleteKey(key, name);
}
void
CArchMiscWindows::deleteValue(HKEY key, const TCHAR* name)
{
assert(key != NULL);
assert(name != NULL);
RegDeleteValue(key, name);
}
bool
CArchMiscWindows::hasValue(HKEY key, const TCHAR* name)
{
DWORD type;
LONG result = RegQueryValueEx(key, name, 0, &type, NULL, NULL);
return (result == ERROR_SUCCESS &&
(type == REG_DWORD || type == REG_SZ));
}
void
CArchMiscWindows::setValue(HKEY key,
const TCHAR* name, const std::string& value)
{
assert(key != NULL);
assert(name != NULL);
RegSetValueEx(key, name, 0, REG_SZ,
reinterpret_cast<const BYTE*>(value.c_str()),
value.size() + 1);
}
void
CArchMiscWindows::setValue(HKEY key, const TCHAR* name, DWORD value)
{
assert(key != NULL);
assert(name != NULL);
RegSetValueEx(key, name, 0, REG_DWORD,
reinterpret_cast<CONST BYTE*>(&value),
sizeof(DWORD));
}
std::string
CArchMiscWindows::readValueString(HKEY key, const TCHAR* name)
{
// get the size of the string
DWORD type;
DWORD size = 0;
LONG result = RegQueryValueEx(key, name, 0, &type, NULL, &size);
if (result != ERROR_SUCCESS || type != REG_SZ) {
return std::string();
}
// allocate space
char* buffer = new char[size];
// read it
result = RegQueryValueEx(key, name, 0, &type,
reinterpret_cast<BYTE*>(buffer), &size);
if (result != ERROR_SUCCESS || type != REG_SZ) {
delete[] buffer;
return std::string();
}
// clean up and return value
std::string value(buffer);
delete[] buffer;
return value;
}
DWORD
CArchMiscWindows::readValueInt(HKEY key, const TCHAR* name)
{
DWORD type;
DWORD value;
DWORD size = sizeof(value);
LONG result = RegQueryValueEx(key, name, 0, &type,
reinterpret_cast<BYTE*>(&value), &size);
if (result != ERROR_SUCCESS || type != REG_DWORD) {
return 0;
}
return value;
}

View File

@@ -15,7 +15,11 @@
#ifndef CARCHMISCWINDOWS_H
#define CARCHMISCWINDOWS_H
#define WIN32_LEAN_AND_MEAN
#include "common.h"
#include "stdstring.h"
#include <windows.h>
//! Miscellaneous win32 functions.
class CArchMiscWindows {
@@ -45,6 +49,37 @@ public:
Delegates to CArchDaemonWindows.
*/
static void daemonFailed(int result);
//! Open and return a registry key, closing the parent key
static HKEY openKey(HKEY parent, const TCHAR* child);
//! Open and return a registry key, closing the parent key
static HKEY openKey(HKEY parent, const TCHAR* const* keyPath);
//! Close a key
static void closeKey(HKEY);
//! Delete a key (which should have no subkeys)
static void deleteKey(HKEY parent, const TCHAR* name);
//! Delete a value
static void deleteValue(HKEY parent, const TCHAR* name);
//! Test if a value exists
static bool hasValue(HKEY key, const TCHAR* name);
//! Set a string value in the registry
static void setValue(HKEY key, const TCHAR* name,
const std::string& value);
//! Set a DWORD value in the registry
static void setValue(HKEY key, const TCHAR* name, DWORD value);
//! Read a string value from the registry
static std::string readValueString(HKEY, const TCHAR* name);
//! Read a DWORD value from the registry
static DWORD readValueInt(HKEY, const TCHAR* name);
};
#endif