public class HelloWindows {
/** @dll.import("winmm") */
static native boolean sndPlaySound(String lpszSound, int fuSound);
public static void main (String args[]) {
boolean returnCode;
int flags = 0;
String soundFile = System.getProperty("com.ms.windir") + "\\MEDIA\\The
Microsoft Sound.wav";
System.out.println("Playing '" + soundFile + "'...");
returnCode = sndPlaySound(soundFile,flags);
}
}
Figure 2 HelloWindowsEx (MultiPlatform)
public class HelloWindowsEx {
/** @dll.import("winmm",entrypoint="sndPlaySound") */
static native boolean playWAV(String lpszSound, int fuSound);
public static void main (String args[]) {
if (System.getProperty("os.name").startsWith("Windows")) {
// If the com.ms.util.SystemVersionManager class is not found,
// catch the NoClassDefFoundError
try {
java.util.Properties vmProperties =
com.ms.util.SystemVersionManager.getVMVersion();
int majorVersion = (new
Integer(vmProperties.getProperty("MajorVersion"))).intValue();
int minorVersion = (new
Integer(vmProperties.getProperty("MinorVersion"))).intValue();
int buildIncrement = (new
Integer(vmProperties.getProperty("BuildIncrement"))).intValue();
if ((majorVersion >= 4) && (minorVersion >= 79) &&
(buildIncrement >= 2164))
{
boolean returnCode;
int flags = 0;
String soundFile = System.getProperty("com.ms.windir") + "\\MEDIA\\The
Microsoft Sound.wav";
System.out.println("Playing '" + soundFile + "'...");
returnCode = playWAV(soundFile,flags);
} else {
// Not the right version of the VM
System.out.println("J/Direct requires VM Version 4.79.2252 or higher.");
}
} catch (NoClassDefFoundError e) {
System.out.println("Class " + e.getMessage() + " not found.");
// If com/ms/util/SystemVersionManager is not on the system,
// then this is probably not the VM.
if (e.getMessage().equals("com/ms/util/SystemVersionManager")) {
System.out.println("J/Direct requires VM Version 4.79.2252 or higher.");
}
}
} else {
// This isn't Windows.
System.out.println("J/Direct requires a Windows system.");
}
}
}
Figure 3
Parameter and Return Value
Native C Type | Java Type |
BOOL | boolean |
BOOL[] | boolean[] |
BYTE | byte |
BYTE* | byte[] |
CHAR | byte |
CLSID* | com.ms.com._Guid |
double | double |
double* | double[] |
DWORD | int |
DWORD* | int[] |
float | float |
float* | float[] |
GUID* | com.ms.com._Guid |
IID* | com.ms.com._Guid |
INT | int |
LONG | int |
LPCSTR | String |
Note: LPCSTR/String may be used as parameters but not as return values. In OLE mode, however, String maps to LPWSTR and both are permitted as return values. The Microsoft VM frees the string by using CoTaskMemFree. | |
LPSTR | StringBuffer |
SAFEARRAY* | com.ms.com.SafeArray |
Note: May only be passed as parameter, not as return value. | |
SHORT | short |
UINT | int |
ULONG | int |
VARIANT* | com.ms.com.Variant |
VOID | void |
Note: void/VOID may not be used as parameters and may only be used as return values. | |
WORD | short |
WORD* | short[] |
__int64 | long |
__int64* | long[] |
COM interface | interface |
Note: Must use jactivex or similar tool to generate interface. | |
function pointer | com.ms.dll.Callback |
Note: May only be passed as parameter, not as return value. | |
pointer to struct | Object |
Note: An IUnknown* is used instead of a pointer to a struct in OLE mode. |
Figure 4
Parameter and Return Value
Visual Basic Type | Java Type |
Byte | byte |
Double | double |
Integer | short |
Long | boolean |
Long | int |
Single | float |
VARIANT | com.ms.com.Variant |
Figure 5 Applet Using J/Direct
import com.ms.security.*;
public class HelloIE extends com.ms.ui.UIApplet {
/** @dll.import("winmm") */
static native boolean sndPlaySound(String lpszSound, int fuSound);
public String soundFile = new String();
public void init() {
PolicyEngine.assertPermission(PermissionID.SYSTEM);
soundFile = System.getProperty("com.ms.windir") + "\\MEDIA\\The Microsoft
Sound.wav";
String buttonText = "Play '" + soundFile + "'";
setLayout(new com.ms.ui.UIBorderLayout(0, 0));
add(new com.ms.ui.UIPushButton(buttonText), "Center");
}
public boolean action(java.awt.Event e, Object arg) {
if ( arg instanceof com.ms.ui.UIPushButton ) {
PolicyEngine.assertPermission(PermissionID.SYSTEM);
boolean returnCode = sndPlaySound(soundFile,0);
}
return true;
}
}
Figure 6 Batch File for Signing CAB
rem //-------------------------
rem // Enable test certificates
rem //-------------------------
setreg.exe 1 true
rem
rem //-------------------------
rem // Compile HelloIE
rem //-------------------------
jvc.exe HelloIE.java
rem
rem //-------------------------
rem // Turn .class into .cab
rem //-------------------------
cabarc.exe -s 6144 n hello.cab HelloIE.class
rem
rem //-------------------------
rem // Make a test certificate
rem //-------------------------
makecert.exe -sv hello.key -n "CN=My Publisher Name" hello.cer
cert2spc.exe hello.cer hello.spc
rem
rem //-------------------------
rem // Digitally sign .cab
rem //-------------------------
signcode.exe -j javasign.dll -jp low -spc hello.spc -v hello.key hello.cab
rem
rem //-------------------------
rem // Launch the web page
rem //-------------------------
start hello.htm