HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

How do I disable a Frame's Minimize and Maximize buttons? How do I obtain the amount of a drive's free disk space? How do I retrieve the window handle of a Java Frame? How can I read the label of a disk drive? These and other similar questions are quite prevalent on the Java Usenet newsgroups. The answer to these questions is always the same: you need to use the Java Native Interface (JNI). These types of questions are so commonplace (not only in the newsgroups) that I decided to provide the answers in the form of a Windows-specific Java utility class written with the help of the JNI.

Background
One of Java's many strengths is its platform-independent nature. This feature allows a pure Java program written under a Win32 operating system (i.e., Win95/98/NT) to be easily ported to a UNIX operating system without any change to the program's compiled bytecode. However, there are times when a call to an operating system function from within a Java program is necessary and desirable. The JNI is the mechanism by which a Java program can call an OS specific function. The JNI comes complete with its own set of data types and native methods that can create Java objects (arrays, Strings, etc.), call Java methods, and catch and throw exceptions.

A detailed explanation of the JNI is beyond the scope of this article. For more information look at the JNI section of Sun's Java Tutorial located at www.javasoft.com/docs/books/tutorial/native1.1/index.html.
Alternatively, read Sun's JNI Specification at http://java.sun.com/products/jdk/1.1/docs/guide/jni/spec/jniTOC.doc.html.

Implementation
The Java utility class, JUtil, contains 21 static (class) methods: three pure Java methods whose implementations reside in a Java source file (see Listing 1) and 18 native methods whose implementations reside in a C source file (see Listing 2). Table 1 lists the method names, where the methods are implemented, and a brief method description.

Table 1

The implementations for the 18 native JUtil methods make use of native JNI methods and Win32 API functions to accomplish their tasks. The C function prototypes are generated from the Java class file using the JDK javah utility that creates a C header file (see Listing 3). This C header file must be included in the C source file, which is compiled into a Dynamic Link Library (DLL) file.

Since all the methods are static, there's no need to instantiate an object of the JUtil class to use them. Naturally, you'll have to qualify the method names with the class name when invoking the methods. I call the System class's loadLibrary() method inside a static block of the Java source file (see Listing 1). This method loads the DLL file at class load time, which occurs the first time a JUtil method is called from within your Java program. The JUtil class is made final, so it can't be subclassed (as it's strictly a utility class), and the constructor is made private so the class can't be instantiated.

I'm not going to detail the use of all the JUtil methods as I've provided an HTML help file generated from the Java source file's javadoc comments. Nevertheless, I will cover the most utilized methods.

Quite a few of the JUtil native methods deal with manipulating a Java window. These methods require a Win32 window handle to perform their native operation. You retrieve a Java window's Win32 window handle by using the JUtil class's getHwnd() method and passing it the title of the window. Although it's not an ideal solution, as you can have multiple windows with the same name, it's the only viable way of retrieving the required information. This method allows you to retrieve the Win32 handle of any application window currently running on the system. You must take care to specify the exact window title, including case and punctuation.

When a Java Frame is set unresizable, via the Frame class's setResizable() method, the user is prevented from resizing the Frame with the mouse. Unfortunately, this method doesn't disable the Frame's Minimize and Maximize buttons or its equivalent system menu items. Using the JUtil class's setMinimizeEnabled() and setMaximizeEnabled() methods you can disable those buttons and menu items for the specified window. Except for the Close menu item, there is a JUtil native method to manipulate all of a window's system menu items (i.e., Restore, Move, Size, Minimize and Maximize). (Note: Starting with JDK 1.1.7B and Java 2 [formerly JDK 1.2] setting a Frame unresizable will visually remove the Frame's Minimize and Maximize buttons and disable those system menu items.)

At times you may want to start your main Java program's window in a maximized state. You can initiate this action with Pure Java methods by determining the screen size, via the Toolkit class's getScreenSize() method, and then sizing the window to the retrieved screen dimensions.

However, the presence of the Windows taskbar or any other dockable desktop toolbar ­ such as Microsoft's Office toolbar ­ is not taken into account, resulting in the taskbar becoming obscured by the window. Using the JUtil class's setWindowMaximized() method maximizes the window without obscuring any dockable desktop toolbars. To programmatically minimize (iconify) a window use the JUtil class's setWindowMinimized() method.

Another useful JUtil method is the setWindowRestored() method. This method restores the specified window to its original state before it is minimized or maximized. If the window is minimized (iconified), it not only restores the window's original state, it makes the window active (selected) as well. (Note: Starting with Java 2, the Frame class provides the setState() method to control the Frame's state. Passing Frame.NORMAL to the setState() method will restore the Frame to its original state whereas passing Frame.ICONIFIED will minimize the Frame.)

