1
0
Fork 0

Remove XP compatibility code behind _WIN32_WINNT

This commit is contained in:
Duncan Ogilvie 2025-08-17 18:01:29 +02:00
parent bb300b6d30
commit 49ef645cb1
14 changed files with 7 additions and 146 deletions

1
CMakeLists.txt generated
View File

@ -441,6 +441,7 @@ target_link_libraries(dbg PRIVATE
Shlwapi
Ws2_32
Wininet
Iphlpapi
)
if(CMAKE_SIZEOF_VOID_P EQUAL 4) # x86

View File

@ -105,6 +105,7 @@ private-link-libraries = [
"Shlwapi",
"Ws2_32",
"Wininet",
"Iphlpapi",
]
x86.private-link-libraries = [
"src/dbg/dbghelp/dbghelp_x86.lib",

View File

@ -403,7 +403,6 @@ void WaitForMultipleThreadsTermination(const HANDLE* hThread, int count, DWORD t
duint GetThreadCount()
{
duint threadCount = std::thread::hardware_concurrency();
#if (_WIN32_WINNT >= 0x0601) // GetLogicalProcessorInformationEx is supported on Windows 7
DWORD length = 0;
if(GetLogicalProcessorInformationEx(RelationAll, nullptr, &length) || GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
@ -432,6 +431,5 @@ duint GetThreadCount()
}
offset += info->Size;
}
#endif // _WIN32_WINNT >= 0x0600
return threadCount;
}

View File

@ -76,19 +76,11 @@ class SearchTimer
public:
SearchTimer()
{
#if (_WIN32_WINNT >= 0x0600) // GetTickCount64 is not supported on Windows XP
ticks = GetTickCount64();
#else
ticks = GetTickCount();
#endif // _WIN32_WINNT >= 0x0600
}
void StopTimer()
{
#if (_WIN32_WINNT >= 0x0600) // GetTickCount64 is not supported on Windows XP
ticks = GetTickCount64() - ticks;
#else
ticks = GetTickCount() - ticks;
#endif // _WIN32_WINNT >= 0x0600
}
DWORD GetTicks()
{

View File

@ -468,11 +468,7 @@ namespace Exprfunc
duint gettickcount()
{
#if (_WIN32_WINNT >= 0x0600)
return GetTickCount64();
#else
return GetTickCount();
#endif _WIN32_WINNT >= 0x0600
return (duint)GetTickCount64();
}
duint rdtsc()

View File

@ -163,45 +163,15 @@ bool HandlesGetName(HANDLE remoteHandle, String & name, String & typeName)
}
else if(strcmp(typeName.c_str(), "Thread") == 0)
{
DWORD TID, PID;
#if (_WIN32_WINNT >= 0x0600)
TID = GetThreadId(hLocalHandle);
PID = GetProcessIdOfThread(hLocalHandle);
#else
auto getTidPid = [](HANDLE hThread, DWORD & TID, DWORD & PID)
{
static auto pGetThreadId = (DWORD(__stdcall*)(HANDLE))GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetThreadId");
static auto pGetProcessIdOfThread = (DWORD(__stdcall*)(HANDLE))GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetProcessIdOfThread");
if(pGetThreadId != NULL && pGetProcessIdOfThread != NULL) //Vista or Server 2003 only
{
TID = pGetThreadId(hThread);
PID = pGetProcessIdOfThread(hThread);
}
else //Windows XP
{
THREAD_BASIC_INFORMATION threadInfo;
ULONG threadInfoSize = 0;
NTSTATUS isok = NtQueryInformationThread(hThread, ThreadBasicInformation, &threadInfo, sizeof(threadInfo), &threadInfoSize);
if(NT_SUCCESS(isok))
{
TID = (DWORD)(duint)threadInfo.ClientId.UniqueThread;
PID = (DWORD)(duint)threadInfo.ClientId.UniqueProcess;
}
}
};
getTidPid(hLocalHandle, TID, PID);
#endif // _WIN32_WINNT < 0x0600
auto TID = GetThreadId(hLocalHandle);
auto PID = GetProcessIdOfThread(hLocalHandle);
if(TID == 0 || PID == 0) //The first time could fail because the process didn't specify query permissions.
{
HANDLE hLocalQueryHandle;
if(DuplicateHandle(hProcess, remoteHandle, GetCurrentProcess(), &hLocalQueryHandle, THREAD_QUERY_INFORMATION, FALSE, 0))
{
#if (_WIN32_WINNT >= 0x0600)
TID = GetThreadId(hLocalQueryHandle);
PID = GetProcessIdOfThread(hLocalQueryHandle);
#else
getTidPid(hLocalHandle, TID, PID);
#endif // _WIN32_WINNT < 0x0600
CloseHandle(hLocalQueryHandle);
}
}

View File

@ -1,9 +1,6 @@
#include "tcpconnections.h"
#include <WS2tcpip.h>
#if (_WIN32_WINNT >= 0x0600)
#include <iphlpapi.h>
#pragma comment(lib, "iphlpapi.lib")
static const char* TcpStateToString(unsigned int State)
{
@ -37,12 +34,9 @@ static const char* TcpStateToString(unsigned int State)
return "UNKNOWN";
}
}
#endif // _WIN32_WINNT >= 0x0600
bool TcpEnumConnections(duint pid, std::vector<TCPCONNECTIONINFO> & connections)
{
#if (_WIN32_WINNT >= 0x0600)
TCPCONNECTIONINFO info;
wchar_t AddrBuffer[TCP_ADDR_SIZE] = L"";
ULONG ulSize = 0;
@ -106,8 +100,5 @@ bool TcpEnumConnections(duint pid, std::vector<TCPCONNECTIONINFO> & connections)
}
}
}
#else
// This feature requires Windows Vista or greater, so don't compile on Windows XP.
#endif // _WIN32_WINNT >= 0x0600
return true;
}

