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 Shlwapi
Ws2_32 Ws2_32
Wininet Wininet
Iphlpapi
) )
if(CMAKE_SIZEOF_VOID_P EQUAL 4) # x86 if(CMAKE_SIZEOF_VOID_P EQUAL 4) # x86

View File

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

View File

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

View File

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

View File

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

View File

@ -163,45 +163,15 @@ bool HandlesGetName(HANDLE remoteHandle, String & name, String & typeName)
} }
else if(strcmp(typeName.c_str(), "Thread") == 0) else if(strcmp(typeName.c_str(), "Thread") == 0)
{ {
DWORD TID, PID; auto TID = GetThreadId(hLocalHandle);
#if (_WIN32_WINNT >= 0x0600) auto PID = GetProcessIdOfThread(hLocalHandle);
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
if(TID == 0 || PID == 0) //The first time could fail because the process didn't specify query permissions. if(TID == 0 || PID == 0) //The first time could fail because the process didn't specify query permissions.
{ {
HANDLE hLocalQueryHandle; HANDLE hLocalQueryHandle;
if(DuplicateHandle(hProcess, remoteHandle, GetCurrentProcess(), &hLocalQueryHandle, THREAD_QUERY_INFORMATION, FALSE, 0)) if(DuplicateHandle(hProcess, remoteHandle, GetCurrentProcess(), &hLocalQueryHandle, THREAD_QUERY_INFORMATION, FALSE, 0))
{ {
#if (_WIN32_WINNT >= 0x0600)
TID = GetThreadId(hLocalQueryHandle); TID = GetThreadId(hLocalQueryHandle);
PID = GetProcessIdOfThread(hLocalQueryHandle); PID = GetProcessIdOfThread(hLocalQueryHandle);
#else
getTidPid(hLocalHandle, TID, PID);
#endif // _WIN32_WINNT < 0x0600
CloseHandle(hLocalQueryHandle); CloseHandle(hLocalQueryHandle);
} }
} }

View File

@ -1,9 +1,6 @@
#include "tcpconnections.h" #include "tcpconnections.h"
#include <WS2tcpip.h> #include <WS2tcpip.h>
#if (_WIN32_WINNT >= 0x0600)
#include <iphlpapi.h> #include <iphlpapi.h>
#pragma comment(lib, "iphlpapi.lib")
static const char* TcpStateToString(unsigned int State) static const char* TcpStateToString(unsigned int State)
{ {
@ -37,12 +34,9 @@ static const char* TcpStateToString(unsigned int State)
return "UNKNOWN"; return "UNKNOWN";
} }
} }
#endif // _WIN32_WINNT >= 0x0600
bool TcpEnumConnections(duint pid, std::vector<TCPCONNECTIONINFO> & connections) bool TcpEnumConnections(duint pid, std::vector<TCPCONNECTIONINFO> & connections)
{ {
#if (_WIN32_WINNT >= 0x0600)
TCPCONNECTIONINFO info; TCPCONNECTIONINFO info;
wchar_t AddrBuffer[TCP_ADDR_SIZE] = L""; wchar_t AddrBuffer[TCP_ADDR_SIZE] = L"";
ULONG ulSize = 0; 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; return true;
} }

View File

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

View File

@ -65,20 +65,13 @@ void SectionLockerGlobal::Initialize()
// This gets called on the same thread as the GUI // This gets called on the same thread as the GUI
m_guiMainThreadId = GetCurrentThreadId(); m_guiMainThreadId = GetCurrentThreadId();
#if (_WIN32_WINNT >= 0x0600)
// Destroy previous data if any existed // Destroy previous data if any existed
memset(m_srwLocks, 0, sizeof(m_srwLocks)); memset(m_srwLocks, 0, sizeof(m_srwLocks));
for(int i = 0; i < ARRAYSIZE(m_srwLocks); i++) for(int i = 0; i < ARRAYSIZE(m_srwLocks); i++)
InitializeSRWLock(&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; m_Initialized = true;
} }
@ -87,7 +80,6 @@ void SectionLockerGlobal::Deinitialize()
if(!m_Initialized) if(!m_Initialized)
return; return;
#if (_WIN32_WINNT >= 0x0600)
for(int i = 0; i < ARRAYSIZE(m_srwLocks); i++) for(int i = 0; i < ARRAYSIZE(m_srwLocks); i++)
{ {
// Wait for the lock's ownership to be released // Wait for the lock's ownership to be released
@ -97,18 +89,6 @@ void SectionLockerGlobal::Deinitialize()
// Invalidate data // Invalidate data
memset(&m_srwLocks[i], 0, sizeof(SRWLOCK)); 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; m_Initialized = false;
} }

View File

