3 messages in net.java.dev.jna.users[jna-users] Re: Hello friend
FromSent OnAttachments
Timothy WallOct 8, 2008 8:36 am 
Timothy WallOct 8, 2008 10:58 am 
Timothy WallOct 8, 2008 11:45 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:[jna-users] Re: Hello friendActions...
From:Timothy Wall (twal@dev.java.net)
Date:Oct 8, 2008 11:45:33 am
List:net.java.dev.jna.users

PLEASE DIRECT RESPONSES TO THE JNA USERS LIST. THIS MEANS YOU, D.ULZII-ORSHIKH!

On Oct 8, 2008, at 2:14 PM, D.Ulzii-Orshikh wrote:

I call Process32First function to get info of first process of running processes. But it does not return anything. It does not change my pe variable's value.

Here is complete code of my class :

import com.sun.jna.FromNativeContext; import com.sun.jna.Native; import com.sun.jna.NativeLong; import com.sun.jna.Platform; import com.sun.jna.Pointer; import com.sun.jna.PointerType; import com.sun.jna.Structure;

//import com.sun.jna.Structure.ByReference; import com.sun.jna.ptr.ByReference; import com.sun.jna.ptr.PointerByReference; import com.sun.jna.win32.StdCallLibrary; import com.sun.jna.win32.W32APIFunctionMapper; import com.sun.jna.win32.W32APITypeMapper;

import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date;

import java.util.HashMap; import java.util.Map;

public class Win32IdleTime {

static byte[] buff= new byte[1024]; static char[] tmp= new char[1024]; static String buf =" ";

public interface Kernel32 extends StdCallLibrary { Kernel32 INSTANCE = (Kernel32)Native.loadLibrary("kernel32", Kernel32.class);

You should pass the same options to Native.loadLibrary that the original Kernel32 definition does. This will ensure that the appropriate version of each function is used (I'm surprised you haven't complained about an UnsatisfiedLinkError, which is what you would get with the code you've posted).

class HANDLE extends PointerType { /** Override to the appropriate object for INVALID_HANDLE_VALUE. */ public Object fromNative(Object nativeValue, FromNativeContext context) { Object o = super.fromNative(nativeValue, context); if (INVALID_HANDLE_VALUE.equals(o)) return INVALID_HANDLE_VALUE; return o; } } /** Constant value representing an invalid HANDLE. */ HANDLE INVALID_HANDLE_VALUE = new HANDLE() { { super.setPointer(Pointer.createConstant(-1)); } public void setPointer(Pointer p) { throw new UnsupportedOperationException("Immutable reference"); } };

HANDLE CreateToolhelp32Snapshot(int dwFlags,int th32ProcessID); boolean CloseHandle(HANDLE hObject);

public class PROCESSENTRY32 extends Structure {

public static class ByReference extends PROCESSENTRY32 implements Structure.ByReference {};

This definition is redundant. PROCESSENTRY32 will behave identically to PROCESSENTRY32.ByReference when used as a function parameter.

public int dwSize; public int cntUsage; public int th32ProcessID; public Pointer th32DefaultHeapID; //ULONG_PTR public int th32ModuleID; public int cntThreads; public int th32ParentProcessID; public NativeLong pcPriClassBase; public int dwFlags; public char[] szExeFile;

public PROCESSENTRY32() { szExeFile = new char[250]; } }

boolean Process32First(HANDLE hSnapshot, PROCESSENTRY32.ByReference lppe); boolean Process32Next(HANDLE hSnapshot, PROCESSENTRY32.ByReference lppe); //PeByReference lppe

HANDLE OpenProcess(int dwDesiredAccess, boolean bInheritHandle, int dwProcessId); int GetPriorityClass(HANDLE hProcess);

};

public static boolean get_Process_List() { Kernel32.HANDLE hProcessSnap; Kernel32.HANDLE hProcess; Kernel32.PROCESSENTRY32 pe32; Kernel32.PROCESSENTRY32.ByReference pe;

int dwPriorityClass;

if (Platform.isWindows()) {

Kernel32 kernel32 = Kernel32.INSTANCE;

pe = new Kernel32.PROCESSENTRY32.ByReference();

hProcessSnap = kernel32.CreateToolhelp32Snapshot( 0x00000002, 0 ); if( hProcessSnap == kernel32.INVALID_HANDLE_VALUE ) { System.out.println("CreateToolhelp32Snapshot (of processes)"); return false; } // Set the size of the structure before using it. // Retrieve information about the first process, // and exit if unsuccessful if( !kernel32.Process32First( hProcessSnap, pe ) ) {

kernel32.CloseHandle( hProcessSnap ); // clean the snapshot object return false;

}

// Now walk the snapshot of processes, and // display information about each process in turn do {

System.out.println("\n \n==================================================== =" ); System.out.println("\nPROCESS NAME: " + pe.szExeFile );

This will always print the same value, which is the address of your character array. You need to extract the C string with Native.toString(char[]).

System .out.println("\n-----------------------------------------------------" );

// Retrieve the priority class. dwPriorityClass = 0; hProcess = kernel32.OpenProcess( 0x0400, false, pe.th32ProcessID ); if( hProcess == null ) System.out.println("OpenProcess"); else { dwPriorityClass = kernel32.GetPriorityClass( hProcess ); if( dwPriorityClass==0 ) System.out.println("GetPriorityClass"); kernel32.CloseHandle( hProcess ); }

System.out.println( "\n Process ID = "+ pe.th32ProcessID ); System.out.println( "\n Thread count = "+ pe.cntThreads ); System.out.println( "\n Parent process ID = "+ pe.th32ParentProcessID ); System.out.println( "\n Priority base = "+ pe.pcPriClassBase ); if( dwPriorityClass!=0 ) System.out.println( "\n Priority class = "+ dwPriorityClass );

} while( kernel32.Process32Next( hProcessSnap, pe ) );

kernel32.CloseHandle( hProcessSnap ); return true;

} return false; }

