diff --git a/library/DFProcess-windows.cpp b/library/DFProcess-windows.cpp index 7b5063b2a..f8f997ea5 100644 --- a/library/DFProcess-windows.cpp +++ b/library/DFProcess-windows.cpp @@ -57,7 +57,7 @@ NormalProcess::NormalProcess(uint32_t pid, vector & known_versio : d(new Private()) { HMODULE hmod = NULL; - DWORD junk; + DWORD needed; HANDLE hProcess; bool found = false; @@ -70,7 +70,7 @@ NormalProcess::NormalProcess(uint32_t pid, vector & known_versio return; // try getting the first module of the process - if(EnumProcessModules(hProcess, &hmod, 1 * sizeof(HMODULE), &junk) == 0) + if(EnumProcessModules(hProcess, &hmod, sizeof(hmod), &needed) == 0) { CloseHandle(hProcess); // cout << "EnumProcessModules fail'd" << endl; @@ -290,7 +290,7 @@ bool NormalProcess::getThreadIDs(vector & threads ) CloseHandle( AllThreads ); return true; } - +/* typedef struct _MEMORY_BASIC_INFORMATION { void * BaseAddress; @@ -301,32 +301,54 @@ typedef struct _MEMORY_BASIC_INFORMATION uint32_t Protect; uint32_t Type; } MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION; - +*/ // FIXME: NEEDS TESTING! void NormalProcess::getMemRanges( vector & ranges ) { MEMORY_BASIC_INFORMATION MBI; - const uint64_t PageSize = 4096; + DWORD needed; + HMODULE hmod; + HMODULE * allModules = 0; + bool hasModules = false; + // get page size + SYSTEM_INFO si; + GetSystemInfo(&si); + uint64_t PageSize = si.dwPageSize; + uint64_t page = 0; - while (VirtualQuery(this->d->my_handle, page * PageSize, sizeof(MBI)) == sizeof(MBI)) + + // get all the modules + if(EnumProcessModules(this->d->my_handle, &hmod, sizeof(hmod), &needed)) + { + allModules = (HMODULE *) malloc(needed); + hasModules = EnumProcessModules(this->d->my_handle, allModules, needed, &needed); + } + + // go through all the VM regions, convert them to our internal format + while (VirtualQueryEx(this->d->my_handle, (const void*) (page * PageSize), &MBI, sizeof(MBI)) == sizeof(MBI)) { page = MBI.RegionSize / PageSize; if(MBI.RegionSize - MBI.RegionSize / PageSize != 0) page ++; // skip over non-whole page - if( !(MBI.Protect & MEM_COMMIT) ) // skip empty regions + if( !(MBI.State & MEM_COMMIT) ) // skip empty regions continue; + // TODO: we could possibly discard regions shared with other processes (DLLs)? + // MBI.Type & MEM_PRIVATE + t_memrange temp; - temp.start = MBI.BaseAddress; - temp.end = MBI.BaseAddress + MBI.RegionSize; - temp.read = MBI.Protect & PAGE_EXECUTE_READ | MBI.Protect & PAGE_EXECUTE_READWRITE | MBI.Protect & PAGE_READONLY | MBI.Protect & PAGE_READWRITE; - temp.write = MBI.Protect & PAGE_EXECUTE_READWRITE | MBI.Protect & PAGE_READWRITE; - temp.execute = MBI.Protect & PAGE_EXECUTE_READ | MBI.Protect & PAGE_EXECUTE_READWRITE | MBI.Protect & PAGE_EXECUTE; - temp.name = "N/A"; // FIXME: pull some relevant names from somewhere... + temp.start = (uint64_t) MBI.BaseAddress; + temp.end = ((uint64_t)MBI.BaseAddress + (uint64_t)MBI.RegionSize); + temp.read = MBI.Protect & PAGE_EXECUTE_READ || MBI.Protect & PAGE_EXECUTE_READWRITE || MBI.Protect & PAGE_READONLY || MBI.Protect & PAGE_READWRITE; + temp.write = MBI.Protect & PAGE_EXECUTE_READWRITE || MBI.Protect & PAGE_READWRITE; + temp.execute = MBI.Protect & PAGE_EXECUTE_READ || MBI.Protect & PAGE_EXECUTE_READWRITE || MBI.Protect & PAGE_EXECUTE; + // FIXME: some relevant description text would be helpful + strcpy(temp.name,"N/A"); ranges.push_back(temp); } - + if(allModules) + free(allModules); } uint8_t NormalProcess::readByte (const uint32_t offset) diff --git a/library/include/DFHack_C.h b/library/include/DFHack_C.h index 6c5dd004f..b23457fda 100644 --- a/library/include/DFHack_C.h +++ b/library/include/DFHack_C.h @@ -25,6 +25,8 @@ distribution. #ifndef DFHACK_C_API #define DFHACK_C_API +#include "dfhack/DFPragma.h" + #include #include #include diff --git a/library/include/dfhack/DFError.h b/library/include/dfhack/DFError.h index 28e11df33..0e4d00145 100644 --- a/library/include/dfhack/DFError.h +++ b/library/include/dfhack/DFError.h @@ -26,6 +26,7 @@ distribution. #define ERROR_H_INCLUDED #include "DFExport.h" +#include "DFPragma.h" #include #include #include diff --git a/library/include/dfhack/DFPragma.h b/library/include/dfhack/DFPragma.h index e678093cc..fb0d395c4 100644 --- a/library/include/dfhack/DFPragma.h +++ b/library/include/dfhack/DFPragma.h @@ -7,8 +7,18 @@ #ifdef _MSC_VER // don't spew nonsense #pragma warning( disable: 4251 ) - // don't display bogus 'deprecation' and 'unsafe' warnings + // don't display bogus 'deprecation' and 'unsafe' warnings. + // See the idiocy: http://msdn.microsoft.com/en-us/magazine/cc163794.aspx + #define _CRT_SECURE_NO_DEPRECATE + #define _SCL_SECURE_NO_DEPRECATE #pragma warning( disable: 4996 ) + // Let me demonstrate: + /** + * [peterix@peterix dfhack]$ man wcscpy_s + * No manual entry for wcscpy_s + * + * Proprietary extensions. + */ // disable stupid #pragma warning( disable: 4800 ) // disable more stupid diff --git a/library/modules/Maps_C.cpp b/library/modules/Maps_C.cpp index 0c5647b16..58e9bb76c 100644 --- a/library/modules/Maps_C.cpp +++ b/library/modules/Maps_C.cpp @@ -22,6 +22,7 @@ must not be misrepresented as being the original software. distribution. */ +#include "dfhack/DFPragma.h" #include #include