diff --git a/include/WinStd/Base64.h b/include/WinStd/Base64.h index 66e47e0b..7392fa6e 100644 --- a/include/WinStd/Base64.h +++ b/include/WinStd/Base64.h @@ -86,7 +86,7 @@ namespace winstd if (i >= size) break; - buf[num++] = ((unsigned char*)data)[i]; + buf[num++] = reinterpret_cast(data)[i]; } // If this is the last block, flush the buffer. diff --git a/include/WinStd/Common.h b/include/WinStd/Common.h index ed1cbd44..7eca909d 100644 --- a/include/WinStd/Common.h +++ b/include/WinStd/Common.h @@ -390,7 +390,7 @@ template inline DWORD FormatMessage(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _Out_ std::basic_string &str, _In_opt_ va_list *Arguments) { std::unique_ptr > lpBuffer; - DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, (LPSTR)&lpBuffer, 0, Arguments); + DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast(&lpBuffer._Myptr), 0, Arguments); if (dwResult) str.assign(lpBuffer.get(), dwResult); return dwResult; @@ -401,7 +401,7 @@ template inline DWORD FormatMessage(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _Out_ std::basic_string &str, _In_opt_ va_list *Arguments) { std::unique_ptr > lpBuffer; - DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, (LPWSTR)&lpBuffer, 0, Arguments); + DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast(&lpBuffer._Myptr), 0, Arguments); if (dwResult) str.assign(lpBuffer.get(), dwResult); return dwResult; diff --git a/include/WinStd/Cred.h b/include/WinStd/Cred.h index 104f24ea..4c2f1719 100644 --- a/include/WinStd/Cred.h +++ b/include/WinStd/Cred.h @@ -162,14 +162,14 @@ inline BOOL CredProtectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszCredentials, _In_ DWO DWORD dwSize = _countof(buf); // Try with the stack buffer first. - if (CredProtectA(fAsSelf, (LPSTR)pszCredentials, cchCredentials, buf, &dwSize, ProtectionType)) { + if (CredProtectA(fAsSelf, const_cast(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) { // Copy from stack. sProtectedCredentials.assign(buf, dwSize - 1); return TRUE; } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Allocate on heap and retry. auto buf = std::unique_ptr<_Elem[]>(new _Elem[dwSize]); - if (CredProtectA(fAsSelf, (LPSTR)pszCredentials, cchCredentials, buf.get(), &dwSize, ProtectionType)) { + if (CredProtectA(fAsSelf, const_cast(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) { sProtectedCredentials.assign(buf.get(), dwSize - 1); return TRUE; } @@ -186,14 +186,14 @@ inline BOOL CredProtectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszCredentials, _In_ DW DWORD dwSize = _countof(buf); // Try with the stack buffer first. - if (CredProtectW(fAsSelf, (LPWSTR)pszCredentials, cchCredentials, buf, &dwSize, ProtectionType)) { + if (CredProtectW(fAsSelf, const_cast(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) { // Copy from stack. sProtectedCredentials.assign(buf, dwSize - 1); return TRUE; } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Allocate on heap and retry. auto buf = std::unique_ptr<_Elem[]>(new _Elem[dwSize]); - if (CredProtectW(fAsSelf, (LPWSTR)pszCredentials, cchCredentials, buf.get(), &dwSize, ProtectionType)) { + if (CredProtectW(fAsSelf, const_cast(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) { sProtectedCredentials.assign(buf.get(), dwSize - 1); return TRUE; } @@ -210,14 +210,14 @@ inline BOOL CredUnprotectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszProtectedCredential DWORD dwSize = _countof(buf); // Try with the stack buffer first. - if (CredUnprotectA(fAsSelf, (LPSTR)pszProtectedCredentials, cchCredentials, buf, &dwSize)) { + if (CredUnprotectA(fAsSelf, const_cast(pszProtectedCredentials), cchCredentials, buf, &dwSize)) { // Copy from stack. sCredentials.assign(buf, dwSize); return TRUE; } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Allocate on heap and retry. auto buf = std::unique_ptr<_Elem[]>(new _Elem[dwSize]); - if (CredUnprotectA(fAsSelf, (LPSTR)pszProtectedCredentials, cchCredentials, buf.get(), &dwSize)) { + if (CredUnprotectA(fAsSelf, const_cast(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) { sCredentials.assign(buf.get(), dwSize); return TRUE; } @@ -234,14 +234,14 @@ inline BOOL CredUnprotectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszProtectedCredentia DWORD dwSize = _countof(buf); // Try with the stack buffer first. - if (CredUnprotectW(fAsSelf, (LPWSTR)pszProtectedCredentials, cchCredentials, buf, &dwSize)) { + if (CredUnprotectW(fAsSelf, const_cast(pszProtectedCredentials), cchCredentials, buf, &dwSize)) { // Copy from stack. sCredentials.assign(buf, dwSize); return TRUE; } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Allocate on heap and retry. auto buf = std::unique_ptr<_Elem[]>(new _Elem[dwSize]); - if (CredUnprotectW(fAsSelf, (LPWSTR)pszProtectedCredentials, cchCredentials, buf.get(), &dwSize)) { + if (CredUnprotectW(fAsSelf, const_cast(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) { sCredentials.assign(buf.get(), dwSize); return TRUE; } diff --git a/include/WinStd/ETW.h b/include/WinStd/ETW.h index a40cb53a..d16eeca2 100644 --- a/include/WinStd/ETW.h +++ b/include/WinStd/ETW.h @@ -565,7 +565,7 @@ namespace winstd /// \param[in] prop Session properties /// inline event_session(_In_opt_ handle_type h, _In_ const EVENT_TRACE_PROPERTIES *prop) : - m_prop((EVENT_TRACE_PROPERTIES*)new char[prop->Wnode.BufferSize]), + m_prop(reinterpret_cast(new char[prop->Wnode.BufferSize])), handle(h) { memcpy(m_prop.get(), prop, prop->Wnode.BufferSize); @@ -626,7 +626,7 @@ namespace winstd inline LPCTSTR name() const { const EVENT_TRACE_PROPERTIES *prop = m_prop.get(); - return (LPCTSTR)((const char*)prop + prop->LoggerNameOffset); + return reinterpret_cast(reinterpret_cast(prop) + prop->LoggerNameOffset); } @@ -657,7 +657,7 @@ namespace winstd inline ULONG create(_In_ LPCTSTR SessionName, _In_ const EVENT_TRACE_PROPERTIES *Properties) { handle_type h; - std::unique_ptr prop((EVENT_TRACE_PROPERTIES*)new char[Properties->Wnode.BufferSize]); + std::unique_ptr prop(reinterpret_cast(new char[Properties->Wnode.BufferSize])); memcpy(prop.get(), Properties, Properties->Wnode.BufferSize); ULONG ulRes = StartTrace(&h, SessionName, prop.get()); if (ulRes == ERROR_SUCCESS) @@ -1067,12 +1067,12 @@ inline ULONG TdhGetEventInformation(_In_ PEVENT_RECORD pEvent, _In_ ULONG TdhCon ulResult = TdhGetEventInformation(pEvent, TdhContextCount, pTdhContext, (PTRACE_EVENT_INFO)szBuffer, &ulSize); if (ulResult == ERROR_SUCCESS) { // Copy from stack. - info.reset((PTRACE_EVENT_INFO)new char[ulSize]); + info.reset(reinterpret_cast(new char[ulSize])); memcpy(info.get(), szBuffer, ulSize); return ERROR_SUCCESS; } else if (ulResult == ERROR_INSUFFICIENT_BUFFER) { // Create buffer on heap and retry. - info.reset((PTRACE_EVENT_INFO)new char[ulSize]); + info.reset(reinterpret_cast(new char[ulSize])); return TdhGetEventInformation(pEvent, TdhContextCount, pTdhContext, info.get(), &ulSize); } @@ -1089,12 +1089,12 @@ inline ULONG TdhGetEventMapInformation(_In_ PEVENT_RECORD pEvent, _In_ LPWSTR pM ulResult = TdhGetEventMapInformation(pEvent, pMapName, (PEVENT_MAP_INFO)szBuffer, &ulSize); if (ulResult == ERROR_SUCCESS) { // Copy from stack. - info.reset((PEVENT_MAP_INFO)new char[ulSize]); + info.reset(reinterpret_cast(new char[ulSize])); memcpy(info.get(), szBuffer, ulSize); return ERROR_SUCCESS; } else if (ulResult == ERROR_INSUFFICIENT_BUFFER) { // Create buffer on heap and retry. - info.reset((PEVENT_MAP_INFO)new char[ulSize]); + info.reset(reinterpret_cast(new char[ulSize])); return TdhGetEventMapInformation(pEvent, pMapName, info.get(), &ulSize); } @@ -1112,7 +1112,7 @@ inline ULONG TdhGetProperty(_In_ PEVENT_RECORD pEvent, _In_ ULONG TdhContextCoun if (ulResult == ERROR_SUCCESS) { // Query property value. aData.resize((ulSize + sizeof(_Ty) - 1) / sizeof(_Ty)); - ulResult = TdhGetProperty(pEvent, TdhContextCount, pTdhContext, PropertyDataCount, pPropertyData, ulSize, (LPBYTE)aData.data()); + ulResult = TdhGetProperty(pEvent, TdhContextCount, pTdhContext, PropertyDataCount, pPropertyData, ulSize, reinterpret_cast(aData.data())); } return ulResult; diff --git a/include/WinStd/Hex.h b/include/WinStd/Hex.h index e9d10898..56c8829f 100644 --- a/include/WinStd/Hex.h +++ b/include/WinStd/Hex.h @@ -78,7 +78,7 @@ namespace winstd // Convert data character by character. for (size_t i = 0; i < size; i++) { unsigned char - x = ((unsigned char*)data)[i], + x = reinterpret_cast(data)[i], x_h = ((x & 0xf0) >> 4), x_l = ((x & 0x0f) ); diff --git a/include/WinStd/MSI.h b/include/WinStd/MSI.h index d99cd4f5..19a2d9cb 100644 --- a/include/WinStd/MSI.h +++ b/include/WinStd/MSI.h @@ -293,7 +293,7 @@ inline UINT MsiRecordReadStream(_In_ MSIHANDLE hRecord, _In_ unsigned int iField uiResult = ::MsiRecordReadStream(hRecord, iField, NULL, &dwSize); if (uiResult == ERROR_SUCCESS) { binData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty)); - return ::MsiRecordReadStream(hRecord, iField, (char*)binData.data(), &dwSize); + return ::MsiRecordReadStream(hRecord, iField, reinterpret_cast(binData.data()), &dwSize); } else { // Return error code. return uiResult; diff --git a/include/WinStd/Sec.h b/include/WinStd/Sec.h index b685c769..f1077c7d 100644 --- a/include/WinStd/Sec.h +++ b/include/WinStd/Sec.h @@ -255,7 +255,7 @@ namespace winstd handle_type h = new CtxtHandle; ULONG attr; TimeStamp exp; - SECURITY_STATUS res = InitializeSecurityContext(phCredential, NULL, (LPTSTR)pszTargetName, fContextReq, 0, TargetDataRep, pInput, 0, h, pOutput, &attr, &exp); + SECURITY_STATUS res = InitializeSecurityContext(phCredential, NULL, const_cast(pszTargetName), fContextReq, 0, TargetDataRep, pInput, 0, h, pOutput, &attr, &exp); if (SUCCEEDED(res)) { attach(h); m_attrib = attr; @@ -282,7 +282,7 @@ namespace winstd _In_opt_ PSecBufferDesc pInput, _Inout_opt_ PSecBufferDesc pOutput) { - return InitializeSecurityContext(phCredential, m_h, (LPTSTR)pszTargetName, fContextReq, 0, TargetDataRep, pInput, 0, NULL, pOutput, &m_attrib, &m_expires); + return InitializeSecurityContext(phCredential, m_h, const_cast(pszTargetName), fContextReq, 0, TargetDataRep, pInput, 0, NULL, pOutput, &m_attrib, &m_expires); } protected: diff --git a/include/WinStd/Win.h b/include/WinStd/Win.h index 59209fbe..af29fc74 100644 --- a/include/WinStd/Win.h +++ b/include/WinStd/Win.h @@ -945,10 +945,10 @@ inline LSTATUS RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCSTR pszName, _Out_ if (dwType == REG_SZ || dwType == REG_MULTI_SZ) { // The value is REG_SZ or REG_MULTI_SZ. dwSize /= sizeof(CHAR); - sValue.assign((LPCSTR)aStackBuffer, dwSize && ((LPCSTR)aStackBuffer)[dwSize - 1] == 0 ? dwSize - 1 : dwSize); + sValue.assign(reinterpret_cast(aStackBuffer), dwSize && reinterpret_cast(aStackBuffer)[dwSize - 1] == 0 ? dwSize - 1 : dwSize); } else if (dwType == REG_EXPAND_SZ) { // The value is REG_EXPAND_SZ. Expand it from stack buffer. - if (::ExpandEnvironmentStringsA((LPCSTR)aStackBuffer, sValue) == 0) + if (::ExpandEnvironmentStringsA(reinterpret_cast(aStackBuffer), sValue) == 0) lResult = ::GetLastError(); } else { // The value is not a string type. @@ -958,7 +958,7 @@ inline LSTATUS RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCSTR pszName, _Out_ if (dwType == REG_SZ || dwType == REG_MULTI_SZ) { // The value is REG_SZ or REG_MULTI_SZ. Read it now. auto szBuffer = std::unique_ptr(new CHAR[dwSize / sizeof(CHAR)]); - if ((lResult = ::RegQueryValueExA(hReg, pszName, NULL, NULL, (LPBYTE)szBuffer.get(), &dwSize)) == ERROR_SUCCESS) { + if ((lResult = ::RegQueryValueExA(hReg, pszName, NULL, NULL, reinterpret_cast(szBuffer.get()), &dwSize)) == ERROR_SUCCESS) { dwSize /= sizeof(CHAR); sValue.assign(szBuffer.get(), dwSize && szBuffer[dwSize - 1] == 0 ? dwSize - 1 : dwSize); } else @@ -966,7 +966,7 @@ inline LSTATUS RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCSTR pszName, _Out_ } else if (dwType == REG_EXPAND_SZ) { // The value is REG_EXPAND_SZ. Read it and expand environment variables. auto szBuffer = std::unique_ptr(new CHAR[dwSize / sizeof(CHAR)]); - if ((lResult = ::RegQueryValueExA(hReg, pszName, NULL, NULL, (LPBYTE)szBuffer.get(), &dwSize)) == ERROR_SUCCESS) { + if ((lResult = ::RegQueryValueExA(hReg, pszName, NULL, NULL, reinterpret_cast(szBuffer.get()), &dwSize)) == ERROR_SUCCESS) { if (::ExpandEnvironmentStringsA(szBuffer.get(), sValue) == 0) lResult = ::GetLastError(); } else @@ -994,10 +994,10 @@ inline LSTATUS RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCWSTR pszName, _Out_ if (dwType == REG_SZ || dwType == REG_MULTI_SZ) { // The value is REG_SZ or REG_MULTI_SZ. dwSize /= sizeof(WCHAR); - sValue.assign((LPCWSTR)aStackBuffer, dwSize && ((LPCWSTR)aStackBuffer)[dwSize - 1] == 0 ? dwSize - 1 : dwSize); + sValue.assign(reinterpret_cast(aStackBuffer), dwSize && reinterpret_cast(aStackBuffer)[dwSize - 1] == 0 ? dwSize - 1 : dwSize); } else if (dwType == REG_EXPAND_SZ) { // The value is REG_EXPAND_SZ. Expand it from stack buffer. - if (::ExpandEnvironmentStringsW((LPCWSTR)aStackBuffer, sValue) == 0) + if (::ExpandEnvironmentStringsW(reinterpret_cast(aStackBuffer), sValue) == 0) lResult = ::GetLastError(); } else { // The value is not a string type. @@ -1007,7 +1007,7 @@ inline LSTATUS RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCWSTR pszName, _Out_ if (dwType == REG_SZ || dwType == REG_MULTI_SZ) { // The value is REG_SZ or REG_MULTI_SZ. Read it now. auto szBuffer = std::unique_ptr(new WCHAR[dwSize / sizeof(WCHAR)]); - if ((lResult = ::RegQueryValueExW(hReg, pszName, NULL, NULL, (LPBYTE)szBuffer.get(), &dwSize)) == ERROR_SUCCESS) { + if ((lResult = ::RegQueryValueExW(hReg, pszName, NULL, NULL, reinterpret_cast(szBuffer.get()), &dwSize)) == ERROR_SUCCESS) { dwSize /= sizeof(WCHAR); sValue.assign(szBuffer.get(), dwSize && szBuffer[dwSize - 1] == 0 ? dwSize - 1 : dwSize); } else @@ -1015,7 +1015,7 @@ inline LSTATUS RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCWSTR pszName, _Out_ } else if (dwType == REG_EXPAND_SZ) { // The value is REG_EXPAND_SZ. Read it and expand environment variables. auto szBuffer = std::unique_ptr(new WCHAR[dwSize / sizeof(WCHAR)]); - if ((lResult = ::RegQueryValueExW(hReg, pszName, NULL, NULL, (LPBYTE)szBuffer.get(), &dwSize)) == ERROR_SUCCESS) { + if ((lResult = ::RegQueryValueExW(hReg, pszName, NULL, NULL, reinterpret_cast(szBuffer.get()), &dwSize)) == ERROR_SUCCESS) { if (::ExpandEnvironmentStringsW(szBuffer.get(), sValue) == 0) lResult = ::GetLastError(); } else @@ -1187,7 +1187,7 @@ inline int WINAPI LoadString(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_ { // Get read-only pointer to string resource. LPCSTR pszStr; - int i = LoadStringA(hInstance, uID, (LPSTR)&pszStr, 0); + int i = LoadStringA(hInstance, uID, reinterpret_cast(&pszStr), 0); if (i) { sBuffer.assign(pszStr, i); return i; @@ -1201,7 +1201,7 @@ inline int WINAPI LoadString(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_ { // Get read-only pointer to string resource. LPCWSTR pszStr; - int i = LoadStringW(hInstance, uID, (LPWSTR)&pszStr, 0); + int i = LoadStringW(hInstance, uID, reinterpret_cast(&pszStr), 0); if (i) { sBuffer.assign(pszStr, i); return i; diff --git a/src/ETW.cpp b/src/ETW.cpp index 7d4ca25a..d2ce8bc0 100644 --- a/src/ETW.cpp +++ b/src/ETW.cpp @@ -37,17 +37,17 @@ const winstd::event_data winstd::event_data::blank; winstd::event_rec::~event_rec() { if (ExtendedData) - delete (unsigned char*)ExtendedData; + delete reinterpret_cast(ExtendedData); if (UserData) - delete (unsigned char*)UserData; + delete reinterpret_cast(UserData); } void winstd::event_rec::set_extended_data(_In_ USHORT count, _In_count_(count) const EVENT_HEADER_EXTENDED_DATA_ITEM *data) { if (ExtendedData) - delete (unsigned char*)ExtendedData; + delete reinterpret_cast(ExtendedData); set_extended_data_internal(count, data); } @@ -56,7 +56,7 @@ void winstd::event_rec::set_extended_data(_In_ USHORT count, _In_count_(count) c void winstd::event_rec::set_user_data(_In_ USHORT size, _In_bytecount_(size) LPCVOID data) { if (UserData) - delete (unsigned char*)UserData; + delete reinterpret_cast(UserData); set_user_data_internal(size, data); } @@ -73,13 +73,13 @@ void winstd::event_rec::set_extended_data_internal(_In_ USHORT count, _In_count_ data_size += data[i].DataSize; // Allocate memory for extended data. - ExtendedData = (EVENT_HEADER_EXTENDED_DATA_ITEM*)(new unsigned char[sizeof(EVENT_HEADER_EXTENDED_DATA_ITEM)*count + data_size]); + ExtendedData = reinterpret_cast(new unsigned char[sizeof(EVENT_HEADER_EXTENDED_DATA_ITEM)*count + data_size]); // Bulk-copy extended data descriptors. memcpy(ExtendedData, data, sizeof(EVENT_HEADER_EXTENDED_DATA_ITEM) * count); // Copy the data. - unsigned char *ptr = (unsigned char*)(ExtendedData + count); + unsigned char *ptr = reinterpret_cast(ExtendedData + count); for (size_t i = 0; i < count; i++) { if (data[i].DataSize) { memcpy(ptr, (void*)(data[i].DataPtr), data[i].DataSize);