我正在使用Windows中的一些注册表函数(RegOpenKeyEx
、RegQueryInfoKey
和RegEnumValue
),假设这是我到目前为止拥有的代码:
const int MAX_VALUE_NAME= 16383;
const int MAX_DATA = 16383;
DWORD i;
HKEY hKey = HKEY_CURRENT_USER;
LPCTSTR lpSubKey = TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU");
DWORD ulOptions = 0;
REGSAM samDesired = KEY_READ | KEY_WRITE | KEY_QUERY_VALUE;
HKEY phkResult;
DWORD dwIndex = 0;
TCHAR lpValueName[MAX_VALUE_NAME];
LPCTSTR ValueMRU;
BYTE *DataMRU;
DWORD lpcchValueName = MAX_VALUE_NAME;
BYTE lpData[MAX_DATA+2];
DWORD cbData = sizeof(lpData);
DWORD type;
TCHAR lpClass[MAX_PATH] = TEXT("");
DWORD lpcClass = MAX_PATH;
LPDWORD lpReserved = NULL;
DWORD lpcSubKeys = 0;
DWORD lpcMaxSubKeyLen;
DWORD lpcMaxClassLen;
DWORD lpcValues;
DWORD lpcMaxValueNameLen;
DWORD lpcMaxValueLen;
DWORD lpcbSecurityDescriptor;
FILETIME lpfLastWriteTime;
char *pMsg = NULL;
long R;
long OpenK = RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, &phkResult);
if (OpenK == ERROR_SUCCESS)
{
long Query = RegQueryInfoKey(phkResult, lpClass, &lpcClass, lpReserved, &lpcSubKeys, &lpcMaxSubKeyLen,
&lpcMaxClassLen, &lpcValues, &lpcMaxValueLen, &lpcMaxValueLen, &lpcbSecurityDescriptor, &lpfLastWriteTime);
if (Query == ERROR_SUCCESS)
{
if (lpcValues)
{
cout << "Number of values in the subkey: " << lpcValues << endl;
cout << "\nThe values are:: \n" << endl;
//Show RunMRU values and content.
for (i = 0; i < lpcValues; i++)
{
lpcchValueName = MAX_VALUE_NAME;
//lpValueName[0] = '\0';
lpValueName[MAX_VALUE_NAME];
//lpData[MAX_DATA + 2];
cbData = MAX_DATA;
R = RegEnumValue(phkResult, i, lpValueName, &lpcchValueName, NULL,
&type, lpData, &cbData);
int Quantity = strlen((const char *)lpData);
if (R != ERROR_NO_MORE_ITEMS)
{
cout << "\0" << lpValueName << ": " << lpData << endl;
cout << "Number of characters: " << Quantity << endl;
cout << "\n";
}
else
{
cout << "Error enumerating values. Code: " << R << endl;
}
}
} //EOIF.
这就是我得到的:
问题如下:
警局。lpData获取值的内容,而不是值。
编辑1: I可以通过将它添加到FOR
中来解决“1”问题
int Quantity = strlen(lpData);
std::string original = lpData;
std::string result = original.substr(0, original.size() - 2);
std::string result = original.substr(0, Quantity - 2);
if (R != ERROR_NO_MORE_ITEMS)
{
cout << "\0" << lpValueName << ": " << (TCHAR*)result.c_str() << endl;
cout << "Numbers of characters: " << Quantity-2 << endl;
cout << "\n";
}
else
{
cout << "Error enumerating the values. Code: " << R << endl;
}
现在,我只需要知道如何避免在FOR中显示MRUList值。
发布于 2013-10-13 15:07:04
char* dest = new char[strlen(lpData) - 2];
memcpy(dest, lpData, strlen(lpData) - 2)
//do stuff with dest..
delete[] dest;
另一种方法是到std::字符串它。
std::string dest = std::string(lpData);
dest.erase(std::find_last_of("\\1"), 2);
https://stackoverflow.com/questions/19346102
复制相似问题