J
John paul coder
Guest
Hello All,
When I compile the below program, at the end of the function when returning (return bRetVal, I am getting the below warning message:
Warning C4800 'BOOL': forcing value to bool 'true' or 'false' (performance warning)
Is it because ReadEmail function returns "bool" and bRetVal has of type "BOOL"?
Can anyone please help me on this?
And also I am seeing the same warning message when calling return bRetVal; after returning the ErrorCode like as shown in the below code snippet:
ErrorCode = GetLastError();
MYLOG(L" RegOpenCurrentUser failed %u", ErrorCode);
return bRetVal;
Now after the ErrorCode call I replaced (return bRetVal with (return FALSE with this it is not showing that warning.
Below is the complete code snippet:
bool MyReader::ReadEmail()
{
HANDLE hToken = NULL;
HKEY hkey;
BOOL bRetVal = FALSE;
DWORD ErrorCode;
PWTS_SESSION_INFO pSessionInfo = 0;
DWORD dwCount = 0;
int dwSessionId = 0;
// Get the list of all terminal sessions
if (WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1,
&pSessionInfo, &dwCount))
{
// look over obtained list in search of the active session
for (DWORD i = 0; i < dwCount; ++i)
{
WTS_SESSION_INFO si = pSessionInfo;
if (WTSActive == si.State)
{
// If the current session is active – store its ID
dwSessionId = si.SessionId;
break;
}
}
WTSFreeMemory(pSessionInfo);
// Get token of the logged in user by the active session ID
if (dwSessionId)
{
bRetVal = WTSQueryUserToken(dwSessionId, &hToken);
}
else
{
ErrorCode = GetLastError();
MYLOG(L" Get WTSEnumerateSessions failed - %u", ErrorCode);
return FALSE; //return bRetVal;
}
}
else
{
ErrorCode = GetLastError();
MYLOG(L"Get WTSEnumerateSessions failed - %u", ErrorCode);
return FALSE; //return bRetVal;
}
if (bRetVal)
{
bRetVal = ImpersonateLoggedOnUser(hToken);
if (bRetVal)
{
if (ERROR_SUCCESS == RegOpenCurrentUser(KEY_READ, &hkey))
{
WCHAR szValue[MAX_PATH] = { 0 };
DWORD dwDataSize = sizeof(szValue) / sizeof(WCHAR);
wstring strRegPath = m_wszParams;
wstring strKey = EMAIL;
MyIOStruct myIOstructObj;
McIOManager IOManager;
DWORD nPos = strRegPath.find(REG_SOFTWARE);
wstring strKeyPath = strRegPath.substr(nPos, strRegPath.length());
wstring strRoot = strRegPath.substr(0, nPos - 1);
HKEY hKey;
std::wstring value;
MyStorageType myStorageTypeObj = MyStorageType::eRegistry;
myIOstructObj.hkeyRoot = hkey;
myIOstructObj.lpszSubKey = (LPWSTR)strKeyPath.c_str();
myIOstructObj.dwType = REG_SZ;
myIOstructObj.cbData = dwDataSize;
myIOstructObj.dwFlags = 0;
long lRes = 0;
myIOstructObj.lpszValueName = (LPWSTR)EMAIL;
memset(szValue, 0, MAX_PATH);
myIOstructObj.lpData = (LPBYTE)&szValue[0];
lRes = IOManager.ReadValue(myStorageTypeObj, myIOstructObj);
value.clear();
if ((LPWSTR)myIOstructObj.lpData == nullptr)
{
value.assign(L"");
}
else
{
value.assign((LPWSTR)myIOstructObj.lpData);
}
{
m_ReponsePair.insert(make_pair(L"email", value));
}
}
else
{
ErrorCode = GetLastError();
MYLOG(L" RegOpenCurrentUser failed %u", ErrorCode);
return bRetVal;
}
}
else
{
ErrorCode = GetLastError();
MYLOG(L" Impersonating logged on user failed %u", ErrorCode);
if (hToken != NULL)
{
::CloseHandle(hToken);
}
return bRetVal;
}
}
else
{
MYLOG(L" Query user token %u", GetLastError());
if (hToken != NULL)
{
::CloseHandle(hToken);
}
return 0;
}
RevertToSelf();
if (hToken != NULL)
::CloseHandle(hToken);
if (hkey != NULL)
::CloseHandle(hkey);
return bRetVal;
}
Thanks in advance.
Continue reading...
When I compile the below program, at the end of the function when returning (return bRetVal, I am getting the below warning message:
Warning C4800 'BOOL': forcing value to bool 'true' or 'false' (performance warning)
Is it because ReadEmail function returns "bool" and bRetVal has of type "BOOL"?
Can anyone please help me on this?
And also I am seeing the same warning message when calling return bRetVal; after returning the ErrorCode like as shown in the below code snippet:
ErrorCode = GetLastError();
MYLOG(L" RegOpenCurrentUser failed %u", ErrorCode);
return bRetVal;
Now after the ErrorCode call I replaced (return bRetVal with (return FALSE with this it is not showing that warning.
Below is the complete code snippet:
bool MyReader::ReadEmail()
{
HANDLE hToken = NULL;
HKEY hkey;
BOOL bRetVal = FALSE;
DWORD ErrorCode;
PWTS_SESSION_INFO pSessionInfo = 0;
DWORD dwCount = 0;
int dwSessionId = 0;
// Get the list of all terminal sessions
if (WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1,
&pSessionInfo, &dwCount))
{
// look over obtained list in search of the active session
for (DWORD i = 0; i < dwCount; ++i)
{
WTS_SESSION_INFO si = pSessionInfo;
if (WTSActive == si.State)
{
// If the current session is active – store its ID
dwSessionId = si.SessionId;
break;
}
}
WTSFreeMemory(pSessionInfo);
// Get token of the logged in user by the active session ID
if (dwSessionId)
{
bRetVal = WTSQueryUserToken(dwSessionId, &hToken);
}
else
{
ErrorCode = GetLastError();
MYLOG(L" Get WTSEnumerateSessions failed - %u", ErrorCode);
return FALSE; //return bRetVal;
}
}
else
{
ErrorCode = GetLastError();
MYLOG(L"Get WTSEnumerateSessions failed - %u", ErrorCode);
return FALSE; //return bRetVal;
}
if (bRetVal)
{
bRetVal = ImpersonateLoggedOnUser(hToken);
if (bRetVal)
{
if (ERROR_SUCCESS == RegOpenCurrentUser(KEY_READ, &hkey))
{
WCHAR szValue[MAX_PATH] = { 0 };
DWORD dwDataSize = sizeof(szValue) / sizeof(WCHAR);
wstring strRegPath = m_wszParams;
wstring strKey = EMAIL;
MyIOStruct myIOstructObj;
McIOManager IOManager;
DWORD nPos = strRegPath.find(REG_SOFTWARE);
wstring strKeyPath = strRegPath.substr(nPos, strRegPath.length());
wstring strRoot = strRegPath.substr(0, nPos - 1);
HKEY hKey;
std::wstring value;
MyStorageType myStorageTypeObj = MyStorageType::eRegistry;
myIOstructObj.hkeyRoot = hkey;
myIOstructObj.lpszSubKey = (LPWSTR)strKeyPath.c_str();
myIOstructObj.dwType = REG_SZ;
myIOstructObj.cbData = dwDataSize;
myIOstructObj.dwFlags = 0;
long lRes = 0;
myIOstructObj.lpszValueName = (LPWSTR)EMAIL;
memset(szValue, 0, MAX_PATH);
myIOstructObj.lpData = (LPBYTE)&szValue[0];
lRes = IOManager.ReadValue(myStorageTypeObj, myIOstructObj);
value.clear();
if ((LPWSTR)myIOstructObj.lpData == nullptr)
{
value.assign(L"");
}
else
{
value.assign((LPWSTR)myIOstructObj.lpData);
}
{
m_ReponsePair.insert(make_pair(L"email", value));
}
}
else
{
ErrorCode = GetLastError();
MYLOG(L" RegOpenCurrentUser failed %u", ErrorCode);
return bRetVal;
}
}
else
{
ErrorCode = GetLastError();
MYLOG(L" Impersonating logged on user failed %u", ErrorCode);
if (hToken != NULL)
{
::CloseHandle(hToken);
}
return bRetVal;
}
}
else
{
MYLOG(L" Query user token %u", GetLastError());
if (hToken != NULL)
{
::CloseHandle(hToken);
}
return 0;
}
RevertToSelf();
if (hToken != NULL)
::CloseHandle(hToken);
if (hkey != NULL)
::CloseHandle(hkey);
return bRetVal;
}
Thanks in advance.
Continue reading...