Extern and Banks?

BlitzMax Forums/BlitzMax Beginners Area/Extern and Banks?

Chalky(Posted 2007) [#1]
I am trying to convert the following B3D decls function to BMAX:
; .lib "kernel32.dll"
; GetVolumeInformation% _
;    ( _
;     lpRootPathName$, _
;     lpVolumeNameBuffer*, _
;     nVolumeNameSize%, _
;     lpVolumeSerialNumber*, _
;     lpMaximumComponentLength%, _
;     lpFileSystemFlags%, _
;     lpFileSystemNameBuffer*, _
;     nFileSystemNameSize% _
;    ):"GetVolumeInformationA"

Print GetSysDiskSN%("C:\")

Function GetSysDiskSN%(Root$)
	Local APICall%
	Local DiskSerialNo%

	VolSerialNoBank = CreateBank(4)
	VolNameBank = CreateBank(255)
	VolFileSysBank = CreateBank(255)
	APICall%=GetVolumeInformation%(Root$,VolNameBank,BankSize(VolNameBank),VolSerialNoBank,0,0,VolFileSysBank,BankSize(VolFileSysBank))
	DiskSerialNo%=PeekInt(VolSerialNoBank,0)
	FreeBank VolSerialNoBank
	FreeBank VolNameBank
	FreeBank VolFileSysBank
	Return DiskSerialNo%
End Function

However, I am struggling and have not been able to find anything on the forums specific to my problem. I thought my BMax bank references should be 'Byte Ptr' in the extern declaration, and the strings as '$z'?
Extern "Win32"
	Function GetVolumeInformationA(..
		lpRootPathName$z,..
		lpVolumeNameBuffer:Byte Ptr,..
		nVolumeNameSize%,..
		lpVolumeSerialNumber:Byte Ptr,..
		lpMaximumComponentLength%,..
		lpFileSystemFlags%,..
		lpFileSystemNameBuffer:Byte Ptr,..
		nFileSystemNameSize%)
End Extern

Graphics 800,600
SetColor 255,255,255
DrawText "Serial No = "+GetSysDiskSN%("C:\"),0,0
Flip
WaitKey
End

Function GetSysDiskSN%(Root$)
	Local DiskSerialNo%

	VolSerialNoBank=CreateBank(4)
	VolNameBank=CreateBank(255)
	VolFileSysBank=CreateBank(255)
	GetVolumeInformationA("C:\",VolNameBank,BankSize(VolNameBank),VolSerialNoBank,0,0,VolFileSysBank,BankSize(VolFileSysBank))
	DiskSerialNo%=PeekInt(VolSerialNoBank,0)
	VolSerialNoBank=Null
	VolNameBank=Null
	VolFileSysBank=Null
	GCCollect
	Return DiskSerialNo%
End Function

but I have read that BMax strings are now objects rather than CStrings (gulp)...

In B3D I passed the values returned by CreateBank() as the bank address pointers, which worked. However, in BMax this results in an Int to Byte Ptr conversion error.

No matter what I try I cannot get this to work. I am totally confused. Can anyone point me in the right direction?


grable(Posted 2007) [#2]
You dont often need banks in bmax, since you have proper pointers and such.
Extern "Win32"
	Function GetVolumeInformationA(..
		lpRootPathName$z,..
		lpVolumeNameBuffer:Byte Ptr,..
		nVolumeNameSize%,..
		lpVolumeSerialNumber:Int Var,..
		lpMaximumComponentLength:Int Var,..
		lpFileSystemFlags:Int Var,..
		lpFileSystemNameBuffer:Byte Ptr,..
		nFileSystemNameSize%)
EndExtern

Function GetVolumeInformation( root:String, label:String Var, serial:Int Var, maxlen:Int Var, sysflags:Int Var, sysname:String Var)
	Local labelbuf:Byte[64]
	Local sysnamebuf:Byte[64]
	GetVolumeInformationA( root, labelbuf,labelbuf.Length, serial, maxlen, sysflags, sysnamebuf,sysnamebuf.Length)
	label = String.FromCString( labelbuf)
	sysname = String.FromCString( sysnamebuf)	
EndFunction

Local label:String
Local serial:Int
Local maxlen:Int
Local sysflags:Int
Local sysname:String
GetVolumeInformation( "C:\", label, serial, maxlen, sysflags, sysname)
Print label
Print serial
Print maxlen
Print sysflags
Print sysname

Notice that i changed the "Byte Ptr" OUT parameters to Var params.


Chalky(Posted 2007) [#3]
Wow - that's very different from my failed attempt. I had not come across 'Var' params yet (here they are being used to create VB ByRef-like behaviour? A search for 'pointers' in helpfiles and forum now required methinks). Hopefully I can apply what I have learnt from your example to future decls I convert. Thank you grable - that was doing my brain cell in!