View File

@ -318,11 +318,7 @@ DWORD ThreadGetId(HANDLE Thread)
}
// Wasn't found, check with Windows
#if (_WIN32_WINNT >= 0x0600) // GetThreadId is not supported on Windows XP
return GetThreadId(Thread);
#else
return 0;
#endif // _WIN32_WINNT >= 0x0600
}
int ThreadSuspendAll()
@ -385,14 +381,10 @@ ULONG64 ThreadQueryCycleTime(HANDLE hThread)
{
ULONG64 CycleTime;
#if (_WIN32_WINNT >= 0x0600) // QueryThreadCycleTime is not supported on Windows XP
if(!QueryThreadCycleTime(hThread, &CycleTime))
CycleTime = 0;
return CycleTime;
#else
return 0;
#endif // _WIN32_WINNT >= 0x0600
}
void ThreadUpdateWaitReasons()

View File

@ -65,20 +65,13 @@ void SectionLockerGlobal::Initialize()
// This gets called on the same thread as the GUI
m_guiMainThreadId = GetCurrentThreadId();
#if (_WIN32_WINNT >= 0x0600)
// Destroy previous data if any existed
memset(m_srwLocks, 0, sizeof(m_srwLocks));
for(int i = 0; i < ARRAYSIZE(m_srwLocks); i++)
InitializeSRWLock(&m_srwLocks[i]);
#else
// Fall back to critical sections otherwise
// Destroy previous data if any existed
memset(m_crLocks, 0, sizeof(m_crLocks));
for(int i = 0; i < ARRAYSIZE(m_crLocks); i++)
InitializeCriticalSection(&m_crLocks[i]);
#endif // _WIN32_WINNT >= 0x0600
m_Initialized = true;
}
@ -87,7 +80,6 @@ void SectionLockerGlobal::Deinitialize()
if(!m_Initialized)
return;
#if (_WIN32_WINNT >= 0x0600)
for(int i = 0; i < ARRAYSIZE(m_srwLocks); i++)
{
// Wait for the lock's ownership to be released
@ -97,18 +89,6 @@ void SectionLockerGlobal::Deinitialize()
// Invalidate data
memset(&m_srwLocks[i], 0, sizeof(SRWLOCK));
}
#else
for(int i = 0; i < ARRAYSIZE(m_crLocks); i++)
{
// Wait for the lock's ownership to be released
EnterCriticalSection(&m_crLocks[i]);
LeaveCriticalSection(&m_crLocks[i]);
// Delete critical section
DeleteCriticalSection(&m_crLocks[i]);
memset(&m_crLocks[i], 0, sizeof(CRITICAL_SECTION));
}
#endif // _WIN32_WINNT >= 0x0600
m_Initialized = false;
}