Sincerely, Olzii

-----Original Message----- From: Timothy Wall [mailto:twal@dev.java.net] Sent: Thursday, October 09, 2008 1:59 AM To: D.Ulzii-Orshikh Cc: use@jna.dev.java.net Subject: Re: Hello friend

Please address replies to the jna users list. If you look at the description of that list on the project page, it recomments providing the C declarations that your are trying to map into Java.

In addition, a detailed description of what exactly is not working would help. Does the function never return? does it crash your program? Does it return data you don't expect?

On Oct 8, 2008, at 11:50 AM, D.Ulzii-Orshikh wrote:

Dear Timothy Wall,

Thank you for your quick reply.

Sorry I wrote my commented line, Last line was :

kernel32.Process32First( hProcessSnap, pe)

So what is wrong with my code ? It is not working.

Sincerely, Olzii

-----Original Message----- From: Timothy Wall [mailto:twal@dev.java.net] Sent: Wednesday, October 08, 2008 11:37 PM To: D.Ulzii-Orshikh Cc: use@jna.dev.java.net Subject: Re: Hello friend

All structure parameters are passed by reference unless explicitly passed by value by using the ByValue interface.

On Oct 8, 2008, at 11:31 AM, D.Ulzii-Orshikh wrote:

Dear Timothy Wall,

I am writing simple Java application that detects foreground window. And I need to get EXE filename of current window. But I can not pass by reference my PROCESSENTRY32 structure to function Process32First function. Please help me friend ?

Here is my code : public class PROCESSENTRY32 extends Structure {

public static class ByReference extends PROCESSENTRY32 implements Structure.ByReference {};

public int dwSize; public int cntUsage; public int th32ProcessID; public Pointer th32DefaultHeapID; //ULONG_PTR public int th32ModuleID; public int cntThreads; public int th32ParentProcessID; public NativeLong pcPriClassBase; public int dwFlags; public char[] szExeFile;

public PROCESSENTRY32() { szExeFile = new char[250]; } } .

boolean Process32First(HANDLE hSnapshot, PROCESSENTRY32 lppe);

. Kernel32.PROCESSENTRY32.ByReference pe; pe = new Kernel32.PROCESSENTRY32.ByReference(); . kernel32.Process32First( hProcessSnap, pe32 )

Thank you , Please help me friend.

Olzii