_The Java Native Method Interface and Windows_
by Andrew Wilson

Listing One
import ProcessEntry32;
import java.util.*;

class ToolHelp extends Object
{
	static { System.loadLibrary("tlh"); } 
	// Load the native system library

	final private int TH32CS_SNAPPROCESS = 2;

	private Vector ProcEntries;
	public ToolHelp()
	{
		ProcEntries = new Vector();
		CreateToolhelp32Snapshot( 
		TH32CS_SNAPPROCESS, 0, ProcEntries);
	}
	public Vector getProcList()
	{
		return ProcEntries;
	}
	// native function declaration
	private native boolean CreateToolhelp32Snapshot( 
		int dwFlags, int th32ProcessID, Vector vector);
}

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

#ifndef _Included_ToolHelp
#define _Included_ToolHelp
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     ToolHelp
 * Method:    CreateToolhelp32Snapshot
 * Signature: (IILjava/util/Vector;)Z
 */
JNIEXPORT jboolean JNICALL Java_ToolHelp_CreateToolhelp32Snapshot
  (JNIEnv *, jobject, jint, jint, jobject);

#ifdef __cplusplus
}
#endif
#endif


Listing Three
JNIEXPORT jboolean JNICALL Java_ToolHelp_CreateToolhelp32Snapshot
  (JNIEnv *env, jobject, jint, jint process, jobject vector)
{
	HANDLE handle =  CreateToolhelp32Snapshot(
		TH32CS_SNAPPROCESS, process);
	BOOL first = TRUE;
	jobject ProcEntry32;
	PROCESSENTRY32 ppe;
	do {
		ppe.dwSize = sizeof( PROCESSENTRY32 );
		if(first)
		{
			first = FALSE;
			if ( !Process32First( handle, &ppe))
				return FALSE;
		}
		else
		{
			if ( !Process32Next( handle, &ppe))
				return FALSE;
		}
		ProcEntry32 = CreateProcessEntry32(env, &ppe);
	}
	while(AddObject(env, vector,ProcEntry32 ) );	
	return TRUE;
}


Listing Four
jobject CreateProcessEntry32( JNIEnv * env, LPPROCESSENTRY32 lppe)
{
	jclass jcProcEntry32 =  env->FindClass("ProcessEntry32");
	if(jcProcEntry32 == NULL)
		return NULL;
	jmethodID jmProcEntry32 = env->GetMethodID(jcProcEntry32,
		"<init>", "()V");
	if(jmProcEntry32 == NULL)
		return NULL;
  	jobject joProcEntry32 = env->NewObject(jcProcEntry32,jmProcEntry32,"");
	if(joProcEntry32 == NULL)
		return NULL;
	if (!SetDWORDField(env, lppe->cntUsage, joProcEntry32, "cntUsage"))
		return NULL;
	if (!SetDWORDField(env, lppe->th32ProcessID, joProcEntry32, "th32ProcessID"))
		return NULL;
	if (!SetDWORDField(env, lppe->th32DefaultHeapID, joProcEntry32, 
                                                        "th32DefaultHeapID"))
		return NULL;
	if (!SetDWORDField(env, lppe->cntThreads, joProcEntry32, "cntThreads"))
		return NULL;
	if (!SetDWORDField(env, lppe->th32ModuleID, joProcEntry32, 
                                                      "th32ModuleID"))
		return NULL;
	if (!SetDWORDField(env, lppe->th32ParentProcessID, joProcEntry32, 
                                                       "th32ParentProcessID"))
		return NULL;
	if (!SetDWORDField(env, lppe->pcPriClassBase, joProcEntry32, 
                                                            "pcPriClassBase"))
		return NULL;
	if (!SetDWORDField(env, lppe->dwFlags, joProcEntry32, "dwFlags"))
		return NULL;
	if (!SetStringField(env, lppe->szExeFile, joProcEntry32, "szExeFile"))
		return NULL;
	return joProcEntry32;
}

Listing Five
class ProcessEntry32 extends Object
{
	public int cntUsage;
	public int th32ProcessID;
	public int th32DefaultHeapID;
	public int th32ModuleID;
	public int cntThreads;
	public int th32ParentProcessID;
	public int pcPriClassBase;
	public int dwFlags;
	public String szExeFile;
}

Listing Six
BOOL AddObject (JNIEnv *env, jobject vector, jobject ProcEntry32)
{
	if(ProcEntry32 == NULL)
		return FALSE;
	jmethodID jmAddElement = env->GetMethodID(
		env->GetObjectClass(vector),
		"addElement", 
		"(Ljava/lang/Object;)V");
	if(jmAddElement == NULL)
		return FALSE;
	env->CallVoidMethod(vector, jmAddElement, ProcEntry32);
	return TRUE;
}

Listing Seven
BOOL SetDWORDField(JNIEnv * env,DWORD field,jobject procEntry32,char *szField)
{
	jfieldID jfield =  env->GetFieldID( 
		env->GetObjectClass(procEntry32),
		szField, 
		"I");
	if ( !IsValidField(jfield, szField) )
		return FALSE;
	env->SetIntField(procEntry32, jfield, field);
	return TRUE;
}