View File

@ -105,7 +105,6 @@ private:
{
auto threadId = GetCurrentThreadId();
#if (_WIN32_WINNT >= 0x0600)
auto srwLock = &m_srwLocks[LockIndex];
if(Shared)
@ -146,24 +145,11 @@ private:
assert(m_exclusiveOwner[LockIndex].count == 0);
m_exclusiveOwner[LockIndex].threadId = threadId;
m_exclusiveOwner[LockIndex].count = 1;
#else
auto cr = &m_crLocks[LockIndex];
if(ProcessGuiEvents && threadId == m_guiMainThreadId)
{
while(!TryEnterCriticalSection(cr))
GuiProcessEvents();
}
else
{
EnterCriticalSection(cr);
}
#endif // _WIN32_WINNT >= 0x0600
}
template<SectionLock LockIndex, bool Shared>
static void ReleaseLock()
{
#if (_WIN32_WINNT >= 0x0600)
if(Shared)
{
if(m_exclusiveOwner[LockIndex].threadId == GetCurrentThreadId())
@ -181,9 +167,6 @@ private:
m_exclusiveOwner[LockIndex].threadId = 0;
ReleaseSRWLockExclusive(&m_srwLocks[LockIndex]);
}
#else
LeaveCriticalSection(&m_crLocks[LockIndex]);
#endif // _WIN32_WINNT >= 0x0600
}
static bool m_Initialized;

View File

