VirtualProtectEx_ + GetLastError_

Just starting out? Need help? Post your questions and find answers here.
ALAN-MHz
User
User
Posts: 68
Joined: Fri Jul 29, 2005 11:47 am

VirtualProtectEx_ + GetLastError_

Post 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!
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: VirtualProtectEx_ + GetLastError_

Post 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.
ALAN-MHz
User
User
Posts: 68
Joined: Fri Jul 29, 2005 11:47 am

Re: VirtualProtectEx_ + GetLastError_

Post 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!
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: VirtualProtectEx_ + GetLastError_

Post by cas »

What output inside virtualbox do you get when you run these 2 snippets that i posted above?
Fred
Administrator
Administrator
Posts: 16664
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: VirtualProtectEx_ + GetLastError_

Post 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
ALAN-MHz
User
User
Posts: 68
Joined: Fri Jul 29, 2005 11:47 am

Re: VirtualProtectEx_ + GetLastError_

Post 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!
Fred
Administrator
Administrator
Posts: 16664
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: VirtualProtectEx_ + GetLastError_

Post by Fred »

Please always post a small code snippet when requesting help so we can quickly test and tune it.
Post Reply