summaryrefslogtreecommitdiff
path: root/lib/win32
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2007-07-26 22:11:03 +0000
committerChris Wilson <chris+github@qwirx.com>2007-07-26 22:11:03 +0000
commit498e58eb188d98c6ec78e57cdc5f0c211f1f4dcf (patch)
tree4455a5542918955c8f5ade2915d4ae9a6b341093 /lib/win32
parent88edb51fc08aa8dfe1fed4c614343d1f2405dac9 (diff)
Make Configuration take a std::string filename instead of a char array,
in C++ style. Add a function to get default config file paths at runtime, dependent on the location of the executable being run. Pass the config file name directly to Daemon::Main, instead of faking argv. No default raid file path at compile time on Windows, depends on executable location when run. Determine RaidFile path at runtime if not supplied in config file on Windows. Don't define default locations for config files at compile time on Windows, provide macros to determine them at runtime instead. Make FileHandleGuard take a std::string instead of a char array, C++ style. Determine config file location at runtime instead of hard-coding on Windows. Thanks to Paul MacKenzie, Per Thomsen, Pete Jalajas, Stuart Sanders, Dave Bamford and Gary for pushing me to do this. (fixes #12) Determine config file path at runtime. Call Daemon::Main with config file name instead of building fake argv. (refs #3, merges [1684] [1685] [1686] [1687] [1688] [1689] [1690] [1691] [1692])
Diffstat (limited to 'lib/win32')
-rw-r--r--lib/win32/emu.cpp70
-rw-r--r--lib/win32/emu.h4
2 files changed, 58 insertions, 16 deletions
diff --git a/lib/win32/emu.cpp b/lib/win32/emu.cpp
index 4224d62e..1a6b0e79 100644
--- a/lib/win32/emu.cpp
+++ b/lib/win32/emu.cpp
@@ -218,6 +218,44 @@ bool EnableBackupRights( void )
}
}
+// forward declaration
+char* ConvertFromWideString(const WCHAR* pString, unsigned int codepage);
+
+// --------------------------------------------------------------------------
+//
+// Function
+// Name: GetDefaultConfigFilePath(std::string name)
+// Purpose: Calculates the default configuration file name,
+// by using the directory location of the currently
+// executing program, and appending the provided name.
+// In case of fire, returns an empty string.
+// Created: 26th May 2007
+//
+// --------------------------------------------------------------------------
+std::string GetDefaultConfigFilePath(const std::string& rName)
+{
+ WCHAR exePathWide[MAX_PATH];
+ GetModuleFileNameW(NULL, exePathWide, MAX_PATH-1);
+
+ char* exePathUtf8 = ConvertFromWideString(exePathWide, CP_UTF8);
+ if (exePathUtf8 == NULL)
+ {
+ return "";
+ }
+
+ std::string configfile = exePathUtf8;
+ delete [] exePathUtf8;
+
+ // make the default config file name,
+ // based on the program path
+ configfile = configfile.substr(0,
+ configfile.rfind('\\'));
+ configfile += "\\";
+ configfile += rName;
+
+ return configfile;
+}
+
// --------------------------------------------------------------------------
//
// Function
@@ -1252,15 +1290,15 @@ BOOL AddEventSource
// Work out the executable file name, to register ourselves
// as the event source
- char cmd[MAX_PATH];
- if (GetModuleFileName(NULL, cmd, sizeof(cmd)-1) == 0)
+ WCHAR cmd[MAX_PATH];
+ DWORD len = GetModuleFileNameW(NULL, cmd, MAX_PATH);
+
+ if (len == 0)
{
::syslog(LOG_ERR, "Failed to get the program file name: %s",
GetErrorMessage(GetLastError()).c_str());
return FALSE;
}
- cmd[sizeof(cmd)-1] = 0;
- std::string exepath(cmd);
// Create the event source as a subkey of the log.
@@ -1282,12 +1320,12 @@ BOOL AddEventSource
// Set the name of the message file.
- if (RegSetValueEx(hk, // subkey handle
- "EventMessageFile", // value name
- 0, // must be zero
- REG_EXPAND_SZ, // value type
- (LPBYTE) exepath.c_str(), // pointer to value data
- (DWORD) (exepath.size()))) // data size
+ if (RegSetValueExW(hk, // subkey handle
+ L"EventMessageFile", // value name
+ 0, // must be zero
+ REG_EXPAND_SZ, // value type
+ (LPBYTE)cmd, // pointer to value data
+ len*sizeof(WCHAR))) // data size
{
::syslog(LOG_ERR, "Failed to set the event message file: %s",
GetErrorMessage(GetLastError()).c_str());
@@ -1315,12 +1353,12 @@ BOOL AddEventSource
// Set the category message file and number of categories.
- if (RegSetValueEx(hk, // subkey handle
- "CategoryMessageFile", // value name
- 0, // must be zero
- REG_EXPAND_SZ, // value type
- (LPBYTE) exepath.c_str(), // pointer to value data
- (DWORD) (exepath.size()))) // data size
+ if (RegSetValueExW(hk, // subkey handle
+ L"CategoryMessageFile", // value name
+ 0, // must be zero
+ REG_EXPAND_SZ, // value type
+ (LPBYTE)cmd, // pointer to value data
+ len*sizeof(WCHAR))) // data size
{
::syslog(LOG_ERR, "Failed to set the category message file: "
"%s", GetErrorMessage(GetLastError()).c_str());
diff --git a/lib/win32/emu.h b/lib/win32/emu.h
index 1f078c14..8ab74130 100644
--- a/lib/win32/emu.h
+++ b/lib/win32/emu.h
@@ -377,6 +377,10 @@ bool ConvertFromUtf8 (const std::string& rSource, std::string& rDest,
bool ConvertUtf8ToConsole(const char* pString, std::string& rDest);
bool ConvertConsoleToUtf8(const char* pString, std::string& rDest);
+// Utility function which returns a default config file name,
+// based on the path of the current executable.
+std::string GetDefaultConfigFilePath(const std::string& rName);
+
// GetErrorMessage() returns a system error message, like strerror()
// but for Windows error codes.
std::string GetErrorMessage(DWORD errorCode);