@ -1132,24 +1132,8 @@ __declspec(dllexport) void* _gui_sendmessage(GUIMSG type, void* param1, void* pa
__declspec(dllexport) const char* _gui_translate_text(const char* source)
{
#if (_WIN32_WINNT >= 0x0600) // Starting from Windows Vista this is supported
QByteArray translatedUtf8 = QCoreApplication::translate("DBG", source).toUtf8();
TLS_TranslatedString.Data[translatedUtf8.size()] = 0; // Set the string terminator first.
memcpy(TLS_TranslatedString.Data, translatedUtf8.constData(), std::min((size_t)translatedUtf8.size(), sizeof(TLS_TranslatedString.Data) - 1)); // Then copy the string safely.
return TLS_TranslatedString.Data; // Don't need to free this memory. But this pointer should be used immediately to reduce race condition.
#else
if(TLS_TranslatedStringMap)
{
QByteArray translatedUtf8 = QCoreApplication::translate("DBG", source).toUtf8();
// Boom... VS does not support "thread_local"... and cannot use "__declspec(thread)" in a DLL... https://blogs.msdn.microsoft.com/oldnewthing/20101122-00/?p=12233
// Simulating Thread Local Storage with a map...
DWORD ThreadId = GetCurrentThreadId();
TranslatedStringStorage & TranslatedString = (*TLS_TranslatedStringMap)[ThreadId];
TranslatedString.Data[translatedUtf8.size()] = 0; // Set the string terminator first.
memcpy(TranslatedString.Data, translatedUtf8.constData(), std::min((size_t)translatedUtf8.size(), sizeof(TranslatedString.Data) - 1)); // Then copy the string safely.
return TranslatedString.Data; // Don't need to free this memory. But this pointer should be used immediately to reduce race condition.
}
else // Translators are not initialized yet.
return source;
#endif // _WIN32_WINNT >= 0x0600
}

View File

@ -137,12 +137,6 @@ HandlesView::HandlesView(QWidget* parent) : QWidget(parent)
connect(Config(), SIGNAL(shortcutsUpdated()), this, SLOT(refreshShortcuts()));
connect(Bridge::getBridge(), SIGNAL(dbgStateChanged(DBGSTATE)), this, SLOT(dbgStateChanged(DBGSTATE)));
#if (_WIN32_WINNT < 0x0600) // This is only supported on Windows Vista or greater
mTcpConnectionsTable->setRowCount(1);
mTcpConnectionsTable->setCellContent(0, 0, tr("TCP Connection enumeration is only available on Windows Vista or greater."));
mTcpConnectionsTable->reloadData();
#endif (_WIN32_WINNT < 0x0600)
mWindowsTable->setAccessibleName(tr("Windows"));
mHandlesTable->setAccessibleName(tr("Handles"));
mTcpConnectionsTable->setAccessibleName(tr("TCP Connections"));
@ -530,7 +524,6 @@ void HandlesView::enumPrivileges()
//Enumerate TCP connections and update TCP connections table
void HandlesView::enumTcpConnections()
{
#if (_WIN32_WINNT >= 0x0600)
BridgeList<TCPCONNECTIONINFO> connections;
if(DbgFunctions()->EnumTcpConnections(&connections))
{
@ -551,7 +544,6 @@ void HandlesView::enumTcpConnections()
mTcpConnectionsTable->reloadData();
// refresh values also when in mSearchList
mTcpConnectionsTable->refreshSearchList();
#endif // _WIN32_WINNT < 0x0600
}
/*

View File

@ -63,13 +63,7 @@ bool MyApplication::notify(QObject* receiver, QEvent* event)
static Configuration* mConfiguration;
char gCurrentLocale[MAX_SETTING_SIZE] = "";
#if (_WIN32_WINNT < 0x0600)
// Boom... VS does not support "thread_local"... and cannot use "__declspec(thread)" in a DLL... https://blogs.msdn.microsoft.com/oldnewthing/20101122-00/?p=12233 (dead link)
// Simulating Thread Local Storage with a map...
std::map<DWORD, TranslatedStringStorage>* TLS_TranslatedStringMap; //key = Thread Id, value = Translate Buffer
#else
thread_local TranslatedStringStorage TLS_TranslatedString;
#endif
static bool isValidLocale(const QString & locale)
{
@ -173,10 +167,6 @@ int main(int argc, char* argv[])
if(x64dbgTranslator.load(QString("x64dbg_%1").arg(gCurrentLocale), path))
application.installTranslator(&x64dbgTranslator);
#if (_WIN32_WINNT < 0x0600)
TLS_TranslatedStringMap = new std::map<DWORD, TranslatedStringStorage>();
#endif // _WIN32_WINNT < 0x0600
// load config file + set config font
mConfiguration = new Configuration;
application.setFont(ConfigFont("Application"));
@ -237,12 +227,6 @@ int main(int argc, char* argv[])
#endif
delete mainWindow;
mConfiguration->save(); //save config on exit
#if (_WIN32_WINNT < 0x0600)
//delete tls
auto temp = TLS_TranslatedStringMap;
TLS_TranslatedStringMap = nullptr;
delete temp;
#endif // _WIN32_WINNT < 0x0600
//TODO free Zydis/config/bridge and prevent use after free.

View File

@ -27,11 +27,8 @@ struct TranslatedStringStorage
{
char Data[4096];
};
#if (_WIN32_WINNT >= 0x0600)
extern thread_local TranslatedStringStorage TLS_TranslatedString;
#else
extern std::map<DWORD, TranslatedStringStorage>* TLS_TranslatedStringMap;
#endif // _WIN32_WINNT >= 0x0600
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
class MyEventFilter : public QAbstractNativeEventFilter