In my opinion, the most useful of the window-specific JUtil native methods is setWindowAlwaysOnTop(). This method allows you to set the specified window as the topmost window in the z-order. Again, you can achieve this behavior via pure Java methods but the implementation always seems to fall short of the desired behavior. For instance, most programmers will implement the WindowListener interface on a Frame and provide an implementation for the WindowListener's windowDeactivated() method from which the Window class's toFront() method is called to keep the Frame activated at all times. Unfortunately, this results in not allowing the user of the Java program to select any other application currently visible on the desktop. The true behavior of setting a window as the topmost window in the z-order allows the user to activate (select) any other application window while keeping the topmost window visible but deactivated.

I've noted where the newer versions of the JDK (i.e., 1.1.7B and Java 2) obviate the need for a couple of the JUtil methods. However, the window-specific JUtil native methods can be used to manipulate any window from a Java program, not just the Java program's own windows.

I've included a Java test program (see Figure 1) that exercises all of the JUtil methods. This test program along with the javadoc generated HTML help file should be sufficient to get you started using the JUtil class.

Figure 1
Figure 1:

Anomaly
I mentioned earlier that the JUtil class contains native methods that manipulate all of a window's system menu items except the Close menu item. These methods will either enable or disable the system menu item and associated button (if any) on the specified window. While disabling these system menu items works flawlessly, the opposite isn't true. Using these methods to enable the window's system menu items will visually set the menu item as selectable but won't initiate the menu action when the item is actually selected. I'm at a complete loss to explain this behavior as the Win32 API function used to enable the menu items works without any problem when called directly from a non-Java Windows program.

Summary
The JUtil class and its associated DLL file provides a Windows-specific solution for many of the most frequently asked Java Usenet questions. Although the class is not comprehensive, it is quite useful and can be used as a building block for adding your own methods.

About the Author
Pat Paternostro is the director of education for Tri-Com Consulting Group in Rocky Hill, Connecticut. Tri-Com provides programming services for a variety of development tasks. You can reach Pat at [email protected]

	

Listing 1. 

import java.awt.*; 
import java.util.*; 
import java.io.*; 

/** 
 * A Windows®-specific Java utility class. 
 * 
 * @author Pat Paternostro, Tri-Com Consulting Group L.L.C. 
 * @version 1.0 
 */ 
public final class JUtil 
{ 
 /** 
  * The drive type cannot be determined. 
  */ 
 public static final int DRIVE_UNKNOWN = 0; 
  
 /** 
  * The root directory does not exist. 
  */ 
 public static final int DRIVE_NO_ROOT_DIR = 1; 
  
 /** 
  * The disk can be removed from the drive. 
  */ 
 public static final int DRIVE_REMOVABLE = 2; 
  
 /** 
  * The disk cannot be removed from the drive. 
  */ 
 public static final int DRIVE_FIXED = 3; 
  
 /** 
  * The drive is a remote (network) drive. 
  */ 
 public static final int DRIVE_REMOTE = 4; 
  
 /** 
  * The drive is a CD-ROM drive. 
  */ 
 public static final int DRIVE_CDROM = 5; 
  
 /** 
  * The drive is a RAM disk. 
  */ 
 public static final int DRIVE_RAMDISK = 6; 
  

 static 
 { 
  System.loadLibrary("JUtil"); 
 } 
  
 /* Make the constructor private so the class cannot be instantiated */ 
 private JUtil() {} 
  
 /** 
  * Retrieves the character typed at the command console. 
  * 
  * @return the character typed at the command console 
  */ 
 public static native char getConsoleChar(); 
  
 /** 
  * Retrieves the system's logical drives. 
  * 
  * @return an array of <CODE>String</CODE> objects that contains the logical 
  * drive names in the form of <CODE><B>x:\</B></CODE> 
  where <CODE><B>x</B></CODE> denotes the drive letter 
  */ 
 public static native String[] getLogicalDrives(); 
  
 /** 
  * Retrieves the specified drive's free disk space. 
  * 
  * @param <CODE>drive</CODE> a <CODE>String</CODE> that specifies the root directory 
  * of the disk to return free disk space information about. 
  If <CODE>drive</CODE> is 
  * <CODE>null</CODE> the method uses the root of the current directory. 
  * @return the free disk space in bytes 
  * @see #getLogicalDrives 
  */ 
 public static native long getFreeDiskSpace(String drive); 
  
