Regedit Screenshot http://i54.tinypic.com/3503vi8.jpg爲什麼我的註冊表讀取程序失敗?
現在,這樣的代碼:
HKEY hKey;
LONG regOpenCriss = RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\", 0, KEY_QUERY_VALUE, &hKey);
char mydata[2000] = {'\0'};
DWORD dwType = REG_SZ;
DWORD dataLength = sizeof(mydata);
LPVOID messagecaliss;
GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), NULL,(LPTSTR) &messagecaliss, 0, NULL);
if (regOpenCriss == ERROR_SUCCESS) {
RegQueryValueEx(hKey, "Test2", 0, &dwType, (LPBYTE)&mydata, &dataLength); //change the key you want to read
printf("%s\n", mydata);
system("PAUSE");
RegCloseKey(hKey);
}
else
MessageBox(NULL,(LPCTSTR)messagecaliss,"ERROR",MB_OK|MB_ICONINFORMATION);
printf("%s\t\n", mydata);
std::string FullPath(mydata,dataLength-1);
printf("%s\n", FullPath);
std::string FileName = GetFileNameFromPath(mydata);
printf("%s\n", FileName);
system("PAUSE");
功能GetFilenameFromPath定義爲:
std::string GetFileNameFromPath (std::string str) {
size_t found;
found=str.find_last_of("/\\");
return str.substr(found+1);}
當我打電話的RegQueryValueEx用 「QKSMTPServer3」 作爲第二個參數,這裏是輸出:
C:\Program Files (x86)\QK SMTP Server 3\QKSmtpServer3.exe
Press any key to continue . . .
C:\Program Files (x86)\QK SMTP Server 3\QKSmtpServer3.exe
C:\Program Files (x86)\QK SMTP Server 3\QKSmtpServer3.exe
QKSmtpServer3.exe
Press any key to continue . . .
這是我想要的。現在,當我使用「Test2」調用RegQueryValueEx時,得到:
C:\Test.exe
Press any key to continue . . .
C:\Test.exe
並且程序崩潰。任何想法爲什麼?
非常感謝您
函數GetFileNameFromPath取自http://www.cplusplus.com/reference/string/string/find_last_of/ – 2011-06-13 03:23:08