/////////////////////////////////////////////////////////////////
//
// FreeDisk.java
//
/**
* Retrieves the amount of free disk space on drive C using J/Direct
* @author Jonathan Locke
*/
public class FreeDisk
{
/**
* Main application entrypoint.
* @param arg Command line arguments
*/
static public void main(String[] arg)
{
int[] sectorsPerCluster = new int[1];
int[] bytesPerSector = new int[1];
int[] numberOfFreeClusters = new int[1];
int[] totalNumberOfClusters = new int[1];
if (GetDiskFreeSpace("c:\\", sectorsPerCluster, bytesPerSector,
numberOfFreeClusters, totalNumberOfClusters))
{
System.out.println("There are " + sectorsPerCluster[0]
*bytesPerSector[0]
* numberOfFreeClusters[0]
+ " free bytes on drive c.");
}
else
{
System.out.println("GetDiskFreeSpace failed");
}
}
/** @dll.import("KERNEL32",auto) */
public static native boolean GetDiskFreeSpace(String rootPathName,
int[] sectorsPerCluster, int[] bytesPerSector,
int[] numberOfFreeClusters, int[] totalNumberOfClusters);
}
///////////////////////////////// End of File /////////////////////////////////
Figure 2 GetEnv.java
/////////////////////////////////////////////////////////////////
//
// GetEnv.java
//
/**
* Shows the PATH and CLASSPATH environment variables using J/Direct
* @author Jonathan Locke
*/
public class GetEnv
{
/**
* Main application entrypoint.
* @param arg Command line arguments
*/
static public void main(String[] arg)
{
System.out.println("PATH = " + getEnv("path"));
System.out.println("CLASSPATH = " + getEnv("classpath"));
}
/**
* Gets an environment variable by name
* @param name The name of the environment variable to get
* @return The value of the named environment variable, or
* null if no such variable exists
*/
static String getEnv(String name)
{
// Allocate string buffer for return value
StringBuffer value = new StringBuffer(128);
// Get environment variable via J/Direct
int chars = GetEnvironmentVariable(name, value, value.capacity());
// If 0 is returned, the variable was not found
if (chars == 0)
{
return null;
}
// If it was not large enough, reallocate buffer and retry
if (chars >= value.capacity())
{
value.ensureCapacity(chars);
GetEnvironmentVariable(name, value, value.capacity());
}
return value.toString();
}
/** @dll.import("KERNEL32",auto) */
static native int GetEnvironmentVariable(String name, StringBuffer buf,
int size);
}
///////////////////////////////// End of File /////////////////////////////////
Figure 3 MousePos.java
/////////////////////////////////////////////////////////////////
//
// MousePos.java
//
/**
* Demonstrates how to pass a structure to a Win32 function
* using J/Direct.
* @author Jonathan Locke
*/
public class MousePos
{
/**
* Main application entrypoint.
* @param arg Command line arguments
*/
static public void main(String[] arg)
{
POINT pt = new POINT();
POINT opt = new POINT();
while (true)
{
GetCursorPos(pt);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
}
if (pt.x != opt.x || pt.y != opt.y)
{
System.out.println("x = " + pt.x + ", y = " + pt.y);
opt.x = pt.x;
opt.y = pt.y;
}
}
}
/** @dll.import("USER32") */
public static native boolean GetCursorPos(POINT pt);
}
/** @dll.struct() */
class POINT
{
int x;
int y;
}
///////////////////////////////// End of File /////////////////////////////////
Figure 4 GetVersion.java
/////////////////////////////////////////////////////////////////
//
// GetVersion.java
//
/**
* Gets full version information for the current OS
* @author Jonathan Locke
*/
public class GetVersion
{
/**
* Main application entrypoint.
* @param arg Command line arguments
*/
static public void main(String[] arg)
{
OSVERSIONINFO osvi = new OSVERSIONINFO();
if (GetVersionEx(osvi))
{
String platform;
switch (osvi.platformId)
{
case 0: // VER_PLATFORM_WIN32s
platform = "Win32s";
break;
case 1: // VER_PLATFORM_WIN32_WINDOWS
platform = "Windows 95";
break;
case 2: // VER_PLATFORM_WIN32_NT
platform = "Windows NT";
break;
default:
platform = "Unknown Windows platform";
break;
}
System.out.println(platform + " " + osvi.majorVersion +
"." + osvi.minorVersion + "." + osvi.buildNumber +
" " + osvi.csdVersion);
}
else
{
System.out.println("GetVersionEx failed.");
}
}
/** @dll.import("KERNEL32", auto) */
public static native boolean GetVersionEx(OSVERSIONINFO osvi);
}
/** @dll.struct(auto) */
class OSVERSIONINFO
{
public int size = com.ms.dll.DllLib.sizeOf(this);
public int majorVersion;
public int minorVersion;
public int buildNumber;
public int platformId;
/** @dll.structmap([type=TCHAR[128]]) */
public String csdVersion;
}
///////////////////////////////// End of File /////////////////////////////////
Figure 5 EnumWindows.java
/////////////////////////////////////////////////////////////////
//
// EnumWindows.java
//
/**
* Demonstrates how to implement callbacks in J/Direct
* @author Jonathan Locke
*/
public class EnumWindows
{
/**
* Main application entrypoint.
* @param arg Command line arguments
*/
static public void main(String[] arg)
{
if (!EnumWindows(new EnumWindowsProc(), 0))
{
System.out.println("EnumWindows failed.");
}
}
/** @dll.import("USER32") */
public native static boolean EnumWindows(EnumWindowsProc callback,
int lParam);
}
/**
* A class for EnumWindows callback methods
*/
class EnumWindowsProc extends com.ms.dll.Callback
{
/**
* EnumWindows callback method
* @param hwnd Window handle
* @param lParam Data value originally passed to EnumWindows
*/
public boolean callback(int hwnd, int lParam)
{
StringBuffer buf = new StringBuffer(512);
if (GetWindowText(hwnd, buf, buf.capacity()) != 0)
{
System.out.println(Integer.toHexString(hwnd) + " - " + buf);
}
return true;
}
/** @dll.import("USER32",auto) */
public native static int GetWindowText(int hwnd, StringBuffer buf, int cch);
}
///////////////////////////////// End of File /////////////////////////////////
Figure 6 WinJava.java
/////////////////////////////////////////////////////////////////
//
// WinJava.java
//
/**
* A simple Windows program written using J/Direct.
* @author Jonathan Locke
* @dll.import("USER32",auto)
*/
public class WinJava
{
/**
* Main application entrypoint.
* @param arg Command line arguments
*/
static public void main(String[] arg)
{
// Register window class
WNDCLASSEX wc = new WNDCLASSEX();
wc.lpszClassName = "WinJava";
wc.hbrush = 6; // COLOR_WINDOW + 1
wc.lpfnWndProc = com.ms.dll.DllLib.addrOfss
(
com.ms.dll.Root.alloc(new WinJavaWndProc())
);
if (0 == RegisterClassEx(wc))
{
System.out.println("RegisterClassEx failed!");
}
// Create top level "frame" window
int hwnd = CreateWindowEx(0, "WinJava", "WinJava!", 0x00CF0000,
0, 0, 200, 200, 0, 0, 0, 0);
if (hwnd == 0)
{
System.out.println("CreateWindowEx failed!");
}
// Show the window
ShowWindow(hwnd, 1);
// Standard message dispatch loop
MSG msg = new MSG();
while (GetMessage(msg, 0, 0, 0))
{
TranslateMessage(msg);
DispatchMessage(msg);
}
}
static native int RegisterClassEx(WNDCLASSEX w);
static native int CreateWindowEx(int dwExStyle, String lpClassName,
String lpWindowName, int dwStyle, int x, int y, int dx, int dy,
int hwndParent, int hmenu, int hinst, int param);
static native int ShowWindow(int hwnd, int show);
static native boolean GetMessage(MSG msg, int hwnd, int min, int max);
static native boolean TranslateMessage(MSG a);
static native int DispatchMessage(MSG a);
}
/**
* Implementation of the WNDPROC callback for our window
* @dll.import("USER32",auto)
*/
class WinJavaWndProc extends WNDPROC
{
public int callback(int hwnd, int msg, int w, int l)
{
switch (msg)
{
case 0x0f: // WM_PAINT
{
PAINTSTRUCT ps = new PAINTSTRUCT();
int hdc = BeginPaint(hwnd, ps);
String s = "Hello World from J/Direct!";
SetBkMode(hdc, 1); // TRANSPARENT
ExtTextOut(hdc, 10, 10, 0, null, s, s.length(), null);
EndPaint(hwnd, ps);
}
break;
case 0x02: // WM_DESTROY
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, w, l);
}
static native void PostQuitMessage(int exitcode);
static native int DefWindowProc(int hwnd, int msg, int w, int l);
static native int BeginPaint(int hwnd, PAINTSTRUCT ps);
static native boolean EndPaint(int hwnd, PAINTSTRUCT ps);
/** @dll.import("GDI32",auto) */
static native int SetBkMode(int hdc, int mode);
/** @dll.import("GDI32",auto) */
static native boolean ExtTextOut(int hdc, int x, int y,
int options, RECT clipfill, String str, int cch, int[] spacing);
}
abstract class WNDPROC extends com.ms.dll.Callback
{
public abstract int callback(int hwnd, int msg, int w, int l);
}
/** @dll.struct() */
class MSG
{
public int hwnd;
public int message;
public int wParam;
public int lParam;
public int time;
public int pt_x;
public int pt_y;
}
/** @dll.struct(auto) */
class WNDCLASSEX
{
public int cbSize = com.ms.dll.DllLib.sizeOf(this);
public int style = 0;
public int lpfnWndProc = 0;
public int cbClsExtra = 0;
public int cbWndExtra = 0;
public int hinstance = 0;
public int hicon = 0;
public int hcursor = 0;
public int hbrush = 0;
public String lpszMenuName = null;
public String lpszClassName;
public int hiconSm = 0;
}
/** @dll.struct() */
class PAINTSTRUCT
{
int hdc;
boolean fErase;
int rcPaint_left;
int rcPaint_top;
int rcPaint_right;
int rcPaint_bottom;
boolean fRestore;
boolean fIncUpdate;
/** @dll.structmap([type=FIXEDARRAY, size=32]) */
byte[] rgbReserved = new byte[32];
}
/** @dll.struct() */
class RECT
{
int left;
int top;
int right;
int bottom;
}
///////////////////////////////// End of File /////////////////////////////////