 /** 
  * Retrieves the specified drive's type. 
  * 
  * @param <CODE>drive</CODE> a <CODE>String</CODE> that specifies the root directory 
  * of the disk to return the drive type information about. If <CODE>drive</CODE> is 
  * <CODE>null</CODE> the method uses the root of the current directory. 
  * @return an <CODE>int</CODE> that specifies the drive type 
  * @see #getLogicalDrives 
  * @see #DRIVE_UNKNOWN 
  * @see #DRIVE_NO_ROOT_DIR 
  * @see #DRIVE_REMOVABLE 
  * @see #DRIVE_FIXED 
  * @see #DRIVE_REMOTE 
  * @see #DRIVE_CDROM 
  * @see #DRIVE_RAMDISK 
  */ 
 public static native int getDriveType(String drive); 
  
 /** 
  * Retrieves the specified volume's label. 
  * 
  * @param <CODE>drive</CODE> a <CODE>String</CODE> 
  that specifies the root directory 
  * of the disk to return the volume label. If <CODE>drive</CODE> is 
  * <CODE>null</CODE> the method uses the root of the current directory. 
  * @return a <CODE>String</CODE> containing the specified volume's label 
  * @see #getLogicalDrives 
  */ 
 public static native String getVolumeLabel(String drive); 
  
 /** 
  * Sets the specified volume's label. 
  * 
  * @param <CODE>drive</CODE> a <CODE>String</CODE> that specifies the root directory 
  * of the disk to set the volume label. If <CODE>drive</CODE> is 
  * <CODE>null</CODE> the method uses the root of the current directory. 
  * @param <CODE>label</CODE> a <CODE>String</CODE> that represents the volume's label name. 
The maximum number of characters for a volume label is 11. If <CODE>label</CODE> 
is more that 11 characters 
  * long the volume's label will not be set. If <CODE>label</CODE> 
  is <CODE>null</CODE> the specified volume's 
  * label is deleted. 
  * @return a <CODE>boolean</CODE> specifying whether the volume's label was set. <CODE><B>true</B></CODE> 
  * if the volume's label was set, <CODE><B>false</B></CODE> otherwise. 
  * @see #getLogicalDrives 
  */ 
 public static native boolean setVolumeLabel(String drive, String label); 
  
 /** 
  * Retrieves the current directory. 
  * 
  * @return a <CODE>String</CODE> representing the current directory 
  */ 
 public static native String getCurrentDirectory(); 
  
 /** 
  * Sets the current directory. 
  * 
  * @param <CODE>directory</CODE> a <CODE>String</CODE> that specifies the current directory to set 
  * @return a <CODE>boolean</CODE> indicating if the current directory was set. <CODE><B>true</B></CODE> 
  * if the current directory was set, <CODE><B>false</B></CODE> otherwise. 
  */ 
 public static native boolean setCurrentDirectory(String directory); 
  
 /** 
  * Retrieves the Win32® window handle for the specified window title. 
  * 
  * @param <CODE>title</CODE> the title of the window whose Win32® window handle to retrieve 
  * @return an <CODE>int</CODE> representing the Win32® window handle for the specified window title 
  */ 
 public static native int getHwnd(String title); 

 /** 
  * Minimizes the specified window. 
  * 
  * @param <CODE>hwnd</CODE> the window's Win32® handle 
  * @see #getHwnd 
  */ 
 public static native void setWindowMinimized(int hwnd); 
  
 /** 
  * Maximizes the specified window. 
  * 
  * @param <CODE>hwnd</CODE> the window's Win32® handle 
  * @see #getHwnd 
  */ 
 public static native void setWindowMaximized(int hwnd); 
  
 /** 
  * Restores the specified window. 
  * 
  * @param <CODE>hwnd</CODE> the window's Win32® handle 
  * @see #getHwnd 
  */ 
 public static native void setWindowRestored(int hwnd); 
  
 /** 
  * Enables/disables the specified window's <CODE><B>Restore</B></CODE> button. 
  * 
  * @param <CODE>hwnd</CODE> the window's Win32® handle 
  * @param <CODE>flag</CODE> a <CODE>boolean</CODE> 
    parameter that specifies whether to enable or disable 
  * the window's <CODE><B>Restore</B></CODE> button. 
    <CODE><B>true</B></CODE> enables the button, 
  * <CODE><B>false</B></CODE> disables the button. 
  * @see #getHwnd 
  */ 
 public static native void setWindowRestoreEnabled(int hwnd, boolean flag); 
  
