API GlobalMemoryStatus

BlitzMax Forums/BlitzMax Programming/API GlobalMemoryStatus

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: <a href="http://www.allapi.net/" target="_blank">http://www.allapi.net/</a>
    '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 :)

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
Module Stu.Memory

Import "memory.c"

Extern
    Function TotalPhysicalMemory()
    Function AvailPhysicalMemory()
    Function TotalVirtualMemory()
    Function AvailVirtualMemory()
    Function TotalPageFileMemory()
    Function AvailPageFileMemory()
    Function MemoryLoad()
End Extern


memory.c
include <windows.h>

int TotalPhysicalMemory(void){
    MEMORYSTATUS ms;
    GlobalMemoryStatus(&ms);
    return ms.dwTotalPhys;
}

int AvailPhysicalMemory(void){
    MEMORYSTATUS ms;
    GlobalMemoryStatus(&ms);
    return ms.dwAvailPhys;
}

int TotalVirtualMemory(void){
    MEMORYSTATUS ms;
    GlobalMemoryStatus(&ms);
    return ms.dwTotalVirtual;
}

int AvailVirtualMemory(void){
    MEMORYSTATUS ms;
    GlobalMemoryStatus(&ms);
    return ms.dwAvailVirtual;
}

int TotalPageFileMemory(void){
    MEMORYSTATUS ms;
    GlobalMemoryStatus(&ms);
    return ms.dwTotalPageFile;
}

int AvailPageFileMemory(void){
    MEMORYSTATUS ms;
    GlobalMemoryStatus(&ms);
    return ms.dwAvailPageFile;
}

int MemoryLoad(void){
    MEMORYSTATUS ms;
    GlobalMemoryStatus(&ms);
    return ms.dwMemoryLoad / 2;
}



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.

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.

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...

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...

' Example
Import "diskvolume.c"

Extern
    Rem
    Returns the available disk space on the current drive (the drive where the program was executed from)
    End Rem
    Function GetDiskSpace()
End Extern

Print "Disk space on current drive: "+GetDiskSpace()+" Kb"


diskvolume.c
#include <direct.h>

int diskSpace;

int GetDiskSpace(void)
{
    int const drive = _getdrive();
    struct _diskfree_t diskfree;
	
    _getdiskfree(drive,&diskfree);
	
    __int64 diskSpace=(__int64)diskfree.avail_clusters*(__int64)diskfree.sectors_per_cluster*(__int64)diskfree.bytes_per_sector;
    diskSpace/=1000;
    return (int)diskSpace;
}



very complicated :(

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

You need to replace the Longs with Ints, like so...

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
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 "Total physical: "+(Memoire.dwTotalPhys/1024)+" Kb"
Print "Available physical: "+(Memoire.dwAvailPhys/1024)+" Kb"



:( ???

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.

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

thx ;)