2014-12-08 40 views
0

我有下面的代碼給系統32文件夾內的本地應用程序數據文件夾。我附上了以下代碼:SHGetFolderPath返回本地路徑,但不是當與服務相同時運行

int WriteToLog(const char* str) 
{ 
    FILE* log; 
    log = fopen("C:\\lpa\\sample.txt", "a+"); 
    if (log == NULL) 
     return -1; 
    fprintf(log, "%s\n", str); 
    fclose(log); 
    return 0; 
} 
std::string GetLocalAppDataPath() 
{ 
    HANDLE hfile; 
    TCHAR szPath[MAX_PATH]; 
    if(SUCCEEDED(SHGetFolderPath(NULL,CSIDL_LOCAL_APPDATA,NULL,0, szPath))) 
    { 
     std::string path = boost::lexical_cast<std::string>(szPath); 
     boost::replace_all(path, "\\", "\\\\"); 
     return path; 
    } 
} 

void LoggingInit() 
{ 
     log4cplus::initialize(); 
     helpers::LogLog::getLogLog()->setInternalDebugging(false); 
     std::string app_data_path = GetLocalAppDataPath(); 
     std::string log_folder_path = app_data_path + "\\\\lpa\\\\output\\\\"; 
     std::string log_file = log_folder_path + "output.log"; 
     WriteToLog(log_file.c_str()); 
     SharedAppenderPtr append_1(new RollingFileAppender(LOG4CPLUS_TEXT(log_file), 10*1024*1024, 5)); 
     append_1->setName(LOG4CPLUS_TEXT("LogpointAgentLog")); 
     PatternLayout *p = new PatternLayout(LOG4CPLUS_TEXT("[%D] <%-5p> [%F : %L] %m%n")); 
     append_1->setLayout(std::auto_ptr<Layout>(p)); 
     Logger::getRoot().addAppender(append_1); 
     root = Logger::getRoot(); 
     WriteToLog("Loging Init Successful"); 
     //std::string path = GetRegistryPath(); 
     //WriteToLog(path.c_str()); 
} 

當我使用製作服務創建運行程序時,我沒有得到真正的本地應用程序數據路徑。

int main(int argc, char **argv) 
{ 
    WriteToLog("Logging Init"); 
    LoggingInit(); 
    LOG4CPLUS_INFO(root, "Running as service"); 
    StartLpaService(); //Here I am creating a service. 
    return 0; 
} 

我得到以下數據中創建的文本文件通過WriteToLog功能

Logging Init 
C:\\WINDOWS\\system32\\config\\systemprofile\\AppData\\Local\\lpa\\output\\output.log 
Loging Init Successful 

我得到的本地應用程序數據路徑如果可執行文件運行正常,而不是服務。

C:\\Users\\logpoint\\AppData\\Local\\ 

代碼有什麼問題?

+0

你在哪個用戶帳戶下運行服務?顯然,它不是'logpoint';可能是'SYSTEM'。每個賬戶的「一條真實路徑」是不同的。 – 2014-12-09 02:19:58

+0

我遇到過使用LoadUserProfile(),ImpersonateLoggedOnUser()等函數模擬HKCU的信息。關於如何模擬當前用戶並獲取登錄用戶的LocalAppData路徑,您有任何參考嗎? – Pant 2014-12-09 16:44:34

+0

哪位用戶是「當前用戶」?在任何時候,可以有零個,一個或多個交互式用戶登錄到正在運行服務的計算機上。爲什麼你想要你的服務寫文件到用戶的配置文件?這是一件不尋常的事情想要做。 – 2014-12-09 17:53:48

回答

0

感謝您對Igor給出的見解。我在編寫wix安裝程序時通過在註冊表中編寫LocalAppDataFolder路徑來解決此問題。爲此,我使用:

<Component Id="registry_values" Guid="{11FB6C4C-3C90-4F46-B0D2-BB95150F60E6}"> 
     <RegistryKey Root="HKLM" 
        Key="Software\Logpoint" 
       Action="createAndRemoveOnUninstall"> 
      <RegistryValue Type="string" Name="path" Value="[LocalAppDataFolder]"/> 
     </RegistryKey> 
</Component> 

由於註冊表寫入路徑兩個64和和32位Windows不同,我下面的代碼就知道它:

bool DetectWindowsVersionBit() 
{ 
#if defined(WIN64) 
    return true; // 64-bit process running on 64-bit windows 
#endif 

    BOOL bIsWow64 = false; // must default to FALSE 
    typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); 
    LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(
    GetModuleHandle("kernel32"), "IsWow64Process"); 

    if (NULL != fnIsWow64Process) 
    { 
    if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64)) 
    { 
     //ASSERT(FALSE); // something went majorly wrong 
    } 
    } 
    return bIsWow64; 
} 

因此,我做了我自己的代碼,用於訪問路徑。我現在沒有使用SHGetFolderPath。這裏是:

std::string GetLocalAppDataPath() 
    { 
     HKEY hKey; 
     char buf[255]; 
     DWORD dwType; 
     DWORD dwBufSize = sizeof(buf); 
     std::string ss=""; 
     const char *subKey; 
     bool is64bit = DetectWindowsVersionBit(); 
     if(is64bit) 
     { 
      subKey = "Software\\\\Wow6432Node\\\\Logpoint"; 
     } 
     else 
     { 
      subKey = "Software\\\\Logpoint"; 
     } 
     WriteToLog(" Inside GetLocalAppDataPath") ; 
     if(RegOpenKey(HKEY_LOCAL_MACHINE,subKey,&hKey) == ERROR_SUCCESS) 
     { 
      WriteToLog("Opened the Registry Key"); 
     dwType = REG_SZ; 
     if(RegQueryValueEx(hKey,"path",0, &dwType, (BYTE*)buf, &dwBufSize) == ERROR_SUCCESS) 
     { 
     ss = buf;  
     WriteToLog(ss.c_str()); 
     } 
     else 
     { 
     WriteToLog(" Cound not find the value"); 

     }   
     RegCloseKey(hKey); 
     } 
     else 
     { 
      WriteToLog(" Cannot Open the Local App Data Path"); 

     } 

     return ss; 
    } 
+0

你在爲微軟工作嗎?如果你不這樣做,你可能不應該在'Software \ Microsoft'鍵下寫字。它有點讓我懷疑你的動機。 – 2014-12-10 18:41:32

+0

對不起,我只是檢查方法是否有效。我將用我自己的路徑更新註冊表。 – Pant 2014-12-11 02:25:13

相關問題