Classic GUI for your own application
Nov 7, 2021 1:48:37 GMT -8
Post by mpoupe on Nov 7, 2021 1:48:37 GMT -8
Hi all,
I like old GUI style and your forum helped me to modify my windows 10 to look properly:-)
I also thought is it possible to make my own application to look properly (= classic GUI) on unmodified windows. And this is a sample source code, which does the thing.
Yes, it is a brute force, dirty hack, but someone might find it useful.:-)
#include <windows.h>
#include <stdio.h>
#include <stdint.h>
#ifdef _M_AMD64
#define EXPECTED_PAGE_PROTECTION PAGE_READONLY
#endif
#ifdef _M_IX86
#define EXPECTED_PAGE_PROTECTION PAGE_EXECUTE_READ
#endif
static const wchar_t aPattern[] = L"\\Sessions\\%d\\Windows\\ThemeSectioX";
uint8_t *Search(uint8_t *pBuf,size_t nSize)
{
if(nSize >= sizeof(aPattern))
{
for(size_t i = 0; i < nSize - (sizeof(aPattern)); i += 2)
{
uint8_t *p = pBuf + i;
if(!memcmp(p,aPattern,sizeof(aPattern) - 4))
{
if(!memcmp(p,aPattern,sizeof(aPattern) - 2))
{
continue;
}
return p;
}
}
}
return NULL;
}
static HMODULE InitGUI(void)
{
SYSTEM_INFO si;
MEMORY_BASIC_INFORMATION mbi;
HANDLE hProcess = 0;
HMODULE hLib = LoadLibraryA("uxtheme.dll");
if(!hLib)
{
goto lbExit;
}
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
if(hProcess==0)
{
goto lbExit;
}
GetSystemInfo(&si);
for(uint8_t *pMem = (uint8_t *)si.lpMinimumApplicationAddress;pMem < (uint8_t *)si.lpMaximumApplicationAddress; pMem += mbi.RegionSize)
{
SIZE_T nSize = VirtualQueryEx(hProcess,pMem,&mbi,sizeof(MEMORY_BASIC_INFORMATION));
if(nSize < offsetof(MEMORY_BASIC_INFORMATION,Protect))
{
goto lbExit;
}
if(mbi.Protect != EXPECTED_PAGE_PROTECTION)
{
continue;
}
{
char aBuffer[32];
if(!ReadProcessMemory(hProcess,pMem,aBuffer,sizeof(aBuffer),&nSize))
{
continue;
}
}
uint8_t *p = Search(pMem,mbi.RegionSize);
if(p)
{
DWORD dwOrg;
if(VirtualProtect(pMem,mbi.RegionSize,PAGE_READWRITE,&dwOrg))
{
WriteProcessMemory(hProcess,p,aPattern,sizeof(aPattern),&nSize);
VirtualProtect(pMem,mbi.RegionSize,dwOrg,&dwOrg);
}
goto lbExit;
}
}
lbExit:
if(hProcess)
{
CloseHandle(hProcess);
}
return hLib;
}
int main()
{
HMODULE hLib = InitGUI();
//your application goes here:
MessageBoxA(NULL,"Hello","Hello world",MB_OK);
FreeLibrary(hLib);
return 0;
}
#include <stdio.h>
#include <stdint.h>
#ifdef _M_AMD64
#define EXPECTED_PAGE_PROTECTION PAGE_READONLY
#endif
#ifdef _M_IX86
#define EXPECTED_PAGE_PROTECTION PAGE_EXECUTE_READ
#endif
static const wchar_t aPattern[] = L"\\Sessions\\%d\\Windows\\ThemeSectioX";
uint8_t *Search(uint8_t *pBuf,size_t nSize)
{
if(nSize >= sizeof(aPattern))
{
for(size_t i = 0; i < nSize - (sizeof(aPattern)); i += 2)
{
uint8_t *p = pBuf + i;
if(!memcmp(p,aPattern,sizeof(aPattern) - 4))
{
if(!memcmp(p,aPattern,sizeof(aPattern) - 2))
{
continue;
}
return p;
}
}
}
return NULL;
}
static HMODULE InitGUI(void)
{
SYSTEM_INFO si;
MEMORY_BASIC_INFORMATION mbi;
HANDLE hProcess = 0;
HMODULE hLib = LoadLibraryA("uxtheme.dll");
if(!hLib)
{
goto lbExit;
}
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
if(hProcess==0)
{
goto lbExit;
}
GetSystemInfo(&si);
for(uint8_t *pMem = (uint8_t *)si.lpMinimumApplicationAddress;pMem < (uint8_t *)si.lpMaximumApplicationAddress; pMem += mbi.RegionSize)
{
SIZE_T nSize = VirtualQueryEx(hProcess,pMem,&mbi,sizeof(MEMORY_BASIC_INFORMATION));
if(nSize < offsetof(MEMORY_BASIC_INFORMATION,Protect))
{
goto lbExit;
}
if(mbi.Protect != EXPECTED_PAGE_PROTECTION)
{
continue;
}
{
char aBuffer[32];
if(!ReadProcessMemory(hProcess,pMem,aBuffer,sizeof(aBuffer),&nSize))
{
continue;
}
}
uint8_t *p = Search(pMem,mbi.RegionSize);
if(p)
{
DWORD dwOrg;
if(VirtualProtect(pMem,mbi.RegionSize,PAGE_READWRITE,&dwOrg))
{
WriteProcessMemory(hProcess,p,aPattern,sizeof(aPattern),&nSize);
VirtualProtect(pMem,mbi.RegionSize,dwOrg,&dwOrg);
}
goto lbExit;
}
}
lbExit:
if(hProcess)
{
CloseHandle(hProcess);
}
return hLib;
}
int main()
{
HMODULE hLib = InitGUI();
//your application goes here:
MessageBoxA(NULL,"Hello","Hello world",MB_OK);
FreeLibrary(hLib);
return 0;
}