 /** 
  * Enables/disables the specified window's <CODE><B>Move</B></CODE> system menu item. 
  * 
  * @param <CODE>hwnd</CODE> the window's Win32® handle 
  * @param <CODE>flag</CODE> a <CODE>boolean</CODE> 
    parameter that specifies whether to enable or disable 
  * the window's <CODE><B>Move</B></CODE> system menu item. <CODE><B>true</B></CODE> enables the menu item, 
  * <CODE><B>false</B></CODE> disables the menu item. 
  * @see #getHwnd 
  */ 
 public static native void setWindowMoveEnabled(int hwnd, boolean flag); 
  
 /** 
  * Enables/disables the specified window's <CODE><B>Size</B></CODE> system menu item. 
  * 
  * @param <CODE>hwnd</CODE> the window's Win32® handle 
  * @param <CODE>flag</CODE> a <CODE>boolean</CODE> 
    parameter that specifies whether to enable or disable 
  * the window's <CODE><B>Size</B></CODE> system menu item. <CODE><B>true</B></CODE> enables the menu item, 
  * <CODE><B>false</B></CODE> disables the menu item. 
  * @see #getHwnd 
  */ 
 public static native void setWindowSizeEnabled(int hwnd, boolean flag); 
  
 /** 
  * Enables/disables the specified window's <CODE><B>Minimize</B></CODE> button. 
  * 
  * @param <CODE>hwnd</CODE> the window's Win32® handle 
  * @param <CODE>flag</CODE> a <CODE>boolean</CODE> 
    parameter that specifies whether to enable or disable 
  * the window's <CODE><B>Minimize</B></CODE> button. 
    <CODE><B>true</B></CODE> enables the button, 
  * <CODE><B>false</B></CODE> disables the button. 
  * @see #getHwnd 
  */ 
 public static native void setWindowMinimizeEnabled(int hwnd, boolean flag); 
  
 /** 
  * Enables/disables the specified window's <CODE><B>Maximize</B></CODE> button. 
  * 
  * @param <CODE>hwnd</CODE> the window's Win32® handle 
  * @param <CODE>flag</CODE> a <CODE>boolean</CODE> 
    parameter that specifies whether to enable or disable 
  * the window's <CODE><B>Maximize</B></CODE> button. 
    <CODE><B>true</B></CODE> enables the button, 
  * <CODE><B>false</B></CODE> disables the button. 
  * @see #getHwnd 
  */ 
 public static native void setWindowMaximizeEnabled(int hwnd, boolean flag); 
  
 /** 
  * Sets the specified window as the topmost window in the z-order. 
  * 
  * @param <CODE>hwnd</CODE> the window's Win32® handle 
  * @param <CODE>flag</CODE> a <CODE>boolean</CODE> parameter that specifies whether 
  * the window will be the topmost window in the z-order. 
    <CODE><B>true</B></CODE> sets the window as 
  * the topmost window in the z-order, 
    <CODE><B>false</B></CODE> sets the window behind all topmost windows. 
  * @see #getHwnd 
  */ 
 public static native void setWindowAlwaysOnTop(int hwnd, boolean flag); 
  
 /** 
  * Sets the specified container's components to the specified font. 
  * 
  * @param <CODE>cont</CODE> the <CODE>Container</CODE> 
    whose components will be set with the specified font 
  * @param <CODE>f</CODE> the <CODE>Font</CODE> object that will be used 
  * @see java.awt.Container 
  * @see java.awt.Font 
  * @exception IllegalArgumentException if <CODE>cont</CODE> 
    or <CODE>f</CODE> are <CODE>null</CODE> 
  */ 
 public static void setContainerDefaultFont(Container cont, Font f) 
 throws IllegalArgumentException 
 { 
  /* Check for illegal arguments */ 
  if (cont == null || f == null) 
   throw new IllegalArgumentException("This method cannot accept null arguments!"); 
  
  /* Get the container's components */ 
  Component[] comp = cont.getComponents(); 
  
  /* Cycle through the components and set the font */ 
  for (int i = 0; i < comp.length; i++) 
   comp[i].setFont(f); 
 } 

