Changeset 1685

Show
Ignore:
Timestamp:
26/05/2007 16:27:29 (20 months ago)
Author:
chris
Message:

Add a function to get default config file paths at runtime, dependent on
the location of the executable being run.

Location:
box/chris/general/lib/win32
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • box/chris/general/lib/win32/emu.cpp

    r1661 r1685  
    217217                return true; 
    218218        } 
     219} 
     220 
     221// forward declaration 
     222char* ConvertFromWideString(const WCHAR* pString, unsigned int codepage); 
     223 
     224// -------------------------------------------------------------------------- 
     225// 
     226// Function 
     227//              Name:    GetDefaultConfigFilePath(std::string name) 
     228//              Purpose: Calculates the default configuration file name, 
     229//                       by using the directory location of the currently 
     230//                       executing program, and appending the provided name. 
     231//                       In case of fire, returns an empty string. 
     232//              Created: 26th May 2007 
     233// 
     234// -------------------------------------------------------------------------- 
     235std::string GetDefaultConfigFilePath(const std::string& rName) 
     236{ 
     237        WCHAR exePathWide[MAX_PATH]; 
     238        GetModuleFileNameW(NULL, exePathWide, MAX_PATH-1); 
     239 
     240        char* exePathUtf8 = ConvertFromWideString(exePathWide, CP_UTF8); 
     241        if (exePathUtf8 == NULL) 
     242        { 
     243                return ""; 
     244        } 
     245 
     246        std::string configfile = exePathUtf8; 
     247        delete [] exePathUtf8; 
     248         
     249        // make the default config file name, 
     250        // based on the program path 
     251        configfile = configfile.substr(0, 
     252                configfile.rfind('\\')); 
     253        configfile += "\\"; 
     254        configfile += rName; 
     255 
     256        return configfile; 
    219257} 
    220258 
     
    12481286        // as the event source 
    12491287 
    1250         char cmd[MAX_PATH]; 
    1251         if (GetModuleFileName(NULL, cmd, sizeof(cmd)-1) == 0) 
     1288        WCHAR cmd[MAX_PATH]; 
     1289        DWORD len = GetModuleFileNameW(NULL, cmd, MAX_PATH); 
     1290 
     1291        if (len == 0) 
    12521292        { 
    12531293                ::syslog(LOG_ERR, "Failed to get the program file name: %s", 
     
    12551295                return FALSE; 
    12561296        } 
    1257         cmd[sizeof(cmd)-1] = 0; 
    1258         std::string exepath(cmd); 
    12591297 
    12601298        // Create the event source as a subkey of the log.  
     
    12781316        // Set the name of the message file.  
    12791317  
    1280         if (RegSetValueEx(hk,                // subkey handle  
    1281                          "EventMessageFile", // value name  
    1282                          0,                  // must be zero  
    1283                          REG_EXPAND_SZ,      // value type  
    1284                          (LPBYTE) exepath.c_str(),  // pointer to value data  
    1285                          (DWORD) (exepath.size()))) // data size 
     1318        if (RegSetValueExW(hk,                 // subkey handle  
     1319                           L"EventMessageFile", // value name  
     1320                           0,                  // must be zero  
     1321                           REG_EXPAND_SZ,      // value type  
     1322                           (LPBYTE)cmd,        // pointer to value data  
     1323                           len*sizeof(WCHAR))) // data size 
    12861324        { 
    12871325                ::syslog(LOG_ERR, "Failed to set the event message file: %s", 
     
    13111349        // Set the category message file and number of categories. 
    13121350 
    1313         if (RegSetValueEx(hk,                        // subkey handle  
    1314                           "CategoryMessageFile",    // value name  
    1315                           0,                         // must be zero  
    1316                           REG_EXPAND_SZ,             // value type  
    1317                           (LPBYTE) exepath.c_str(),  // pointer to value data  
    1318                           (DWORD) (exepath.size()))) // data size 
     1351        if (RegSetValueExW(hk,                    // subkey handle  
     1352                           L"CategoryMessageFile", // value name  
     1353                           0,                     // must be zero  
     1354                           REG_EXPAND_SZ,         // value type  
     1355                           (LPBYTE)cmd,           // pointer to value data  
     1356                           len*sizeof(WCHAR)))    // data size 
    13191357        { 
    13201358                ::syslog(LOG_ERR, "Failed to set the category message file: " 
  • box/chris/general/lib/win32/emu.h

    r1676 r1685  
    378378bool ConvertConsoleToUtf8(const char* pString, std::string& rDest); 
    379379 
     380// Utility function which returns a default config file name, 
     381// based on the path of the current executable. 
     382std::string GetDefaultConfigFilePath(const std::string& rName); 
     383 
    380384// GetErrorMessage() returns a system error message, like strerror()  
    381385// but for Windows error codes.