/*
 Access Windows volume serial number of a drive.
 For this to work, nativevolser.dll must exist someone on the Java library path or path.

copyright (c) 2007-2009 Roedy Green, Canadian Mind Products
may be copied and used freely for any purpose but military.

Roedy Green
Canadian Mind Products
#101 - 2536 Wark Street
Victoria, BC Canada
V8T 4G8
tel: (250) 361-9093
roedy g at mindprod dotcom
http://mindprod.com

version History

1.0 2007-12-17 - initial release
1.1 2008-09-23 - fix problem with Microsoft C++ runtime library
*/
package com.mindprod.volser;

/**
 * Access Windows 32-bit volume serial number. Uses JNI native C++/ASM code that only works on Vista, XP, Windows 2000.
 * Do not confuse this with the case-sensitive volume label.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.1 2008-09-23 - fix problem with Microsoft C++ runtime library
 */
public final class Volser
    {
    // ------------------------------ FIELDS ------------------------------

    /**
     * true if you want to include the debugging harness code.
     */
    private static final boolean DEBUGGING = true;

    /**
     * undisplayed embedded copyright notice
     */
    public static final String EMBEDDED_COPYRIGHT =
            "copyright (c) 2007-2009 Roedy Green, Canadian Mind Products, http://mindprod.com";

    /**
     * undisplayed embedded release date
     */
    private static final String RELEASE_DATE = "2008-09-23";

    /**
     * undisplayed embedded version string.
     */
    public static final String VERSION_STRING = "1.1";
    // -------------------------- PUBLIC STATIC METHODS --------------------------

    /**
     * Get the volume serial number.
     *
     * @param rootPath drive with trailing colon and backslash e.g. "C:\\". Can also be a network drive.
     * @return 32-bit volume serial number as an int. Normally displayed in hex in two pieces separated by a colon or
     *         dash.
     */
    public static native int getVolser( String rootPath );

    // -------------------------- STATIC METHODS --------------------------

    static
        {
        // get DLL loaded from somewhere on java.library path.
        System.loadLibrary( "nativevolser" );
        // if have troubles change this code to use
        // System.load( "E:\\com\\mindprod\\volser\\nativevolser.dll" );
        }

    // --------------------------- main() method ---------------------------

    /**
     * test debug harness
     *
     * @param args not used
     */
    public static void main( String[] args )
        {
        if ( DEBUGGING )
            {
            final int volser = Volser.getVolser( "C:\\" );
            System.out.println( Integer.toHexString( volser ) );
            final int high = volser >>> 16;
            final int low = volser & 0xffff;
            // This code does not apply lead zeros on each half.
            // See http://mindprod.com/jgloss/hex.html for how to add that refinement.
            final String hexVolser = Integer.toHexString( high ) + ":" + Integer.toHexString( low );
            System.out.println( "4-byte Volume serial number for drive C: is " + hexVolser );
            }
        }
    }