 /** 
  * Sets the specified menubar's menus and menu items to the specified font. 
  * 
  * @param <CODE>mb</CODE> the <CODE>MenuBar</CODE> 
    whose menus and menu items will be set with the specified font 
  * @param <CODE>f</CODE> the <CODE>Font</CODE> object that will be used 
  * @see java.awt.MenuBar 
  * @see java.awt.Font 
  * @exception IllegalArgumentException if <CODE>mb</CODE> 
    or <CODE>f</CODE> are <CODE>null</CODE> 
  */ 
 public static void setMenuBarDefaultFont(MenuBar mb, Font f) throws IllegalArgumentException 
 { 
  int menuCount = 0, menuItemCount = 0; 

  /* Check for legal arguments */ 
  if (mb == null || f == null) 
   throw new IllegalArgumentException("This method cannot accept null arguments!"); 

  /* Get the menubar's menu count */ 
  menuCount = mb.getMenuCount(); 
  
  /* Cycle through the menus */ 
  for (int i = 0; i < menuCount; i++) 
  { 
   /* Set the menu's font */ 
   mb.getMenu(i).setFont(f); 
  
   /* Get the menu's menu item count */ 
   menuItemCount = mb.getMenu(i).getItemCount(); 
  
   /* Cycle through the menu items and set the font */ 
   for (int j = 0; j < menuItemCount; j++) 
    mb.getMenu(i).getItem(j).setFont(f); 
  } 
 } 

 /** 
  * Copies the specified source file to the specified destination file. 
  * 
  * @param <CODE>source</CODE> the source file name 
  * @param <CODE>dest</CODE> the destination file name 
  * @exception IllegalArgumentException if the <CODE>source</CODE> 
    or <CODE>dest</CODE> file names are null or empty 
  * @exception FileNotFoundException if the source file name does not exist 
  * @exception IOException if a problem was encountered reading from 
    the source file or writing to the destination file 
  */ 
 public static void copyFile(String source, String dest) throws IllegalArgumentException, FileNotFoundException, IOException 
 { 
  FileWriter fw = null; 
  FileReader fr = null; 
  BufferedReader br = null; 
  BufferedWriter bw = null; 
  File fileSource = null; 

  /* Check for illegal arguments */ 
  if ((source == null || source.equals("")) || ((dest == null) || dest.equals(""))) 
   throw new IllegalArgumentException("This method cannot accept null or empty (\"\") String arguments!"); 

  try 
  { 
   fr = new FileReader(source); 
   fw = new FileWriter(dest); 
   br = new BufferedReader(fr); 
   bw = new BufferedWriter(fw); 

   /* Determine the size of the buffer to allocate */ 
   fileSource = new File(source); 

   /* Array length must be an int so cast the source file's length */ 
   int fileLength = (int) fileSource.length(); 

   char charBuff[] = new char[fileLength]; 

   while (br.read(charBuff,0,fileLength) != -1) 
    bw.write(charBuff,0,fileLength); 
  } 
  finally 
  { 
   try 
   { 
    if (br != null) 
     br.close(); 
    if (bw != null) 
     bw.close(); 
   } 
   catch(IOException ioe){} 
  } 
 } 
} 
  

Listing 2. 

#include "JUtil.h" 
#define STRICT 
#include <windows.h> 
#include <conio.h> 
  

JNIEXPORT jchar JNICALL Java_JUtil_getConsoleChar(JNIEnv *env, jclass obj) 
{ 
 return _getch(); 
} 

JNIEXPORT jobjectArray JNICALL Java_JUtil_getLogicalDrives(JNIEnv *env, jclass obj) 
{ 
 DWORD dStrLength; 
 char szDrives[256]; 
 jobjectArray strArray = NULL; 

 memset(szDrives,'\0',sizeof(szDrives)); 
 dStrLength = GetLogicalDriveStrings(sizeof(szDrives),szDrives); 

 /* 
 ** The length of the array is returned from the above function minus the 
 ** terminating null character. The function will place the names of the 
 ** logical drives in the passed character array and null separate the names. 
 ** The names will be in the form of X:\ where X denotes the drive letter. 
 ** Therefore the length returned can be divided by 4 (the total number of 
 ** characters in each name plus the null separator) to determine the number 
 ** of drives returned from the function. This number is then used to allocate 
 ** storage for the array of Strings that will passed back to the caller of this 
 ** function. 
 */ 
 if (dStrLength) 
 { 
  int i, nDriveCount, nOffset = 0; 

  nDriveCount = dStrLength/4; 

  /* Allocate storage for Java array of String objects */ 
  strArray = (*env)->NewObjectArray(env,nDriveCount,(*env)->FindClass(env,"java/lang/String"),
(*env)->NewStringUTF(env, "")); 

  /* Cycle through the string to get the names of the logical drives */ 
  for (i = 0; i < nDriveCount; i++) 
  { 
   /* Add the logical drive name as a Java String to the Java object array */ 
   (*env)->SetObjectArrayElement(env, strArray, i, 
   (*env)->NewStringUTF(env, szDrives)); 

   /* Advance to the next logical drive name */ 
   szDrives[0] = szDrives[nOffset += 4]; 
  } 
 } 

 return strArray; 
} 

