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 21, 2009 5:46:36 am
List:net.java.dev.jna.users

hmm, thanks for your adivece, i will modify it to auto-map String to wchar_t*. You're so kindly, best wishes to you.

On Wed, Jun 17, 2009 at 6:44 PM, Timothy Wall <twal@dev.java.net>wrote:

On Jun 16, 2009, at 11:15 PM, bao bao wrote:

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.

Seems odd that the library call would use C calling convention while the callback uses stdcall, but I guess that's your test code.

The problem in this case is that you're not explicitly telling windows whether you want to use the ASCII or unicode version of GetFileName. Windows defaults to unicode, and JNA defaults to UTF8 with the platform default encoding. Use WString in your JNA mapping, or follow the example of the W32API-derived libraries to auto-map String to wchar_t* instead of char*.