Created
October 10, 2013 13:10
-
-
Save kevinlynx/6918061 to your computer and use it in GitHub Desktop.
JNA used in scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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