JNIEXPORT jlong JNICALL Java_JUtil_getFreeDiskSpace
(JNIEnv *env, jclass obj, jstring drive) 
{ 
 DWORD sectorsPerCluster = 0, bytesPerSector = 0; 
 DWORD freeClusters = 0, totalClusters = 0, freeBytes = 0; 
 BOOL fReturn; 
 const char *str = NULL; 

 str = (*env)->GetStringUTFChars(env, drive, 0); 
 fReturn = GetDiskFreeSpace
 (str,§orsPerCluster,&bytesPerSector,&freeClusters,&totalClusters); 
 (*env)->ReleaseStringUTFChars(env, drive, str); 

 if (fReturn) 
  freeBytes = freeClusters * sectorsPerCluster * bytesPerSector; 

 return freeBytes; 
} 

JNIEXPORT jint JNICALL Java_JUtil_getDriveType
(JNIEnv *env, jclass obj, jstring drive) 
{ 
 const char *str = NULL; 
 UINT uReturn; 

 str = (*env)->GetStringUTFChars(env, drive, 0); 
 uReturn = GetDriveType(str); 
 (*env)->ReleaseStringUTFChars(env, drive, str); 

 return uReturn; 
} 

JNIEXPORT jstring JNICALL Java_JUtil_getVolumeLabel
(JNIEnv *env, jclass obj, jstring drive) 
{ 
 char szVolumeName[256]; 
 const char *str = NULL; 
 DWORD maxCompLen = 0, fileSystemFlags = 0; 

 memset(szVolumeName,'\0',sizeof(szVolumeName)); 
 str = (*env)->GetStringUTFChars(env, drive, 0); 
 GetVolumeInformation(str,szVolumeName,sizeof
 (szVolumeName),NULL,&maxCompLen,&fileSystemFlags,NULL,0); 
 (*env)->ReleaseStringUTFChars(env, drive, str); 

 return (*env)->NewStringUTF(env,szVolumeName); 
} 

JNIEXPORT jboolean JNICALL Java_JUtil_setVolumeLabel
(JNIEnv *env, jclass obj, jstring drive, jstring label) 
{ 
 BOOL fReturn; 
 const char *str1 = NULL, *str2 = NULL; 

 str1 = (*env)->GetStringUTFChars(env, drive, 0); 
 str2 = (*env)->GetStringUTFChars(env, label, 0); 
 fReturn = SetVolumeLabel(str1,str2); 
 (*env)->ReleaseStringUTFChars(env, drive, str1); 
 (*env)->ReleaseStringUTFChars(env, label, str2); 

 return fReturn; 
} 

JNIEXPORT jstring JNICALL Java_JUtil_getCurrentDirectory
(JNIEnv *env, jclass obj) 
{ 
 char szCurrentDir[_MAX_PATH]; 

 memset(szCurrentDir,'\0',sizeof(szCurrentDir)); 
 GetCurrentDirectory(_MAX_PATH,szCurrentDir); 

 return (*env)->NewStringUTF(env, szCurrentDir); 
} 

JNIEXPORT jboolean JNICALL Java_JUtil_setCurrentDirectory
(JNIEnv *env, jclass obj, jstring directory) 
{ 
 BOOL fReturn; 
 const char *str = NULL; 
  
 str = (*env)->GetStringUTFChars(env, directory, 0); 
 fReturn = SetCurrentDirectory(str); 
 (*env)->ReleaseStringUTFChars(env, directory, str); 

 return fReturn; 
} 

JNIEXPORT jint JNICALL Java_JUtil_getHwnd
(JNIEnv *env, jclass obj, jstring title) 
{ 
 HWND hwnd = NULL; 
 const char *str = NULL; 

 str = (*env)->GetStringUTFChars(env, title, 0); 
 hwnd = FindWindow(NULL,str); 
 (*env)->ReleaseStringUTFChars(env, title, str); 

 return (jint) hwnd; 
} 

JNIEXPORT void JNICALL Java_JUtil_setWindowMinimized
(JNIEnv *env, jclass obj, jint hwnd) 
{ 
 SendMessage((HWND) hwnd,WM_SYSCOMMAND,SC_MINIMIZE,0L); 

 return; 
} 

JNIEXPORT void JNICALL Java_JUtil_setWindowMaximized
(JNIEnv *env, jclass obj, jint hwnd) 
{ 
 SendMessage((HWND) hwnd,WM_SYSCOMMAND,SC_MAXIMIZE,0L); 

 return; 
} 

