API GlobalMemoryStatus

BlitzMax Forums/BlitzMax Programming/API GlobalMemoryStatus

Kingo(Posted 2006) [#1]
I'am looking for use API Windows with Type.

For exemple the GlobalMemoryStatus command must have a MEMORYSTATUS Structure to work.

I try with an example I've found on the API Guide Software. But nothing :(

Here's the code I test :

'Status de la mémoire **API Windows**

Type MEMORYSTATUS
	Field	dwLength:Long
	Field	dwMemoryLoad:Long
	Field	dwTotalPhys:Long
	Field	dwAvailPhys:Long
	Field	dwTotalPageFile:Long
	Field	dwAvailPageFile:Long
	Field	dwTotalVirtual:Long
	Field	dwAvailVirtual:Long
EndType

Global GlobalMemoryStatus(lpBuffer:Byte Ptr)

kernel32 = LoadLibraryA("kernel32.dll")

If Kernel32
	GlobalMemoryStatus = (GetProcAddress(kernel32,"GlobalMemoryStatus"))
Else
	Notify("Impossible d'ouvrir la lib")
EndIf

Local Memoire:MEMORYSTATUS = New MEMORYSTATUS

GlobalMemoryStatus(Memoire)

Print Memoire.dwTotalPhys


This, prints a value but not really the total physical memory...

How API works with structure like this ?

Here's the example I've Found on API Guide software (vb example) :

Private Type MEMORYSTATUS
    dwLength As Long
    dwMemoryLoad As Long
    dwTotalPhys As Long
    dwAvailPhys As Long
    dwTotalPageFile As Long
    dwAvailPageFile As Long
    dwTotalVirtual As Long
    dwAvailVirtual As Long
End Type
Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)
Private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@...
    Dim MemStat As MEMORYSTATUS
    'retrieve the memory status
    GlobalMemoryStatus MemStat
    MsgBox "You have" + Str$(MemStat.dwTotalPhys / 1024) + " Kb total memory and" + Str$(MemStat.dwAvailPageFile / 1024) + " Kb available PageFile memory."
End Sub


So, How can I code the GlobalMemoryStatus on Bmax using the API ?

Thx in advance :)


Stuart Morgan(Posted 2006) [#2]
I'm assuming you can build modules, otherwise you just need to Import the memory.c file into your bmx code and delete the "Module Stu.Memory" line.

memory.bmx


memory.c



Kingo(Posted 2006) [#3]
yes but where do you find these c files ?

If I want to retrieve disk volume the computer have is this the same thing ? a c file and so on ?

is it possible to create a program in vb and import it to bmax like this ?

Thx in advance.


Stuart Morgan(Posted 2006) [#4]
yes but where do you find these c files ?

I provided the c file for you in the second codebox. Just copy and paste it into a new file and save it as memory.c and then import it into you bmax source.

' Example
Import "memory.c"

Print "Total Physical Memory: "+(TotalPhysicalMemory()/1024)+" Kb"
Print "Available Physical Memory: "+(AvailPhysicalMemory()/1024)+" Kb"

is it possible to create a program in vb and import it to bmax like this ?

No BlitzMax cannot compile VB source, however it will compile imported C/C++ and Assembler source.


Kingo(Posted 2006) [#5]
Ok thx

Is there a place we can find somme c files to import in ?

Because I need to have some informtions like volume disk etc...


Stuart Morgan(Posted 2006) [#6]
Is there a place we can find somme c files to import in ?

There are plenty of C/C++ source code websites where you can find bits and pieces, but you'll more than likely have to write the C/C++ source yourself if you want to do something specific.

Because I need to have some informtions like volume disk etc...


This should do it...



diskvolume.c



Kingo(Posted 2006) [#7]
very complicated :(

I don't know c langage...But it isn't possible to do that with bmax like I try to ?


Stuart Morgan(Posted 2006) [#8]
You need to replace the Longs with Ints, like so...




Kingo(Posted 2006) [#9]
:( ???

I tried this "replace long by int" byt it gave me wrong informations...And here it's work arf.

I thing i made a mistakes...I tried, long, int....but in fact several mistakes lol

Excellent thx a lot, I am going to try some API like this.

I hope I could translate in bmax some API command like this.


DStastny(Posted 2006) [#10]
You have two problems:
Your Function pointer needs to be declared "Win32" that will cause a crash with the stack.
You need to set the length of the dwLength member of the MEMORYSTATUS Type

This code is fixed and works
Type MEMORYSTATUS
	Field dwLength:Int
	Field dwMemoryLoad:Int
	Field dwTotalPhys:Int
	Field dwAvailPhys:Int
	Field dwTotalPageFile:Int
	Field dwAvailPageFile:Int
	Field dwTotalVirtual:Int
	Field dwAvailVirtual:Int
	
	Method New()
		' must set the legnth
		dwLength=SizeOf(MEMORYSTATUS) 
	End Method
EndType

' Must have "Win32" since windows API
Global GlobalMemoryStatus(lpBuffer:Byte Ptr) "Win32"

kernel32 = LoadLibraryA("kernel32.dll")

If Kernel32
	GlobalMemoryStatus = GetProcAddress(kernel32,"GlobalMemoryStatus")
Else
	Notify("Impossible d'ouvrir la lib")
EndIf

Local Memoire:MEMORYSTATUS = New MEMORYSTATUS

GlobalMemoryStatus(Memoire)

Print "Total physical: "+(Memoire.dwTotalPhys/1024)+" Kb"
Print "Available physical: "+(Memoire.dwAvailPhys/1024)+" Kb"



Hope this helps. Make sure if its Windows API to add "Win32" and make sure you fully read the API documents as there is often a size of length field on the type that needs to be set correctly.

Doug Stastny


Kingo(Posted 2006) [#11]
thx ;)