Skip to content

Instantly share code, notes, and snippets.

@kevinlynx
Created October 10, 2013 13:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinlynx/6918061 to your computer and use it in GitHub Desktop.
Save kevinlynx/6918061 to your computer and use it in GitHub Desktop.
JNA used in scala
package test
import com.sun.jna._
import com.sun.jna.ptr._
trait Kernel32 extends Library {
def GetDiskFreeSpaceA(lpRootPathName: String,
lpSectorsPerCluster: IntByReference, lpBytesPerSector: IntByReference,
lpNumberOfFreeClusters: IntByReference, lpTotalNumberOfClusters: IntByReference): Boolean
def GetComputerNameW(outName: Array[Char], size: IntByReference): Boolean
def GetTempPathA(size: Integer, outName: Array[Byte]): Integer
}
object TestJNA {
def main(args: Array[String]) {
val lib = Native.loadLibrary("kernel32", classOf[Kernel32]).asInstanceOf[Kernel32]
val disk = raw"C:\"
val spc = new IntByReference(0) // Sectors per cluster
val bps = new IntByReference(0) // Bytes per sector
val fc = new IntByReference(0) // Free clusters
val tc = new IntByReference(0) // Total clusters
val ok = lib.GetDiskFreeSpaceA(disk, spc, bps, fc, tc) // status
println(f"""'$disk%s' ($ok%s): sectors/cluster: ${spc.getValue}%d,
bytes/sector: ${bps.getValue}%d, free-clusters: ${fc.getValue}%d,
total/clusters: ${tc.getValue}%d""")
val name = new Array[Char](256)
val size = new IntByReference(256)
val ret = lib.GetComputerNameW(name, size)
println("computer name: " + name.mkString + " ret: " + ret)
{
val name = new Array[Byte](256)
lib.GetTempPathA(256, name)
val s = name.map(_.toChar)
println("temp path: " + s.mkString)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment