Page 1 of 1

VirtualProtectEx_ + GetLastError_

Posted: Tue Mar 13, 2018 10:30 am
by ALAN-MHz
Dear all, i've coded a tool to read some memory from a process, but on a VirtualBox that i use for testing i've got that both VirtualProtectEx and GetLastError return 0, but from MSDN i've read that VirtualProtectEx return 0 if fail, but error code 0 is "The operation completed successfully" so what is the problem ? Thanks!

Re: VirtualProtectEx_ + GetLastError_

Posted: Tue Mar 13, 2018 2:18 pm
by cas
After VirtualProtectEx_() fails, you must call GetLastError_() before any other API function. Some PureBasic commands also internally call API functions and they also can overwrite GetLastError_() result.
For example, here we need to call ProgramFilename() after VirtualProtectEx_() so we do it like this:

Code: Select all

v=VirtualProtectEx_(0,0,0,0,0)
;If v=0
e=GetLastError_()
;EndIf
f$=ProgramFilename()
Debug v
Debug e ;ok, we have error code for VirtualProtectEx_()
And not like this:

Code: Select all

v=VirtualProtectEx_(0,0,0,0,0)
f$=ProgramFilename()
;If v=0
e=GetLastError_()
;EndIf
Debug v
Debug e ; error code was overwritten by ProgramFilename() internal API function
If this does not solve your problem then it could be a bug in your specific version of VirtualBox - maybe try updating if you are on older build.

Re: VirtualProtectEx_ + GetLastError_

Posted: Tue Mar 13, 2018 3:28 pm
by ALAN-MHz
my code is like:

if VirtualProtectEx_ (...) = 0
debug GetLastError_ ()
endif

and i got 0 in debug window, VirtualBox is v5.2.6, i cannot test on a real pc because i need to test on x86 machine and i've an x64 machine, so i'm using virtualbox for testing, any other idea or test to do ? Thanks!

Re: VirtualProtectEx_ + GetLastError_

Posted: Tue Mar 13, 2018 4:20 pm
by cas
What output inside virtualbox do you get when you run these 2 snippets that i posted above?

Re: VirtualProtectEx_ + GetLastError_

Posted: Tue Mar 13, 2018 4:22 pm
by Fred
'Debug' is a complex command and can change the errorcode. You should use DisableDebugger/EnableDebugger if you really to have no interaction between the API call and GetLastError_()

Code: Select all


DisableDebugger
result = VirtualProtectEx_ (...)
lastErrorResult = GetLastError_ ()
EnableDebugger
if result
  debug lastErrorResult
endif

Re: VirtualProtectEx_ + GetLastError_

Posted: Thu Mar 15, 2018 10:24 am
by ALAN-MHz
ok solved, i've 2 problems in my code:

1) wrong parameter passed to VirtualProtectEx
2) String concatenate problem on GetLastError that give me 0 return value

Thanks to all!

Re: VirtualProtectEx_ + GetLastError_

Posted: Thu Mar 15, 2018 11:25 am
by Fred
Please always post a small code snippet when requesting help so we can quickly test and tune it.