10 messages in net.java.dev.jna.usersRe: [jna-users] Callback doesn't work...
FromSent OnAttachments
bao baoJun 14, 2009 8:16 pm 
Timothy WallJun 15, 2009 3:55 am 
bao baoJun 15, 2009 4:43 am 
Timothy WallJun 15, 2009 5:16 am 
bao baoJun 15, 2009 6:35 pm 
Timothy WallJun 16, 2009 3:27 am 
bao baoJun 16, 2009 7:15 am 
bao baoJun 16, 2009 8:14 pm 
Timothy WallJun 17, 2009 3:43 am 
bao baoJun 21, 2009 5:46 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:Re: [jna-users] Callback doesn't work at all.Actions...
From:bao bao (btb8@gmail.com)
Date:Jun 16, 2009 8:14:51 pm
List:net.java.dev.jna.users

The source code of callback: C++ code

#pragma once typedef BOOL (CALLBACK*FILESPROC)(LPCTSTR); extern "C" __declspec(dllexport) void EnumFiles(FILESPROC lpEnumFiles,LPCTSTR lpszFileName); int CALLBACK EnumFilesProc (LPCTSTR lpszFileName);

extern "C" __declspec(dllexport) void EnumFiles(FILESPROC lpEnumFiles,LPCTSTR lpszFileName) {

CFileFind file; //If the first file found start finding others BOOL bFind=file.FindFile(lpszFileName,NULL); while (bFind)//If true search... { bFind=file.FindNextFile();

//Call callback function with the found file path lpEnumFiles(file.GetFileName()); } }

java code: import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.win32.StdCallLibrary.StdCallCallback; public interface IListFiles extends Library { IListFiles INSTANCE = (IListFiles) Native.loadLibrary("ListFiles", IListFiles.class); public static interface FILESPROC extends StdCallCallback { public boolean callback(String fname); } public void EnumFiles(FILESPROC lpEnumFiles, String fname); }

public class ListFilesApp { // http://www.codeproject.com/KB/cpp/EnumFiles.aspx IListFiles lib = IListFiles.INSTANCE; int count = 0; boolean flag = true; public ListFilesApp() { lib.EnumFiles(new FILESPROC() { @Override public boolean callback(String fname) { // System.out.println(fname + ">>"); count++; return flag; } }, "c:\\windows\\*.exe"); System.out.println(count); } public static void main(String[] args) { new ListFilesApp(); } } if you have any question, please let me know. Thanks.

Thanks for your quickly reply and your example. I have wroten another program which works well. I will put the code here at tommorrow morning. Thanks again.

On Tue, Jun 16, 2009 at 6:27 PM, Timothy Wall <twal@dev.java.net>wrote:

Here's a better example than "atexit" (which isn't guaranteed to work, since the VM may not be fully functional by the time the exit hooks are run:

import com.sun.jna.*;

public class CallbackTest {

public interface CLibrary extends Library { int SIGUSR1 = 30; interface sig_t extends Callback { void invoke(int signal); } sig_t signal(int signal, sig_t func); int raise(int signal); }

public static void main(String[] args) { CLibrary lib = (CLibrary)Native.loadLibrary("c", CLibrary.class); CLibrary.sig_t fn = new CLibrary.sig_t() { public void invoke(int signal) { System.out.println("received signal " + signal); System.exit(0); } }; CLibrary.sig_t old_handler = lib.signal(CLibrary.SIGUSR1, fn);

lib.raise(CLibrary.SIGUSR1); while (true) { try { Thread.sleep(10); } catch(Exception e) { }

} } }

On Jun 15, 2009, at 9:35 PM, bao bao wrote:

thanks your advice, i have tried to run the example on the project home

page, but i got: JNA: Can't attach to current thread. I couln't fix it, can you help me out?

On Mon, Jun 15, 2009 at 8:16 PM, Timothy Wall <twal@dev.java.net> wrote:

On Jun 15, 2009, at 7:44 AM, bao bao wrote:

in fact i have extracted some code from my project. but i really don't how to use callback in JNA. Can you modify my code or give me an example? Thanks, sir. Regards

The "atexit" example on the project home page is a complete example of using a JNA Callback instance as a function pointer. Make sure you understand how that works before you try something more complex.