2012-09-10 33 views
2

我遇到的問題是我的應用程序輸入不正確的數據到註冊表(而不是文件路徑,它輸入字符,如「㩃啜敳獲啜敳屲灁」)我不知道問題是什麼,相關的源代碼如下(包括等已被刪除)。RegSetValueEx輸入不正確的數據到註冊表

stringstream ss; 

char* file_path = getenv("APPDATA"); 
strcat(file_path, "\\Application.exe"); 
ss << file_path; 
ss >> file_path_string; 
CA2W unicodeFile_path(file_path); 
cout << "Downloading File"; 
HRESULT hr = URLDownloadToFile (NULL, _T("http://example.com/application.exe"), unicodeFile_path, 0, NULL); 
cout << "Done" << endl; 
cout << "Adding to registry" << endl; 
HKEY hKey; 
CA2W registryLocation("Software\\Microsoft\\Windows\\CurrentVersion\\Run"); 
CA2W registryKey("Application"); 
// Check registry if exists, otherwise create. 
RegCreateKeyEx(HKEY_CURRENT_USER, 
       registryLocation, 
       0, 
       NULL, 
       REG_OPTION_NON_VOLATILE, 
       KEY_WRITE, 
       NULL, 
       &hk, 
       &dwDisp); 
// Store reg value 
RegSetValueEx(hk, 
       registryKey, 
       NULL, 
       REG_SZ, 
       (const BYTE*)file_path_string.c_str(), 
       file_path_string.size() + 1); 

任何幫助將不勝感激。

+0

你有沒有得到正確的方法來做到這一點? – theLearner

回答

1

RegSetValueEx預計(對於REG_SZ)您傳入的數據是Unicode,除非您未在您的環境中定義UNICODE。但.c_str可能是非Unicode流。