JNIEXPORT void JNICALL Java_JUtil_setWindowRestored
(JNIEnv *env, jclass obj, jint hwnd) 
{ 
 SendMessage((HWND) hwnd,WM_SYSCOMMAND,SC_RESTORE,0L); 

 return; 
} 

JNIEXPORT void JNICALL Java_JUtil_setWindowRestoreEnabled
(JNIEnv *env, jclass obj, jint hwnd, jboolean flag) 
{ 
 HMENU hSysMenu = NULL; 

 hSysMenu = GetSystemMenu((HWND) hwnd,FALSE); 

 if (!flag) 
  ModifyMenu(hSysMenu,0,MF_BYPOSITION|MF_GRAYED|MF_STRING,0,"&Restore"); 
 else 
  ModifyMenu(hSysMenu,0,MF_BYPOSITION|MF_ENABLED|MF_STRING,0,"&Restore"); 

 return; 
} 

JNIEXPORT void JNICALL Java_JUtil_setWindowMoveEnabled
(JNIEnv *env, jclass obj, jint hwnd, jboolean flag) 
{ 
 HMENU hSysMenu = NULL; 

 hSysMenu = GetSystemMenu((HWND) hwnd,FALSE); 

 if (!flag) 
  ModifyMenu(hSysMenu,1,MF_BYPOSITION|MF_GRAYED|MF_STRING,0,"&Move"); 
 else 
  ModifyMenu(hSysMenu,1,MF_BYPOSITION|MF_ENABLED|MF_STRING,0,"&Move"); 

 return; 
} 

JNIEXPORT void JNICALL Java_JUtil_setWindowSizeEnabled
(JNIEnv *env, jclass obj, jint hwnd, jboolean flag) 
{ 
 HMENU hSysMenu = NULL; 

 hSysMenu = GetSystemMenu((HWND) hwnd,FALSE); 

 if (!flag) 
  ModifyMenu(hSysMenu,2,MF_BYPOSITION|MF_GRAYED|MF_STRING,0,"&Size"); 
 else 
  ModifyMenu(hSysMenu,2,MF_BYPOSITION|MF_ENABLED|MF_STRING,0,"&Size"); 

 return; 
} 

JNIEXPORT void JNICALL Java_JUtil_setWindowMinimizeEnabled
(JNIEnv *env, jclass obj, jint hwnd, jboolean flag) 
{ 
 HMENU hSysMenu = NULL; 

 hSysMenu = GetSystemMenu((HWND) hwnd,FALSE); 

 if (!flag) 
  ModifyMenu(hSysMenu,3,MF_BYPOSITION|MF_GRAYED|MF_STRING,0,"Mi&nimize"); 
 else 
  ModifyMenu(hSysMenu,3,MF_BYPOSITION|MF_ENABLED|MF_STRING,0,"Mi&nimize"); 

 return; 
} 

JNIEXPORT void JNICALL Java_JUtil_setWindowMaximizeEnabled
(JNIEnv *env, jclass obj, jint hwnd, jboolean flag) 
{ 
 HMENU hSysMenu = NULL; 

 hSysMenu = GetSystemMenu((HWND) hwnd,FALSE); 

 if (!flag) 
  ModifyMenu(hSysMenu,4,MF_BYPOSITION|MF_GRAYED|MF_STRING,0,"Ma&ximize"); 
 else 
  ModifyMenu(hSysMenu,4,MF_BYPOSITION|MF_ENABLED|MF_STRING,0,"Ma&ximize"); 

 return; 
} 

JNIEXPORT void JNICALL Java_JUtil_setWindowAlwaysOnTop
(JNIEnv *env, jclass obj, jint hwnd, jboolean flag) 
{ 
 if (flag) 
  SetWindowPos((HWND) hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE); 
 else 
  SetWindowPos((HWND) hwnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE); 

 return; 
} 
  

Listing 3. 

/* DO NOT EDIT THIS FILE - it is machine generated */ 
#include <jni.h> 
/* Header for class JUtil */ 

