Variables constantly changing :/

BlitzMax Forums/BlitzMax Programming/Variables constantly changing :/

Gabriel(Posted 2005) [#1]
This has been driving me nuts for a couple of days now, and I've finally tracked down what's wrong with all my WinAPI code, though I have zero idea why or how to fix it.

I've cut this down to the absolute minimum amount of code required to show the error.

The code


SuperStrict

Import glimmer.psapi

Function Enumerate()
	
	Local ProcessIDs:Int[]=New Int[1000]
	
	Local cb:Int
	Local cbNeeded:Int
	Local lret:Int
	
	Local NumProcessesFound:Int
	
	Local Counter:Int
	
	'Local hProcess:Int
	Local hp:Int=0
	
	
	
	cb=100
	cbNeeded=0
	
	DebugLog hp
	
	lret=EnumProcesses(Varptr(ProcessIDs[0]),cb,Varptr(cbNeeded))
	
	DebugLog hp
	DebugLog hp
	DebugLog hp
		
End Function


Enumerate()



The PSApi Module it requires. ( I Called it glimmer.psapi, so you'll need to match it or change the import call if you're going to test this.

SuperStrict

Module Glimmer.PSApi

ModuleInfo "Version 1.00"

Import "-lpsapi"


Extern 

Function EnumProcesses(pProcessIDs:Byte Ptr,cb:Int,cbNeeded:Int Ptr) = "EnumProcesses@12"

End Extern



Now run it and you'll see that, despite being set to 0 and never being changed, hp is a huge number right after EnumProcesses() is called. Moreover, it changes every time I call debuglog. Now maybe I've gone a bit batchy after two days straight of putzing around with this, but I'm thinking variables shouldn't be randomly changing every time I call debuglog.

Note that I've made the array much bigger than it needs to be to ensure that the API is not overwriting my variables. It doesn't get close to the end of the array anyway.


DStastny(Posted 2005) [#2]
I dont have the psapi but,

Looks like a calling convetion problem the namemangleing
...@12 is ususally indication of stdcall "Win32" in BMAX terms.

Did you try

Extern "Win32"

Doug Stastny


Yan(Posted 2005) [#3]
SuperStrict

Import "-lpsapi"

Extern "win32"
	Function EnumProcesses(pProcessIDs:Byte Ptr, cb:Int, cbNeeded:Int Var) = "EnumProcesses@12"
End Extern

Enumerate()

End


Function Enumerate()
	
	Local ProcessIDs:Int[]=New Int[100]
	
	Local cb:Int
	Local cbNeeded:Int
	Local lret:Int
	
	Local NumProcessesFound:Int
	
	Local Counter:Int
	
	'Local hProcess:Int
	Local hp:Int=0
	
	cb=400
	cbNeeded=0
	
	DebugLog hp
	
	lret = EnumProcesses(ProcessIDs, cb, cbNeeded)
	
	DebugLog hp
	DebugLog cb
	DebugLog cbNeeded
	DebugLog "Processes : " + cbNeeded / 4
	
	For Local p:Int = EachIn ProcessIDs
		DebugLog Hex(p)
	Next		
End Function

[edit]
Budman beat me too it. ;o)
[/edit]


Gabriel(Posted 2005) [#4]
Oh thank heavens for that, I thought I was going mad.

Thanks guys, it seems to be working fine with "Win32" added.