You can only run 32 bit processes in a 32 bit environment.
Most 64-bit OS's provide some from of 32 bit support. In Win64 this is called WoW64: (Win32 on Win64).
If the OS does not provide this support then you're out of luck. In e.g. Windows 2008 server R2 WOW64 is optional.
Why can't you run 32 bit code in 64-bit mode
In x64 the single byte inc/dec
instructions have been repurposed as REX prefixes.
That means your code will fail at:
protect:
xor byte ptr ds:[eax], 0x25
inc eax <<-- will not work in X64
cmp eax, ecx
Apart from that
mov ecx, StartAddress
....
mov eax StartAddress
Will not work, you need to load pointers into 64 bit registers, or your code will fail if it's loaded at a high address.
Even though many of the opcodes are the same between x86 and x64.
A single run of 32-bit code in a 64-bit process will fail pretty quickly.
If not what can I do?
Change the code so that it conforms to 64-bit.
You need to change all pointers/addresses to be 64 bits wide.
You need to change all registers addressing memory to 64-bits.
__asm
{
push rax
push rcx
mov rcx, StartAddress
add rcx fSize
mov rax StartAddress
protect:
xor byte ptr [rax], 0x25 //ds is the default.
inc rax
cmp rax, rcx
jl protect;
pop rcx
pop rax
}
You cannot run a 32 bit program inside a 64-bit process.
The only reason Win64 et al manage this trick is that the OS performs a privileged mode change back to 32-bit mode as needed.
Because Microsoft C++ compilers are crippled and do not support inline 64-bit assembly you'll have to either use Embarcadero's C++builder of G++ if you want to use 64-bit assembly.