@ -105,7 +105,6 @@ private:
{ {
auto threadId = GetCurrentThreadId(); auto threadId = GetCurrentThreadId();
#if (_WIN32_WINNT >= 0x0600)
auto srwLock = &m_srwLocks[LockIndex]; auto srwLock = &m_srwLocks[LockIndex];
if(Shared) if(Shared)
@ -146,24 +145,11 @@ private:
assert(m_exclusiveOwner[LockIndex].count == 0); assert(m_exclusiveOwner[LockIndex].count == 0);
m_exclusiveOwner[LockIndex].threadId = threadId; m_exclusiveOwner[LockIndex].threadId = threadId;
m_exclusiveOwner[LockIndex].count = 1; 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> template<SectionLock LockIndex, bool Shared>
static void ReleaseLock() static void ReleaseLock()
{ {
#if (_WIN32_WINNT >= 0x0600)
if(Shared) if(Shared)
{ {
if(m_exclusiveOwner[LockIndex].threadId == GetCurrentThreadId()) if(m_exclusiveOwner[LockIndex].threadId == GetCurrentThreadId())
@ -181,9 +167,6 @@ private:
m_exclusiveOwner[LockIndex].threadId = 0; m_exclusiveOwner[LockIndex].threadId = 0;
ReleaseSRWLockExclusive(&m_srwLocks[LockIndex]); ReleaseSRWLockExclusive(&m_srwLocks[LockIndex]);
} }
#else
LeaveCriticalSection(&m_crLocks[LockIndex]);
#endif // _WIN32_WINNT >= 0x0600
} }
static bool m_Initialized; 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) __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(); QByteArray translatedUtf8 = QCoreApplication::translate("DBG", source).toUtf8();
TLS_TranslatedString.Data[translatedUtf8.size()] = 0; // Set the string terminator first. 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. 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. 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(Config(), SIGNAL(shortcutsUpdated()), this, SLOT(refreshShortcuts()));
connect(Bridge::getBridge(), SIGNAL(dbgStateChanged(DBGSTATE)), this, SLOT(dbgStateChanged(DBGSTATE))); 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")); mWindowsTable->setAccessibleName(tr("Windows"));
mHandlesTable->setAccessibleName(tr("Handles")); mHandlesTable->setAccessibleName(tr("Handles"));
mTcpConnectionsTable->setAccessibleName(tr("TCP Connections")); mTcpConnectionsTable->setAccessibleName(tr("TCP Connections"));
@ -530,7 +524,6 @@ void HandlesView::enumPrivileges()
//Enumerate TCP connections and update TCP connections table //Enumerate TCP connections and update TCP connections table
void HandlesView::enumTcpConnections() void HandlesView::enumTcpConnections()
{ {
#if (_WIN32_WINNT >= 0x0600)
BridgeList<TCPCONNECTIONINFO> connections; BridgeList<TCPCONNECTIONINFO> connections;
if(DbgFunctions()->EnumTcpConnections(&connections)) if(DbgFunctions()->EnumTcpConnections(&connections))
{ {
@ -551,7 +544,6 @@ void HandlesView::enumTcpConnections()
mTcpConnectionsTable->reloadData(); mTcpConnectionsTable->reloadData();
// refresh values also when in mSearchList // refresh values also when in mSearchList
mTcpConnectionsTable->refreshSearchList(); mTcpConnectionsTable->refreshSearchList();
#endif // _WIN32_WINNT < 0x0600
} }
/* /*

View File

@ -63,13 +63,7 @@ bool MyApplication::notify(QObject* receiver, QEvent* event)
static Configuration* mConfiguration; static Configuration* mConfiguration;
char gCurrentLocale[MAX_SETTING_SIZE] = ""; 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; thread_local TranslatedStringStorage TLS_TranslatedString;
#endif
static bool isValidLocale(const QString & locale) 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)) if(x64dbgTranslator.load(QString("x64dbg_%1").arg(gCurrentLocale), path))
application.installTranslator(&x64dbgTranslator); application.installTranslator(&x64dbgTranslator);
#if (_WIN32_WINNT < 0x0600)
TLS_TranslatedStringMap = new std::map<DWORD, TranslatedStringStorage>();
#endif // _WIN32_WINNT < 0x0600
// load config file + set config font // load config file + set config font
mConfiguration = new Configuration; mConfiguration = new Configuration;
application.setFont(ConfigFont("Application")); application.setFont(ConfigFont("Application"));
@ -237,12 +227,6 @@ int main(int argc, char* argv[])
#endif #endif
delete mainWindow; delete mainWindow;
mConfiguration->save(); //save config on exit 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. //TODO free Zydis/config/bridge and prevent use after free.

View File

@ -27,11 +27,8 @@ struct TranslatedStringStorage
{ {
char Data[4096]; char Data[4096];
}; };
#if (_WIN32_WINNT >= 0x0600)
extern thread_local TranslatedStringStorage TLS_TranslatedString; 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) #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
class MyEventFilter : public QAbstractNativeEventFilter class MyEventFilter : public QAbstractNativeEventFilter