java利用JNA调用Everything-SDK

借用http://www.voidtools.com/上的一句话

Everything Search Engine

Locate files and folders by name instantly.

文件和文件夹搜索利器,发现个软件算是机缘巧合,现在每天都利用它高速搜索那些不知道或者懒得知道在那个角落的文件和文件夹.速度挺不错,还有一些比较强大的功能,有兴趣可以自己去看看.

那么,为什么要写这篇呢?

在实际开发工作里,很多时候需要高速搜索所有硬盘上的文件,而正巧Everything有一个SDK,那么为何不利用其特点来搜索文件呢?

工作中常常使用的是java系的,但Everything官方的例子里只有C#和C的相关代码却没有java的,很是遗憾.遂想要自己搞点名堂出来.

准备工作:

1.Everything-SDK,http://www.voidtools.com/上下载(我的下载时间2015/12/15,版本不同,方法名有可能不同,请注意压缩包里的头文件)

2.选用Everything32.dll来调用(这个是32位版本的,还有64位版本的Everything64.dll)

3.JNA,此文所用版本为jna-4.2.2

4.jdk,jdk1.7.0_45_x86

干货1:

public interface Everything extends Library {


    Everything instanceDll = (Everything) Native.loadLibrary("Everything32", Everything.class);


    public static final int EVERYTHING_OK = 0;
    public static final int EVERYTHING_ERROR_MEMORY = 1;
    public static final int EVERYTHING_ERROR_IPC = 2;
    public static final int EVERYTHING_ERROR_REGISTERCLASSEX = 3;
    public static final int EVERYTHING_ERROR_CREATEWINDOW = 4;
    public static final int EVERYTHING_ERROR_CREATETHREAD = 5;
    public static final int EVERYTHING_ERROR_INVALIDINDEX = 6;
    public static final int EVERYTHING_ERROR_INVALIDCALL = 7;


    public int Everything_SetSearchW(WString lpSearchString);


    public void Everything_SetMatchPath(boolean bEnable);


    public void Everything_SetMatchCase(boolean bEnable);


    public void Everything_SetMatchWholeWord(boolean bEnable);


    public void Everything_SetRegex(boolean bEnable);


    public void Everything_SetMax(int dwMax);


    public void Everything_SetOffset(int dwOffset);


    public boolean Everything_GetMatchPath();


    public boolean Everything_GetMatchCase();


    public boolean Everything_GetMatchWholeWord();


    public boolean Everything_GetRegex();


    public int Everything_GetMax();


    public int Everything_GetOffset();


    public WString Everything_GetSearchW();


    public int Everything_GetLastError();


    public boolean Everything_QueryW(boolean bWait);


    public void Everything_SortResultsByPath();


    public int Everything_GetNumFileResults();


    public int Everything_GetNumFolderResults();


    public int Everything_GetNumResults();


    public int Everything_GetTotFileResults();


    public int Everything_GetTotFolderResults();


    public int Everything_GetTotResults();


    public boolean Everything_IsVolumeResult(int nIndex);


    public boolean Everything_IsFolderResult(int nIndex);


    public boolean Everything_IsFileResult(int nIndex);


    public void Everything_GetResultFullPathNameW(int nIndex, Buffer lpString, int nMaxCount);


    public void Everything_Reset();


}


这里最闹心的还是这个

public void Everything_GetResultFullPathNameW(int nIndex, Buffer lpString, int nMaxCount);

lpString参数在c#的例子里是StringBuilder,c里面定义的是LPWSTR wbuf, 好吧,java里该是个什么呢?百度,谷歌,必应...

JNA的基本类型映射只有Pointer和Buffer和LPWSTR 能对得上号的,使用Pointer的过程中报错,只有Buffer可行了.

c:LPWSTR wbuf 对应 c#:StringBuilder 对应 java:Buffer

干货2:

        Everything.instanceDll.Everything_SetSearchW(new WString("abc"));

        Everything.instanceDll.Everything_QueryW(true);

        Buffer p = CharBuffer.allocate(260);

        for (int i = 0; i < Everything.instanceDll.Everything_GetNumResults(); i++) {
           
            Everything.instanceDll.Everything_GetResultFullPathNameW(i, p, 260);
            
            char[] buf = (char[]) p.array();

            System.out.println(new String(buf).trim());

        }
嗯,鱼送完了,渔呢?去百度,谷歌,必应...那里有你想要的和不想要的,用洪荒之力吐槽吧

你可能感兴趣的:(java)