可能重复:
How to compare strings
我希望与注册表字符串值进行比较,如果它们相同则会出现一个消息框
目前我正在使用这个函数,它正确地返回值,但每当我想比较它们时,比较结果总是错误的
char* GetRegistry(char* StringName)
{
DWORD dwType = REG_SZ;
HKEY hKey = 0;
char value[1024];
DWORD value_length = 1024;
const char* subkey = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\MCI\\Player";
RegOpenKey(HKEY_LOCAL_MACHINE,subkey,&hKey);
RegQueryValueEx(hKey, StringName, NULL, &dwType, (LPBYTE)&value, &value_length);
return value;
}
我用它来比较它们
if (GetRegistry("First") == GetRegistry("Second"))
{
MessageBox(NULL,":|",":|",1);
}
但MessageBox出现的价值是不同的
任何帮助表示赞赏。
答案 0 :(得分:5)
通过使用std::string
,比较将按预期运行。这也将修复该函数返回指向本地缓冲区的指针的另一个错误。
std::string GetRegistry(const char* StringName)
{
....
return std::string(value);
}
答案 1 :(得分:4)
GetRegistry()
会返回char*
,因此您实际上是将指针与operator==
进行比较。
您应该使用strcmp()
进行原始C类char*
字符串比较,或者更好地使用强大的C ++字符串类,例如CString
或std::[w]string
。
以下是使用ATL的CString
:
#include <atlbase.h>
#include <atlstr.h>
CString GetRegistry(LPCTSTR pszValueName)
{
// Try open registry key
HKEY hKey = NULL;
LPCTSTR pszSubkey = _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\MCI Extensions");
if ( RegOpenKey(HKEY_LOCAL_MACHINE, pszSubkey, &hKey) != ERROR_SUCCESS )
{
// Error:
// throw an exception or something...
//
// (In production code a custom C++ exception
// derived from std::runtime_error could be used)
AtlThrowLastWin32();
}
// Buffer to store string read from registry
TCHAR szValue[1024];
DWORD cbValueLength = sizeof(szValue);
// Query string value
if ( RegQueryValueEx(
hKey,
pszValueName,
NULL,
NULL,
reinterpret_cast<LPBYTE>(&szValue),
&cbValueLength)
!= ERROR_SUCCESS )
{
// Error
// throw an exception or something...
AtlThrowLastWin32();
}
// Create a CString from the value buffer
return CString(szValue);
}
然后你可以这样称呼它:
if ( GetRegistry(_T("First")) == GetRegistry(_T("Second")) )
...
请注意,此代码将在ANSI / MBCS和Unicode版本中编译(它基于Win32 TCHAR
模型)。
答案 2 :(得分:0)
此源代码存在一些问题。
首先,你有一个带有局部变量的函数,一个堆栈上的变量,它返回该变量的地址,但是当函数返回时,变量消失,地址不再有效。
下一个问题是你没有比较字符串。您正在比较函数返回的地址,如果幸运的话,地址可能是相同的。由于您连续两次调用该函数,因此您很幸运,因此地址是相同的。
我建议您执行以下操作:(1)在函数中创建两个本地字符串,调用函数GetRegistry()
和(2)修改GetRegistry()
函数,以便它使用这些缓冲区而不是比它自己的。因此代码看起来像:
char registryEntryOne[1024];
char registryEntryTwo[1024];
DWORD dwRegistryEntryOneLen;
DWORD dwRegistryEntryTwoLen;
registryEntryOne[0] = 0; // init the registry entry to zero length string
registryEntryTwo[0] = 0;
dwRegistryEntryOneLen = sizeof(registryEntryOne);
GetRegistry ("First", registryEntryOne, &dwRegistryEntryOneLen);
dwRegistryEntryTwoLen = sizeof(registryEntryTwo);
GetRegistry ("Second", registryEntryTwo, &dwRegistryEntryTwoLen);
// two strings are equal if:
// the lengths are the same
// at least one of the lengths is non-zero
// the bytes are the same in the same order
if (dwRegistryEntryOneLen && dwRegistryEntryOneLen == dwRegistryEntryTwoLen && memcmp (registryEntryOne, registryEntryTwo, dwRegistryEntryOneLen) == 0) {
// strings are equal
} else {
// strings are not equal
}
GetRegistry()函数类似于:
char* GetRegistry(char* StringName, char *valueBuffer, DWORD *value_length)
{
DWORD dwType = REG_SZ;
HKEY hKey = 0;
const char* subkey = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\MCI\\Player";
RegOpenKey(HKEY_LOCAL_MACHINE,subkey,&hKey);
RegQueryValueEx(hKey, StringName, NULL, &dwType, (LPBYTE)valueBuffer, value_length);
return valueBuffer;
}