#ifndef _Included_JUtil 
#define _Included_JUtil 
#ifdef __cplusplus 
extern "C" { 
#endif 
/* 
 * Class:     JUtil 
 * Method:    getConsoleChar 
 * Signature: ()C 
 */ 
JNIEXPORT jchar JNICALL Java_JUtil_getConsoleChar 
  (JNIEnv *, jclass); 

/* 
 * Class:     JUtil 
 * Method:    getLogicalDrives 
 * Signature: ()[Ljava/lang/String; 
 */ 
JNIEXPORT jobjectArray JNICALL Java_JUtil_getLogicalDrives 
  (JNIEnv *, jclass); 

/* 
 * Class:     JUtil 
 * Method:    getFreeDiskSpace 
 * Signature: (Ljava/lang/String;)J 
 */ 
JNIEXPORT jlong JNICALL Java_JUtil_getFreeDiskSpace 
  (JNIEnv *, jclass, jstring); 

/* 
 * Class:     JUtil 
 * Method:    getDriveType 
 * Signature: (Ljava/lang/String;)I 
 */ 
JNIEXPORT jint JNICALL Java_JUtil_getDriveType 
  (JNIEnv *, jclass, jstring); 

/* 
 * Class:     JUtil 
 * Method:    getVolumeLabel 
 * Signature: (Ljava/lang/String;)Ljava/lang/String; 
 */ 
JNIEXPORT jstring JNICALL Java_JUtil_getVolumeLabel 
  (JNIEnv *, jclass, jstring); 

/* 
 * Class:     JUtil 
 * Method:    setVolumeLabel 
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Z 
 */ 
JNIEXPORT jboolean JNICALL Java_JUtil_setVolumeLabel 
  (JNIEnv *, jclass, jstring, jstring); 

/* 
 * Class:     JUtil 
 * Method:    getCurrentDirectory 
 * Signature: ()Ljava/lang/String; 
 */ 
JNIEXPORT jstring JNICALL Java_JUtil_getCurrentDirectory 
  (JNIEnv *, jclass); 

/* 
 * Class:     JUtil 
 * Method:    setCurrentDirectory 
 * Signature: (Ljava/lang/String;)Z 
 */ 
JNIEXPORT jboolean JNICALL Java_JUtil_setCurrentDirectory 
  (JNIEnv *, jclass, jstring); 

/* 
 * Class:     JUtil 
 * Method:    getHwnd 
 * Signature: (Ljava/lang/String;)I 
 */ 
JNIEXPORT jint JNICALL Java_JUtil_getHwnd 
  (JNIEnv *, jclass, jstring); 

/* 
 * Class:     JUtil 
 * Method:    setWindowMinimized 
 * Signature: (I)V 
 */ 
JNIEXPORT void JNICALL Java_JUtil_setWindowMinimized 
  (JNIEnv *, jclass, jint); 

/* 
 * Class:     JUtil 
 * Method:    setWindowMaximized 
 * Signature: (I)V 
 */ 
JNIEXPORT void JNICALL Java_JUtil_setWindowMaximized 
  (JNIEnv *, jclass, jint); 

/* 
 * Class:     JUtil 
 * Method:    setWindowRestored 
 * Signature: (I)V 
 */ 
JNIEXPORT void JNICALL Java_JUtil_setWindowRestored 
  (JNIEnv *, jclass, jint); 

/* 
 * Class:     JUtil 
 * Method:    setWindowRestoreEnabled 
 * Signature: (IZ)V 
 */ 
JNIEXPORT void JNICALL Java_JUtil_setWindowRestoreEnabled 
  (JNIEnv *, jclass, jint, jboolean); 

/* 
 * Class:     JUtil 
 * Method:    setWindowMoveEnabled 
 * Signature: (IZ)V 
 */ 
JNIEXPORT void JNICALL Java_JUtil_setWindowMoveEnabled 
  (JNIEnv *, jclass, jint, jboolean); 

/* 
 * Class:     JUtil 
 * Method:    setWindowSizeEnabled 
 * Signature: (IZ)V 
 */ 
JNIEXPORT void JNICALL Java_JUtil_setWindowSizeEnabled 
  (JNIEnv *, jclass, jint, jboolean); 

/* 
 * Class:     JUtil 
 * Method:    setWindowMinimizeEnabled 
 * Signature: (IZ)V 
 */ 
JNIEXPORT void JNICALL Java_JUtil_setWindowMinimizeEnabled 
  (JNIEnv *, jclass, jint, jboolean); 

/* 
 * Class:     JUtil 
 * Method:    setWindowMaximizeEnabled 
 * Signature: (IZ)V 
 */ 
JNIEXPORT void JNICALL Java_JUtil_setWindowMaximizeEnabled 
  (JNIEnv *, jclass, jint, jboolean); 

/* 
 * Class:     JUtil 
 * Method:    setWindowAlwaysOnTop 
 * Signature: (IZ)V 
 */ 
JNIEXPORT void JNICALL Java_JUtil_setWindowAlwaysOnTop 
  (JNIEnv *, jclass, jint, jboolean); 

#ifdef __cplusplus 
} 
#endif 
#endif 


  

Download Assoicated Source Files (Zip format - 28.5 KB)
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.